<script>
    import api from "$lib/axios";
    import { onMount } from "svelte";
    import { afterUpdate } from "svelte";

    export let modal;
    export let handleGetPatients;

    let patient = {
        fullname: "",
        age: "",
        birth_date: "",
        gender: "",
        created_at: "",
        created_by: null
    };

    afterUpdate(() => {
        if (modal && modal.patient) {
            patient = { ...modal.patient };
        }
    });

    function age(date) {
        const today = new Date();
        const birth = new Date(date);

        let y = today.getFullYear() - birth.getFullYear();
        let m = today.getMonth() - birth.getMonth();

        if (m < 0) {
            y--;
            m += 12;
        }

        return { y, m };
    }

    onMount(() => {
        const bm = document.querySelector("#detail-patients-modal");
        if (bm) {
            bm.addEventListener("hide.bs.modal", () => {
                handleGetPatients();
            });
        }
    });
</script>

<div class="modal modal-blur fade" id="detail-patients-modal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Detail Pasien</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="card">
                    <div class="card-body p-4">
                        <div class="table-responsive">
                            <table class="table table-vcenter">
                                <tbody>
                                    <tr>
                                        <td><strong>Nama Lengkap</strong></td>
                                        <td>{patient?.fullname || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Usia</strong></td>
                                        <td>
                                            {#if patient?.birth_date}
                                                {@const { y, m } = age(patient.birth_date)}
                                                {y > 0 ? `${y} Tahun ${m} Bulan` : `${m} Bulan`}
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Tanggal Lahir</strong></td>
                                        <td>
                                            {patient?.birth_date ? window.moment(patient.birth_date).format("LL") : "-"}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Jenis Kelamin</strong></td>
                                        <td>
                                            {#if patient.gender == "male"}
                                                <span class="badge bg-primary-lt">Laki-Laki</span>
                                            {:else if patient.gender == "female"}
                                                <span class="badge bg-pink-lt">Perempuan</span>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Didaftarkan Oleh</strong></td>
                                        <td>{patient?.created_by?.username || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Tanggal Daftar</strong></td>
                                        <td>
                                            {patient.created_at ? window.moment(patient.created_at).format("LL") : "-"}
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>