<script>
    import CreateSymptom from "./CreateSymptom.svelte";
    import Detail from "./Detail.svelte";
    import EmptyState from "$components/EmptyState.svelte";
    import Loader from "$components/Loader.svelte";
    import Pagination from "$components/Pagination.svelte";
    import api from "$lib/axios";
    import { Modal } from "$lib/modal";
    import { onMount } from "svelte";
    import { goto } from "$app/navigation";
    import clsx from "clsx";

    let createSymptomModal = new Modal("#create-symptom-modal");
    let detailSymptomModal = new Modal("#detail-symptom-modal");

    let symptoms = [];
    let currentPatient = null;
    let isLoading = false;
    let searchbox = "";
    let alert = {
        success: true,
        message: "",
    };
    let pagination = {
        current_page: 1,
        limit: 10,
        page_size: 0,
        data_size: 0,
        prev: 0,
        next: 0,
    };

    const handleGetSymptoms = async (page = 1, { withLoading = true } = {}) => {
        try {
            if (withLoading) {
                isLoading = true;
            }

            const response = await api.get(`/symptoms?page=${page}&limit=${pagination.limit}&search=${searchbox}`);

            pagination = response.data?.pagination;
            symptoms = response.data?.data;
        } catch (err) {
            alert.success = false;
            alert.message = err.message;
        } finally {
            isLoading = false;
        }
    };

    const handleShowDetail = (symptom) => {
        detailSymptomModal.symptom = symptom;
        detailSymptomModal.show();
    };

    onMount(() => {
        handleGetSymptoms();
    });
</script>

<CreateSymptom modal={createSymptomModal} {handleGetSymptoms} />
<Detail modal={detailSymptomModal} {handleGetSymptoms} />

<div class="card">
    <div class="card-header flex-wrap gap-2">
        <div class="">
            <form
                action=""
                method="post"
                on:submit|preventDefault={(e) => {
                    searchbox = e.target.searchbox.value;
                    handleGetSymptoms();
                }}
            >
                <div class="input-icon">
                    <div class="input-icon-addon"><i class="ti ti-search"></i></div>
                    <input
                        type="search"
                        name="searchbox"
                        id="searchbox"
                        style="width: 300px;"
                        class="form-control"
                        placeholder="Cari..."
                    />
                </div>
            </form>
        </div>
        <div class="flex-grow-1"></div>
        <div class="">
            <button type="button" class="btn btn-primary" on:click={() => createSymptomModal.show()}>
                <i class="ti ti-plus me-2"></i>
                Tambah Gejala
            </button>
        </div>
    </div>
    <div class="card-body p-0">
        {#if !isLoading}
            {#if symptoms.length > 0}
                <div class="table-responsive">
                    <table class="table table-hover table-selectable card-table table-vcenter text-nowrap">
                        <thead>
                            <tr>
                                <th scope="col" class=""></th>
                                <th scope="col" class="">Nama</th>
                                <th scope="col" class="">Deskripsi</th>
                                <th scope="col" class="">Kategori</th>
                                <th scope="col" class="">Tindak Lanjut</th>
                                <th scope="col" class="">Pertanyaan</th>
                                <th scope="col" class="text-end">Dibuat</th>
                                <th scope="col" class="text-end">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            {#each symptoms as symptom, i}
                                {@const categories = {
                                    danger: "danger",
                                    respiratory: "warning",
                                    digestive: "lime",
                                    general: "primary",
                                    skin: "purple",
                                    specific: "secondary",
                                }}
                                <tr>
                                    <th scope="row"><i class="ti ti-grip-vertical opacity-25"></i></th>
                                    <td>{symptom?.name || ""}</td>
                                    <td>
                                        <div class="text-truncate" style="max-width: 350px;">
                                            {symptom?.description || ""}
                                        </div>
                                    </td>
                                    <td>
                                        <span
                                            class={clsx(
                                                "badge",
                                                "text-capitalize",
                                                `bg-${categories[symptom.category]}-lt`,
                                            )}>{symptom.category}</span
                                        >
                                    </td>
                                    <td
                                        ><span class="badge bg-secondary-lt"
                                            >{symptom?.follow_up_questions?.length || "0"}</span
                                        ></td
                                    >
                                    <td>
                                        <div class="text-truncate text-primary" style="max-width: 350px;">
                                            Q: {symptom?.question || ""}
                                        </div>
                                    </td>
                                    <td class="text-end">
                                        {symptom.created_at ? window.moment(symptom.created_at).format("LL") : ""}
                                    </td>
                                    <td class="text-center">
                                        <button
                                            type="button"
                                            class="btn btn-sm btn-primary"
                                            on:click|stopPropagation={() => handleShowDetail(symptom)}
                                            title="Lihat detail gejala klinis"
                                            aria-label="Lihat detail gejala klinis"
                                        >
                                            <i class="ti ti-eye"></i>
                                        </button>
                                    </td>
                                </tr>
                            {/each}
                        </tbody>
                    </table>
                </div>
            {:else}
                <EmptyState height="250px" />
            {/if}
        {:else}
            <Loader height="250px" />
        {/if}
    </div>

    {#if symptoms.length > 0}
        <div class="card-footer border-0" style="background: var(--bs-body-bg);">
            <Pagination
                {pagination}
                on:update={(e) => {
                    pagination.current_page = e.detail.page;
                    handleGetSymptoms(e.detail.page);
                }}
            />
        </div>
    {/if}
</div>
