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

    export let modal;
    export let handleGetSymptoms;

    let symptom = {
        name: "",
        description: "",
        category: "",
        question: "",
        follow_up_questions: [],
        created_at: "",
    };

    const categories = {
        danger: { class: "danger", label: "Bahaya" },
        respiratory: { class: "warning", label: "Pernapasan" },
        digestive: { class: "info", label: "Pencernaan" },
        general: { class: "primary", label: "Umum" },
        specific: { class: "secondary", label: "Spesifik" },
    };

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

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

<div class="modal modal-blur fade" id="detail-symptom-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 Gejala</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>{symptom?.name || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Deskripsi</strong></td>
                                        <td>
                                            {#if symptom?.description}
                                                <div class="text-wrap">
                                                    {symptom.description}
                                                </div>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Kategori</strong></td>
                                        <td>
                                            {#if symptom?.category && categories[symptom.category]}
                                                <span
                                                    class={clsx(
                                                        "badge text-capitalize",
                                                        `bg-${categories[symptom.category].class}-lt`,
                                                    )}
                                                >
                                                    {categories[symptom.category].label}
                                                </span>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Tindak Lanjut</strong></td>
                                        <td>
                                            <span class="badge bg-secondary-lt">
                                                {symptom?.follow_up_questions?.length || 0} Pertanyaan
                                            </span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Pertanyaan</strong></td>
                                        <td>
                                            {#if symptom?.question}
                                                <div class="text-wrap text-primary">
                                                    Q: {symptom.question}
                                                </div>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    {#if symptom?.follow_up_questions && symptom.follow_up_questions.length > 0}
                                        <tr>
                                            <td><strong>Pertanyaan Lanjutan</strong></td>
                                            <td>
                                                <div class="list-group list-group-flush">
                                                    {#each symptom.follow_up_questions as followUp, index}
                                                        <div class="list-group-item px-0 py-2">
                                                            <small class="text-muted">#{index + 1}</small>
                                                            <div class="text-wrap">{followUp.question || followUp}</div>
                                                        </div>
                                                    {/each}
                                                </div>
                                            </td>
                                        </tr>
                                    {/if}
                                    <tr>
                                        <td><strong>Dibuat</strong></td>
                                        <td>
                                            {symptom.created_at ? window.moment(symptom.created_at).format("LL") : "-"}
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
