<script>
    import { onMount } from "svelte";
    import { afterUpdate } from "svelte";
    import clsx from "clsx";

    export let modal;
    export let handleGetRules;

    let rule = {
        disease: {
            name: "",
            description: "",
        },
        symptoms: [],
        confidence: 0,
        created_at: "",
    };

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

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

<div class="modal modal-blur fade" id="detail-rule-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 Relasi</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>{rule?.disease?.name || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Deskripsi</strong></td>
                                        <td>{rule?.disease?.description || "-"}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Gejala Klinis</strong></td>
                                        <td>
                                            {#if rule?.symptoms && rule.symptoms.length > 0}
                                                <div class="d-flex flex-wrap gap-1">
                                                    {#each rule.symptoms as symptom}
                                                        <span
                                                            class={clsx(
                                                                "badge",
                                                                symptom.category === "danger"
                                                                    ? "bg-danger-lt"
                                                                    : "bg-secondary-lt",
                                                            )}
                                                        >
                                                            {symptom?.name || ""}
                                                        </span>
                                                    {/each}
                                                </div>
                                            {:else}
                                                <span class="text-muted">-</span>
                                            {/if}
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Konfidensi</strong></td>
                                        <td>
                                            <span class="fw-bold text-primary">
                                                {rule?.confidence || 0}%
                                            </span>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Dibuat</strong></td>
                                        <td>
                                            {rule?.created_at ? window.moment(rule.created_at).format("LL") : "-"}
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
