import { saveTabungan } from '@/api/api_tabungan';
import { alertError, alertSuccess } from '@/components/costum/alert';
import { ButtonSubmit } from '@/components/costum/button_submit';
import { SelectProdukTabungan } from '@/components/costum/SelectProdukTabungan';
import { useModalBrowseAnggota } from '@/components/modal/modalBrowseAnggota';
import { formatTanggal } from '@/helper';
import { useModalDetailAnggota } from '@/pages/master/anggota/modalDetailAnggota';
import { Anggota, ProdukTabungan, Tabungan } from '@/types';
import { useDetailTabungan, useProdukTabungan } from '@/useQuery/query';
import { usePage } from '@inertiajs/react';
import { ArrowLeft, Save } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Spinner } from 'react-bootstrap';
import { NumericFormat } from 'react-number-format';
import { Link, useNavigate, useParams } from 'react-router';

export default function DetailTabunganPage() {
    const { token } = usePage().props;
    const { id } = useParams();
    const [loadingData, setLoadingData] = useState(true);
    const navigate = useNavigate();

    const modalAnggota = useModalBrowseAnggota({
        onSelected: (anggota: Anggota) => {
            setDataTabungan({
                ...dataTabungan,
                anggota: anggota,
            });
        },
    });

    const modalDetailAnggota = useModalDetailAnggota({
        onSubmit: async () => {
            await detailTabungan.refetch();
            return true;
        },
    });

    const produkTabungan = useProdukTabungan();

    const [dataTabungan, setDataTabungan] = useState<Tabungan>({});

    const detailTabungan = useDetailTabungan(id);

    const saveDataTabungan = async () => {
        const response = await saveTabungan(dataTabungan!, token as string);
        const responseBody = await response.json();

        if (response.status == 201 || response.status == 200) {
            const rekening = responseBody.rekening;
            setDataTabungan({
                ...dataTabungan,
                rekening: rekening,
            });
            detailTabungan.refetch();
            await alertSuccess('Data berhasil disimpan');
            return;
        } else {
            alertError(responseBody.message);
        }
    };

    useEffect(() => {
        if (detailTabungan.data) {
            setDataTabungan(detailTabungan.data);
        }
        setLoadingData(false);
    }, [detailTabungan.data]);

    if (loadingData || detailTabungan.isLoading) {
        return (
            <div className="p-3 text-center">
                <Spinner />
            </div>
        );
    }

    console.log(dataTabungan);

    return (
        <>
            <div className="container" style={{ width: 1024 }}>
                <div className="card rounded-4 shadow-lg">
                    <div className="card-header py-3 bg-transparent">
                        <h5 className="mb-0">Data Tabungan</h5>
                    </div>
                    <div className="card-body">
                        <div className="row">
                            <div className="col-md-9">
                                <div className="row mb-2">
                                    <label className="col-form-label col-md-3">Rekening</label>
                                    <div className="col-md-5">
                                        <div className="input-group">
                                            <input className="form-control" placeholder="Rekening" readOnly value={dataTabungan.rekening ?? ''} />
                                        </div>
                                    </div>
                                </div>
                                <div className="row mb-2">
                                    <label className="col-form-label col-md-3">Anggota</label>
                                    <div className="col-md-5">
                                        <div className="input-group">
                                            <input className="form-control" placeholder="Anggota" readOnly value={dataTabungan.anggota?.id ?? ''} />
                                            <button className="btn btn-primary" onClick={() => modalAnggota.open()}>
                                                Browse
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div id="data-anggota">
                                    <div className="form-group row mb-2">
                                        <label className="col-form-label col-md-3">Nama Lengkap</label>
                                        <div className="col-md-9">
                                            <input
                                                className="form-control"
                                                placeholder="Nama Lengkap"
                                                readOnly
                                                value={dataTabungan.anggota?.nama_lengkap ?? ''}
                                            />
                                        </div>
                                    </div>
                                    <hr />
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3">Alamat</label>
                                        <div className="col-md-9">
                                            <input className="form-control" placeholder="Alamat" value={dataTabungan.anggota?.alamat ?? ''} />
                                        </div>
                                    </div>
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3" />
                                        <div className="col-md-2">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">RT</span>
                                                <input
                                                    className="form-control border-start-0 text-center"
                                                    placeholder="______"
                                                    value={dataTabungan.anggota?.rt ?? ''}
                                                />
                                            </div>
                                        </div>
                                        <div className="col-md-2">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">RW</span>
                                                <input
                                                    className="form-control border-start-0 text-center"
                                                    placeholder="______"
                                                    value={dataTabungan.anggota?.rw ?? ''}
                                                />
                                            </div>
                                        </div>
                                        <div className="col-md-3">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">Kode Pos</span>
                                                <input
                                                    className="form-control border-start-0 text-center"
                                                    placeholder="_________"
                                                    value={dataTabungan.anggota?.kode_pos ?? ''}
                                                />
                                            </div>
                                        </div>
                                    </div>
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3">Kelurahan</label>
                                        <div className="col-md-4">
                                            <input className="form-control" placeholder="Kelurahan" value={dataTabungan.anggota?.kelurahan ?? ''} />
                                        </div>
                                        <label className="col-form-label col-md-1">Kec.</label>
                                        <div className="col-md-4">
                                            <input className="form-control" placeholder="Kecamatan" value={dataTabungan.anggota?.kecamatan ?? ''} />
                                        </div>
                                    </div>
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3">Kota/Kabupaten</label>
                                        <div className="col-md-4">
                                            <input
                                                className="form-control"
                                                placeholder="Kota/Kabupaten"
                                                value={dataTabungan.anggota?.kabupaten ?? ''}
                                            />
                                        </div>
                                        <label className="col-form-label col-md-1">Telp.</label>
                                        <div className="col-md-4">
                                            <input
                                                className="form-control"
                                                placeholder="Nomor Telp/HP"
                                                value={dataTabungan.anggota?.nomor_telp ?? ''}
                                            />
                                        </div>
                                    </div>
                                    <hr />
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3">Produk</label>
                                        <div className="col-md-5">
                                            <SelectProdukTabungan
                                                value={dataTabungan.produk?.id?.toString() ?? ''}
                                                onChange={(value) => {
                                                    const produk = produkTabungan.data?.find((item: ProdukTabungan) => item.id?.toString() == value);
                                                    const data = { ...dataTabungan, produk: produk };
                                                    setDataTabungan(data);
                                                }}
                                            />
                                        </div>
                                        <label className="col-form-label col-md-2 text-end">Suku Bunga/Th</label>
                                        <div className="col-md-2">
                                            <div className="input-group">
                                                <input className="form-control border-end-0" readOnly value={dataTabungan.produk?.suku_bunga ?? ''} />
                                                <span className="input-group-text bg-white">%</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div className="row mb-2">
                                        <label className="col-form-label col-md-3">Saldo Minimal</label>
                                        <div className="col-md-3">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">Rp</span>
                                                <NumericFormat
                                                    className="form-control border-start-0 text-end"
                                                    readOnly
                                                    thousandSeparator="."
                                                    decimalSeparator=","
                                                    value={dataTabungan.produk?.saldo_mengendap ?? ''}
                                                />
                                            </div>
                                        </div>
                                        <label className="col-form-label col-md-3 text-end">Admin Bulanan</label>
                                        <div className="col-md-3">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">Rp</span>
                                                <NumericFormat
                                                    className="form-control border-start-0 text-end"
                                                    readOnly
                                                    thousandSeparator="."
                                                    decimalSeparator=","
                                                    value={dataTabungan.produk?.admin_bulanan ?? ''}
                                                />
                                            </div>
                                        </div>
                                    </div>
                                    <hr />
                                    <div className="form-group row mb-2">
                                        <label className="col-form-label col-md-3">Saldo</label>
                                        <div className="col-md-4">
                                            <div className="input-group">
                                                <span className="input-group-text bg-white border-end-0">Rp</span>
                                                <NumericFormat
                                                    className="form-control border-start-0 text-end"
                                                    readOnly
                                                    thousandSeparator="."
                                                    decimalSeparator=","
                                                    value={dataTabungan?.saldo ?? ''}
                                                />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div className="col-md-3">
                                <div className="row gap-2">
                                    <div className="col-md-12 gap-2 d-flex flex-column">
                                        <div className="d-flex">
                                            <button
                                                className="btn btn-primary flex-grow-1"
                                                disabled={!dataTabungan.rekening}
                                                onClick={() => modalDetailAnggota.open(dataTabungan.anggota?.id)}
                                            >
                                                Data Anggota
                                            </button>
                                        </div>
                                        <hr className="my-1" />
                                        {/* <div className="d-flex">
                                            <button
                                                className="btn btn-primary flex-grow-1"
                                                disabled={!dataTabungan.rekening}
                                                onClick={() => navigate(`/app/master/tabungan/${dataTabungan.rekening}/rekening-koran`)}
                                            >
                                                Rekening Koran
                                            </button>
                                        </div> */}
                                        <div className="d-flex">
                                            <button
                                                disabled={!dataTabungan.rekening}
                                                className="btn btn-primary flex-grow-1"
                                                onClick={() => navigate(`/app/master/tabungan/${dataTabungan.rekening}/statement-transaksi`)}
                                            >
                                                Statement Transaksi
                                            </button>
                                        </div>
                                        <div className="form-label mt-3">
                                            <label className='form-label'>Tanggal Buka</label>
                                            <input className="form-control text-center" readOnly value={formatTanggal(dataTabungan.created_at)} />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div className="card-footer py-3 justify-content-between d-flex bg-transparent">
                        <Link to="/app/master/tabungan" className="btn btn-danger align-items-center d-flex gap-2">
                            <ArrowLeft size={18} />
                            Kembali
                        </Link>
                        <ButtonSubmit icon={<Save size={18} />} className="btn btn-success" onClick={async () => await saveDataTabungan()}>
                            Simpan
                        </ButtonSubmit>
                    </div>
                </div>
            </div>

            {modalAnggota.modal}
            {modalDetailAnggota.modal}
        </>
    );
}
