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

export default function StatementTransaksiPenempatanPage() {
    const { id } = useParams();
    const detailPenempatan = useDetailPenempatan(id);

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

    const transaksiPenempatanBank = useTransaksiPenempatanBank(filter);

    let saldoAkhir = 0;

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

    return (
        <>
            <div className="container">
                <PageHeader
                    title="Statement Transaksi Penempatan Bank"
                    breadcrumb={[
                        {
                            title: 'Penempatan',
                            path: '/app/master/kas',
                        },
                        {
                            title: `${id}`,
                            path: `/app/master/penempatan/${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={transaksiPenempatanBank.data?.filter((item: TransaksiPenempatanModel) => item.status == 1) || []}
                        column={columns}
                        pagination={false}
                        height={500}
                    />
                </div>
                <div>
                    <Link to={`/app/master/penempatan/${id}`} className="btn btn-danger">
                        <div className="d-flex align-items-center gap-2">
                            <ArrowLeft size={18} />
                            Kembali
                        </div>
                    </Link>
                </div>
            </div>
        </>
    );
}
