import DataTable, { Column } from '@/components/costum/DataTable';
import PageHeader from '@/components/costum/page_header';
import { formatDate, formatNumber } from '@/helper';
import { TransaksiTabunganModel } from '@/types';
import { useTransaksiTabungan } from '@/useQuery/query';
import { ArrowLeft } from 'lucide-react';
import { useState } from 'react';
import { Link, useParams } from 'react-router';

export default function StatementBungaDepositoPage() {
    const { id } = useParams();

    const [filter, setFilter] = useState({
        tgl_awal: formatDate(new Date()),
        tgl_akhir: formatDate(new Date()),
        rekening: id,
    });

    const transaksiTabungan = useTransaksiTabungan(filter);

    let saldoAkhir = 0;

    const columns: Column<TransaksiTabunganModel>[] = [
        {
            label: <center>Tanggal</center>,
            width: 100,
            align: 'center',
            value: (row: TransaksiTabunganModel) => formatDate(row.trans_tglbuku),
        },
        {
            label: <center>Kode</center>,
            width: 100,
            align: 'center',
            value: (row: TransaksiTabunganModel) => row.trans_kode,
        },
        {
            label: 'Keterangan',
            value: (row: TransaksiTabunganModel) => row.keterangan,
        },
        {
            label: <center>Masuk</center>,
            width: 120,
            align: 'right',
            value: (row: TransaksiTabunganModel) => formatNumber(row.setor),
        },
        {
            label: <center>Keluar</center>,
            width: 120,
            align: 'right',
            value: (row: TransaksiTabunganModel) => formatNumber(row.tarik),
        },
        {
            label: <center>Saldo Akhir</center>,
            width: 120,
            align: 'right',
            value: (row: TransaksiTabunganModel) => {
                saldoAkhir += (row?.setor ?? 0) - (row?.tarik ?? 0);
                return formatNumber(saldoAkhir);
            },
        },
    ];

    return (
        <>
            <div className="container">
                <PageHeader
                    title="Statement Bunga Deposito"
                    breadcrumb={[
                        {
                            title: 'Deposito',
                            path: '/app/master/deposito',
                        },
                        {
                            title: `${id}`,
                            path: `/app/master/deposito/${id}`,
                        },
                    ]}
                />
                <div className="row mb-2">
                    <div className="col-md-6">
                        <div className="row">
                            <label className="col-form-label col-md-2">Periode</label>
                            <div className="col-md-8">
                                <div className="input-group">
                                    <input
                                        className="form-control"
                                        type="date"
                                        value={filter.tgl_awal}
                                        onChange={(e) => setFilter({ ...filter, tgl_awal: e.target.value })}
                                    />
                                    <span className="input-group-text">Sampai</span>
                                    <input
                                        className="form-control"
                                        type="date"
                                        value={filter.tgl_akhir}
                                        onChange={(e) => setFilter({ ...filter, tgl_akhir: e.target.value })}
                                    />
                                </div>
                            </div>
                            <div className="col-md-2">
                                <div className="d-flex">
                                    <button className="btn btn-primary flex-grow-1">Preview</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div className="mb-2">
                    <DataTable
                        data={transaksiTabungan.data?.filter((item: TransaksiTabunganModel) => item.status == 1) || []}
                        column={columns}
                        pagination={false}
                        height={500}
                    />
                </div>
                <div>
                    <Link to={`/app/master/deposito/${id}`} className="btn btn-danger">
                        <div className="d-flex align-items-center gap-2">
                            <ArrowLeft size={18} />
                            Kembali
                        </div>
                    </Link>
                </div>
            </div>
        </>
    );
}
