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

    let size = {
        patients: 0,
        symptoms: 0,
        diseases: 0,
        rules: 0,
    };

    const handleGetDecks = async (url, category) => {
        try {
            const response = await api.get(url);
            size[category] = response.data.pagination.data_size;
        } catch (err) {
            console.error(err);
        }
    };

    onMount(() => {
        handleGetDecks(`/patients?page=1&limit=1`, "patients");
        handleGetDecks(`/symptoms?page=1&limit=1`, "symptoms");
        handleGetDecks(`/diseases?page=1&limit=1`, "diseases");
        handleGetDecks(`/rules?page=1&limit=1`, "rules");
    });
</script>

<div class="col-12">
    <div class="row g-3">
        <div class="col-sm-6 col-lg-3">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-primary-lt text-white avatar">
                                <i class="ti ti-wheelchair"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">Total Pasien</div>
                            <div class="h2 mb-0">{size.patients || 0}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-purple-lt text-white avatar">
                                <i class="ti ti-ti ti-report-medical"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">Total Gejala Klinis</div>
                            <div class="h2 mb-0">{size.symptoms || 0}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-success-lt text-white avatar">
                                <i class="ti ti-vaccine"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">Total Penyakit</div>
                            <div class="h2 mb-0">{size.diseases || 0}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6 col-lg-3">
            <div class="card card-sm">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <span class="bg-pink-lt text-white avatar">
                                <i class="ti ti-vector-bezier-arc"></i>
                            </span>
                        </div>
                        <div class="col">
                            <div class="font-weight-medium">Total Relasi</div>
                            <div class="h2 mb-0">{size.rules || 0}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
