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

    export let modal;
    export let handleGetDiseases;

    let disease = {
        name: "",
        description: "",
        severity: "",
        treatment: "",
        created_at: "",
    };

    const severity = {
        severe: ["danger", "Berat"],
        moderate: ["warning", "Sedang"],
        mild: ["primary", "Ringan"],
    };

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

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

<div class="modal modal-blur fade" id="detail-diseases-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 Penyakit</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</strong></td>
                                        <td>{disease.name || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Deskripsi</strong></td>
                                        <td>{disease.description || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Tingkat Keparahan</strong></td>
                                        <td>
                                            {#if disease.severity && severity[disease.severity]}
                                                <span
                                                    class={clsx(
                                                        "badge",
                                                        "text-capitalize",
                                                        `bg-${severity[disease.severity][0]}-lt`,
                                                    )}
                                                >
                                                    {severity[disease.severity][1]}
                                                </span>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Rekomendasi Penanganan</strong></td>
                                        <td>{disease.treatment || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Dibuat</strong></td>
                                        <td>
                                            {disease.created_at ? window.moment(disease.created_at).format("LL") : "-"}
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
