<script>
    import PatientGrowth from "./PatientGrowth.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 { onMount } from "svelte";

    let patients = [];
    let currentUser = 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 handleGetPatients = async (page = 1, { withLoading = true } = {}) => {
        try {
            if (withLoading) {
                isLoading = true;
            }

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

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

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

<div class="card mt-3">
    <div class="card-header flex-wrap gap-2">
        <div class="">
            <form
                action=""
                method="post"
                on:submit|preventDefault={(e) => {
                    searchbox = e.target.searchbox.value;
                    handleGetPatients();
                }}
            >
                <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=""></div>
    </div>
    <div class="card-body p-0">
        {#if !isLoading}
            {#if patients.length > 0}
                <div class="table-responsive">
                    <table class="table table-selectable card-table table-vcenter text-nowrap">
                        <thead>
                            <tr>
                                <th scope="col" class="">No</th>
                                <th scope="col" class="">Nama Lengkap</th>
                                <th scope="col" class="">Umur</th>
                                <th scope="col" class="">Tanggal Lahir</th>
                                <th scope="col" class="">Jenis Kelamin</th>
                                <th scope="col" class="text-end">Didaftarkan Oleh</th>
                                <th scope="col" class="text-end">Tanggal Daftar</th>
                            </tr>
                        </thead>
                        <tbody>
                            {#each patients as user, i}
                                <tr>
                                    <th scope="row">{(pagination.current_page - 1) * pagination.limit + (i + 1)}</th>
                                    <td>{user?.fullname || ""}</td>
                                    <td>
                                        {user?.birth_date
                                            ? `${window.moment().diff(user.birth_date, "years")} Tahun`
                                            : ""}
                                    </td>
                                    <td class="">
                                        {user?.birth_date ? window.moment(user.birth_date).format("LL") : ""}
                                    </td>
                                    <td>
                                        {#if user.gender == "male"}
                                            <span class="badge bg-primary-lt">Laki-Laki</span>
                                        {:else}
                                            <span class="badge bg-pink-lt">Perempuan</span>
                                        {/if}
                                    </td>
                                    <td class="text-end">{user?.created_by?.username || ""}</td>
                                    <td class="text-end">
                                        {user.created_at ? window.moment(user.created_at).format("LL") : ""}
                                    </td>
                                </tr>
                            {/each}
                        </tbody>
                    </table>
                </div>
            {:else}
                <EmptyState height="250px" />
            {/if}
        {:else}
            <Loader height="250px" />
        {/if}
    </div>

    {#if patients.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;
                    handleGetPatients(e.detail.page);
                }}
            />
        </div>
    {/if}
</div>
