<script setup lang="ts">
import { Head } from '@inertiajs/vue3';
import {
    Banknote,
    Clock,
    CreditCard,
    DollarSign,
    FileText,
    Moon,
    Package,
    ShoppingCart,
    Sun,
    Sunrise,
    TrendingDown,
    TrendingUp,
    Users,
} from '@lucide/vue';
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { ChartContainer, ChartLegendContent } from '@/components/ui/chart';
import { VisAxis, VisGroupedBar, VisXYContainer, VisDonut } from '@unovis/vue';
import type { ChartConfig } from '@/components/ui/chart';
import AdminLayout from '@/layouts/AdminLayout.vue';

interface DashboardProps {
    rawMaterialStock: {
        total_stock: number;
        total_value: number;
    };
    productStock: {
        total_stock: number;
        total_reject: number;
        total_value: number;
    };
    topSuppliers: Array<{
        name: string;
        total_amount: number;
        purchase_count: number;
    }>;
    topCustomers: Array<{
        name: string;
        total_amount: number;
        order_count: number;
    }>;
    purchaseSummary: {
        total_purchases: number;
        total_spent: number;
        total_discount: number;
    };
    cuttingSummary: {
        total_cost: number;
        total_cutting: number;
        total_warehouse: number;
        total_reject: number;
    };
    orderStats: {
        by_channel: Array<{
            channel: string;
            label: string;
            count: number;
            total: number;
        }>;
        by_payment_type: Array<{
            payment_type: string;
            label: string;
            count: number;
            total: number;
        }>;
        by_marketing: Array<{
            name: string;
            count: number;
            total: number;
        }>;
        by_status: Array<{
            status: string;
            label: string;
            count: number;
        }>;
    };
    revenueSummary: {
        total_revenue: number;
        total_discount: number;
        total_shipping: number;
        total_orders: number;
    };
    cashAccounts: Array<{
        name: string;
        balance: number;
    }>;
    kasbonSummary: {
        pending: { count: number; total: number };
        approved: { count: number; total: number };
        paid: { count: number; total: number };
        total: number;
    };
    payrollSummary: {
        has_period: boolean;
        period_status?: string;
        total_employees: number;
        total_amount: number;
        paid_amount: number;
        unpaid_amount: number;
        paid_count: number;
        unpaid_count: number;
    };
    leaveRequestSummary: {
        total: number;
        pending: number;
        approved: number;
        rejected: number;
    };
}

const props = defineProps<DashboardProps>();

const currentTime = ref(new Date());
let timer: ReturnType<typeof setInterval>;

onMounted(() => {
    timer = setInterval(() => {
        currentTime.value = new Date();
    }, 1000);
});

onUnmounted(() => {
    clearInterval(timer);
});

const timeStr = computed(() => {
    return currentTime.value.toLocaleTimeString('id-ID', {
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false,
    });
});

const dateStr = computed(() => {
    return currentTime.value.toLocaleDateString('id-ID', {
        weekday: 'long',
        year: 'numeric',
        month: 'long',
        day: 'numeric',
    });
});

const greeting = computed(() => {
    const hour = currentTime.value.getHours();

    if (hour >= 4 && hour < 11) {
        return { text: 'Selamat Pagi', icon: Sunrise };
    }

    if (hour >= 11 && hour < 15) {
        return { text: 'Selamat Siang', icon: Sun };
    }

    if (hour >= 15 && hour < 18) {
        return { text: 'Selamat Sore', icon: Sun };
    }

    return { text: 'Selamat Malam', icon: Moon };
});

function formatRupiah(value: number): string {
    return 'Rp ' + value.toLocaleString('id-ID');
}

// Chart configs
const supplierChartConfig = {
    amount: {
        label: 'Total Pembelian',
        color: 'var(--chart-1)',
    },
} satisfies ChartConfig;

const customerChartConfig = {
    amount: {
        label: 'Total Pesanan',
        color: 'var(--chart-2)',
    },
} satisfies ChartConfig;

const channelColors = ['var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)'];
const channelChartConfig = computed(() => {
    const config: ChartConfig = {};
    props.orderStats.by_channel.forEach((item, index) => {
        config[item.channel] = {
            label: item.label,
            color: channelColors[index % channelColors.length],
        };
    });
    return config;
});

const paymentColors = ['var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)'];
const paymentChartConfig = computed(() => {
    const config: ChartConfig = {};
    props.orderStats.by_payment_type.forEach((item, index) => {
        config[item.payment_type] = {
            label: item.label,
            color: paymentColors[index % paymentColors.length],
        };
    });
    return config;
});

const statusColors = ['#facc15', 'var(--chart-1)', 'var(--chart-2)', 'var(--chart-4)'];
const statusChartConfig = computed(() => {
    const config: ChartConfig = {};
    props.orderStats.by_status.forEach((item, index) => {
        config[item.status] = {
            label: item.label,
            color: statusColors[index % statusColors.length],
        };
    });
    return config;
});

const marketingColors = ['var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)', 'var(--chart-5)'];
const marketingChartConfig = computed(() => {
    const config: ChartConfig = {};
    props.orderStats.by_marketing.forEach((item, index) => {
        config[`mkt_${index}`] = {
            label: item.name,
            color: marketingColors[index % marketingColors.length],
        };
    });
    return config;
});

const marketingBarData = computed(() => {
    return props.orderStats.by_marketing.map((item, index) => ({
        name: item.name,
        total: item.total,
        count: item.count,
    }));
});

type SupplierData = { name: string; amount: number };
type CustomerData = { name: string; amount: number };

const supplierBarData = computed<SupplierData[]>(() => {
    return props.topSuppliers.map((s) => ({
        name: s.name.length > 15 ? s.name.substring(0, 15) + '...' : s.name,
        amount: s.total_amount,
    }));
});

const customerBarData = computed<CustomerData[]>(() => {
    return props.topCustomers.map((c) => ({
        name: c.name.length > 15 ? c.name.substring(0, 15) + '...' : c.name,
        amount: c.total_amount,
    }));
});

const totalCashBalance = computed(() => {
    return props.cashAccounts.reduce((sum, acc) => sum + acc.balance, 0);
});
</script>

<template>
    <Head title="Dashboard" />

    <AdminLayout>
        <div class="flex flex-1 flex-col gap-6">
            <!-- Welcome Card -->
            <Card class="relative overflow-hidden">
                <div class="from-primary/5 to-background absolute inset-0 bg-linear-to-br" />
                <CardHeader class="relative">
                    <div class="flex items-center gap-3">
                        <div class="bg-primary/10 flex size-12 items-center justify-center rounded-full">
                            <component :is="greeting.icon" class="text-primary size-6" />
                        </div>
                        <div>
                            <CardTitle class="text-2xl font-bold">
                                {{ greeting.text }}, {{ $page.props.auth.user?.username }}!
                            </CardTitle>
                            <CardDescription class="mt-1">
                                Selamat datang di dasbor aplikasi.
                            </CardDescription>
                        </div>
                    </div>
                </CardHeader>
                <CardContent class="relative">
                    <div class="text-muted-foreground flex items-center gap-2 text-sm">
                        <span>{{ dateStr }}</span>
                        <span>-</span>
                        <span class="flex items-center gap-1">
                            <Clock class="size-3.5" />
                            {{ timeStr }}
                        </span>
                    </div>
                </CardContent>
            </Card>

            <!-- Row 1: Stock Summary -->
            <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
                <!-- 1. Raw Material Stock -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Stok Bahan Baku</CardTitle>
                        <Package class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="text-2xl font-bold">
                            {{ rawMaterialStock.total_stock.toLocaleString('id-ID') }}
                        </div>
                        <p class="text-muted-foreground text-xs">
                            Total nilai: {{ formatRupiah(rawMaterialStock.total_value) }}
                        </p>
                    </CardContent>
                </Card>

                <!-- 2. Product Stock -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Stok Produk</CardTitle>
                        <ShoppingCart class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="text-2xl font-bold">
                            {{ productStock.total_stock.toLocaleString('id-ID') }}
                        </div>
                        <p class="text-muted-foreground text-xs">
                            Total nilai: {{ formatRupiah(productStock.total_value) }}
                            <span v-if="productStock.total_reject > 0" class="ml-1 text-red-500">
                                ({{ productStock.total_reject }} reject)
                            </span>
                        </p>
                    </CardContent>
                </Card>

                <!-- 5. Purchase Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Total Pembelian</CardTitle>
                        <TrendingDown class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="text-2xl font-bold">
                            {{ formatRupiah(purchaseSummary.total_spent) }}
                        </div>
                        <p class="text-muted-foreground text-xs">
                            {{ purchaseSummary.total_purchases }} transaksi pembelian
                        </p>
                    </CardContent>
                </Card>

                <!-- 8. Revenue Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Total Pendapatan</CardTitle>
                        <TrendingUp class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="text-2xl font-bold">
                            {{ formatRupiah(revenueSummary.total_revenue) }}
                        </div>
                        <p class="text-muted-foreground text-xs">
                            {{ revenueSummary.total_orders }} pesanan selesai
                            <span v-if="revenueSummary.total_discount > 0" class="ml-1 text-orange-500">
                                (potongan: {{ formatRupiah(revenueSummary.total_discount) }})
                            </span>
                        </p>
                    </CardContent>
                </Card>
            </div>

            <!-- Row 2: Top 5 Charts -->
            <div class="grid gap-4 md:grid-cols-2">
                <!-- 3. Top 5 Suppliers Chart -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Top 5 Supplier</CardTitle>
                        <CardDescription>Berdasarkan total harga pembelian</CardDescription>
                    </CardHeader>
                    <CardContent>
                        <ChartContainer
                            v-if="topSuppliers.length > 0"
                            :config="supplierChartConfig"
                            class="min-h-[250px] w-full"
                        >
                            <VisXYContainer :data="supplierBarData">
                                <VisGroupedBar
                                    :x="(d: SupplierData) => d.name"
                                    :y="(d: SupplierData) => d.amount"
                                    :color="supplierChartConfig.amount.color"
                                    :rounded-corners="4"
                                    bar-padding="0.1"
                                />
                                <VisAxis
                                    type="x"
                                    :x="(d: SupplierData) => d.name"
                                    :tick-line="false"
                                    :domain-line="false"
                                    :grid-line="false"
                                />
                                <VisAxis
                                    type="y"
                                    :tick-format="(d: number) => ''"
                                    :tick-line="false"
                                    :domain-line="false"
                                    :grid-line="true"
                                />
                            </VisXYContainer>
                        </ChartContainer>
                        <div v-else class="text-muted-foreground flex h-[250px] items-center justify-center">
                            Belum ada data supplier
                        </div>
                    </CardContent>
                </Card>

                <!-- 4. Top 5 Customers Chart -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Top 5 Pelanggan</CardTitle>
                        <CardDescription>Berdasarkan total nilai pesanan</CardDescription>
                    </CardHeader>
                    <CardContent>
                        <ChartContainer
                            v-if="topCustomers.length > 0"
                            :config="customerChartConfig"
                            class="min-h-[250px] w-full"
                        >
                            <VisXYContainer :data="customerBarData">
                                <VisGroupedBar
                                    :x="(d: CustomerData) => d.name"
                                    :y="(d: CustomerData) => d.amount"
                                    :color="customerChartConfig.amount.color"
                                    :rounded-corners="4"
                                    bar-padding="0.1"
                                />
                                <VisAxis
                                    type="x"
                                    :x="(d: CustomerData) => d.name"
                                    :tick-line="false"
                                    :domain-line="false"
                                    :grid-line="false"
                                />
                                <VisAxis
                                    type="y"
                                    :tick-format="(d: number) => ''"
                                    :tick-line="false"
                                    :domain-line="false"
                                    :grid-line="true"
                                />
                            </VisXYContainer>
                        </ChartContainer>
                        <div v-else class="text-muted-foreground flex h-[250px] items-center justify-center">
                            Belum ada data pelanggan
                        </div>
                    </CardContent>
                </Card>
            </div>

            <!-- Row 3: Order Stats Charts -->
            <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
                <!-- 7a. Order by Channel -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Pesanan per Channel</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div v-if="orderStats.by_channel.length > 0">
                            <ChartContainer :config="channelChartConfig" class="min-h-[200px] w-full">
                                <VisXYContainer :data="orderStats.by_channel">
                                    <VisDonut
                                        :value="(d: typeof orderStats.by_channel[number]) => d.count"
                                        :color="orderStats.by_channel.map((_, i) => channelColors[i % channelColors.length])"
                                    />
                                </VisXYContainer>
                            </ChartContainer>
                            <div class="mt-3 flex flex-wrap justify-center gap-3">
                                <div
                                    v-for="(item, index) in orderStats.by_channel"
                                    :key="item.channel"
                                    class="flex items-center gap-1.5"
                                >
                                    <span
                                        class="size-2.5 rounded-full"
                                        :style="{ backgroundColor: channelColors[index % channelColors.length] }"
                                    />
                                    <span class="text-muted-foreground text-xs">{{ item.label }}</span>
                                    <span class="text-xs font-medium">{{ item.count }}</span>
                                </div>
                            </div>
                        </div>
                        <div v-else class="text-muted-foreground flex h-[200px] items-center justify-center">
                            Belum ada data
                        </div>
                    </CardContent>
                </Card>

                <!-- 7b. Order by Payment Type -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Pesanan per Pembayaran</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div v-if="orderStats.by_payment_type.length > 0">
                            <ChartContainer :config="paymentChartConfig" class="min-h-[200px] w-full">
                                <VisXYContainer :data="orderStats.by_payment_type">
                                    <VisDonut
                                        :value="(d: typeof orderStats.by_payment_type[number]) => d.count"
                                        :color="orderStats.by_payment_type.map((_, i) => paymentColors[i % paymentColors.length])"
                                    />
                                </VisXYContainer>
                            </ChartContainer>
                            <div class="mt-3 flex flex-wrap justify-center gap-3">
                                <div
                                    v-for="(item, index) in orderStats.by_payment_type"
                                    :key="item.payment_type"
                                    class="flex items-center gap-1.5"
                                >
                                    <span
                                        class="size-2.5 rounded-full"
                                        :style="{ backgroundColor: paymentColors[index % paymentColors.length] }"
                                    />
                                    <span class="text-muted-foreground text-xs">{{ item.label }}</span>
                                    <span class="text-xs font-medium">{{ item.count }}</span>
                                </div>
                            </div>
                        </div>
                        <div v-else class="text-muted-foreground flex h-[200px] items-center justify-center">
                            Belum ada data
                        </div>
                    </CardContent>
                </Card>

                <!-- 7c. Order by Marketing -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Pesanan per Marketing</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div v-if="orderStats.by_marketing.length > 0">
                            <ChartContainer :config="marketingChartConfig" class="min-h-[200px] w-full">
                                <VisXYContainer :data="orderStats.by_marketing">
                                    <VisDonut
                                        :value="(d: typeof orderStats.by_marketing[number]) => d.count"
                                        :color="orderStats.by_marketing.map((_, i) => marketingColors[i % marketingColors.length])"
                                    />
                                </VisXYContainer>
                            </ChartContainer>
                            <div class="mt-3 flex flex-wrap justify-center gap-3">
                                <div
                                    v-for="(item, index) in orderStats.by_marketing"
                                    :key="item.name"
                                    class="flex items-center gap-1.5"
                                >
                                    <span
                                        class="size-2.5 rounded-full"
                                        :style="{ backgroundColor: marketingColors[index % marketingColors.length] }"
                                    />
                                    <span class="text-muted-foreground text-xs">{{ item.name }}</span>
                                    <span class="text-xs font-medium">{{ item.count }}</span>
                                </div>
                            </div>
                        </div>
                        <div v-else class="text-muted-foreground flex h-[200px] items-center justify-center">
                            Belum ada data
                        </div>
                    </CardContent>
                </Card>

                <!-- 7d. Order by Status -->
                <Card>
                    <CardHeader>
                        <CardTitle class="text-base">Pesanan per Status</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <div v-if="orderStats.by_status.length > 0">
                            <ChartContainer :config="statusChartConfig" class="min-h-[200px] w-full">
                                <VisXYContainer :data="orderStats.by_status">
                                    <VisDonut
                                        :value="(d: typeof orderStats.by_status[number]) => d.count"
                                        :color="orderStats.by_status.map((_, i) => statusColors[i % statusColors.length])"
                                    />
                                </VisXYContainer>
                            </ChartContainer>
                            <div class="mt-3 flex flex-wrap justify-center gap-3">
                                <div
                                    v-for="(item, index) in orderStats.by_status"
                                    :key="item.status"
                                    class="flex items-center gap-1.5"
                                >
                                    <span
                                        class="size-2.5 rounded-full"
                                        :style="{ backgroundColor: statusColors[index % statusColors.length] }"
                                    />
                                    <span class="text-muted-foreground text-xs">{{ item.label }}</span>
                                    <span class="text-xs font-medium">{{ item.count }}</span>
                                </div>
                            </div>
                        </div>
                        <div v-else class="text-muted-foreground flex h-[200px] items-center justify-center">
                            Belum ada data
                        </div>
                    </CardContent>
                </Card>
            </div>

            <!-- Row 4: Cutting & Finance -->
            <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
                <!-- 6. Cutting Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Biaya Produksi & Hasil Cutting</CardTitle>
                        <FileText class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Biaya Produksi</p>
                                <p class="text-xl font-bold">{{ formatRupiah(cuttingSummary.total_cost) }}</p>
                            </div>
                            <div class="grid grid-cols-3 gap-2 pt-2 border-t">
                                <div>
                                    <p class="text-muted-foreground text-xs">Hasil Cutting</p>
                                    <p class="text-lg font-semibold">{{ cuttingSummary.total_cutting.toLocaleString('id-ID') }}</p>
                                </div>
                                <div>
                                    <p class="text-muted-foreground text-xs">Masuk Gudang</p>
                                    <p class="text-lg font-semibold">{{ cuttingSummary.total_warehouse.toLocaleString('id-ID') }}</p>
                                </div>
                                <div>
                                    <p class="text-muted-foreground text-xs">Reject</p>
                                    <p class="text-lg font-semibold text-red-500">{{ cuttingSummary.total_reject.toLocaleString('id-ID') }}</p>
                                </div>
                            </div>
                        </div>
                    </CardContent>
                </Card>

                <!-- 8b. Revenue Detail -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Detail Pendapatan</CardTitle>
                        <DollarSign class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Pendapatan</p>
                                <p class="text-xl font-bold text-green-600">{{ formatRupiah(revenueSummary.total_revenue) }}</p>
                            </div>
                            <div class="grid grid-cols-2 gap-2 pt-2 border-t">
                                <div>
                                    <p class="text-muted-foreground text-xs">Total Potongan</p>
                                    <p class="text-lg font-semibold text-orange-500">{{ formatRupiah(revenueSummary.total_discount) }}</p>
                                </div>
                                <div>
                                    <p class="text-muted-foreground text-xs">Total Ongkir</p>
                                    <p class="text-lg font-semibold">{{ formatRupiah(revenueSummary.total_shipping) }}</p>
                                </div>
                            </div>
                        </div>
                    </CardContent>
                </Card>

                <!-- 9. Cash Accounts -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Kas Toko</CardTitle>
                        <Banknote class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Saldo</p>
                                <p class="text-xl font-bold">{{ formatRupiah(totalCashBalance) }}</p>
                            </div>
                            <div class="space-y-2 pt-2 border-t">
                                <div
                                    v-for="account in cashAccounts"
                                    :key="account.name"
                                    class="flex items-center justify-between"
                                >
                                    <span class="text-muted-foreground text-sm">{{ account.name }}</span>
                                    <span class="text-sm font-medium">{{ formatRupiah(account.balance) }}</span>
                                </div>
                                <div v-if="cashAccounts.length === 0" class="text-muted-foreground text-sm">
                                    Belum ada akun kas
                                </div>
                            </div>
                        </div>
                    </CardContent>
                </Card>
            </div>

            <!-- Row 5: HR Summary -->
            <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
                <!-- 10. Kasbon Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Kasbon Karyawan</CardTitle>
                        <CreditCard class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Keseluruhan</p>
                                <p class="text-xl font-bold">{{ formatRupiah(kasbonSummary.total) }}</p>
                            </div>
                            <div class="grid grid-cols-3 gap-2 pt-2 border-t">
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-yellow-500/10 text-yellow-600 border-yellow-500/20">
                                        Menunggu
                                    </Badge>
                                    <p class="text-sm font-semibold">{{ kasbonSummary.pending.count }}</p>
                                    <p class="text-muted-foreground text-xs">{{ formatRupiah(kasbonSummary.pending.total) }}</p>
                                </div>
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-blue-500/10 text-blue-600 border-blue-500/20">
                                        Disetujui
                                    </Badge>
                                    <p class="text-sm font-semibold">{{ kasbonSummary.approved.count }}</p>
                                    <p class="text-muted-foreground text-xs">{{ formatRupiah(kasbonSummary.approved.total) }}</p>
                                </div>
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-green-500/10 text-green-600 border-green-500/20">
                                        Dibayar
                                    </Badge>
                                    <p class="text-sm font-semibold">{{ kasbonSummary.paid.count }}</p>
                                    <p class="text-muted-foreground text-xs">{{ formatRupiah(kasbonSummary.paid.total) }}</p>
                                </div>
                            </div>
                        </div>
                    </CardContent>
                </Card>

                <!-- 11. Payroll Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Penggajian Bulan Ini</CardTitle>
                        <Users class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div v-if="payrollSummary.has_period" class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Penggajian</p>
                                <p class="text-xl font-bold">{{ formatRupiah(payrollSummary.total_amount) }}</p>
                                <p class="text-muted-foreground text-xs">{{ payrollSummary.total_employees }} karyawan</p>
                            </div>
                            <div class="grid grid-cols-2 gap-2 pt-2 border-t">
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-green-500/10 text-green-600 border-green-500/20">
                                        Sudah Dibayar
                                    </Badge>
                                    <p class="text-sm font-semibold">{{ payrollSummary.paid_count }}</p>
                                    <p class="text-muted-foreground text-xs">{{ formatRupiah(payrollSummary.paid_amount) }}</p>
                                </div>
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-red-500/10 text-red-600 border-red-500/20">
                                        Belum Dibayar
                                    </Badge>
                                    <p class="text-sm font-semibold">{{ payrollSummary.unpaid_count }}</p>
                                    <p class="text-muted-foreground text-xs">{{ formatRupiah(payrollSummary.unpaid_amount) }}</p>
                                </div>
                            </div>
                        </div>
                        <div v-else class="text-muted-foreground flex h-[120px] items-center justify-center">
                            Belum ada periode payroll bulan ini
                        </div>
                    </CardContent>
                </Card>

                <!-- 12. Leave Request Summary -->
                <Card>
                    <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
                        <CardTitle class="text-sm font-medium">Pengajuan Cuti Bulan Ini</CardTitle>
                        <FileText class="text-muted-foreground size-4" />
                    </CardHeader>
                    <CardContent>
                        <div class="space-y-3">
                            <div>
                                <p class="text-muted-foreground text-xs">Total Pengajuan</p>
                                <p class="text-xl font-bold">{{ leaveRequestSummary.total }}</p>
                            </div>
                            <div class="grid grid-cols-3 gap-2 pt-2 border-t">
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-yellow-500/10 text-yellow-600 border-yellow-500/20">
                                        Menunggu
                                    </Badge>
                                    <p class="text-lg font-semibold">{{ leaveRequestSummary.pending }}</p>
                                </div>
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-green-500/10 text-green-600 border-green-500/20">
                                        Disetujui
                                    </Badge>
                                    <p class="text-lg font-semibold">{{ leaveRequestSummary.approved }}</p>
                                </div>
                                <div>
                                    <Badge variant="outline" class="mb-1 bg-red-500/10 text-red-600 border-red-500/20">
                                        Ditolak
                                    </Badge>
                                    <p class="text-lg font-semibold">{{ leaveRequestSummary.rejected }}</p>
                                </div>
                            </div>
                        </div>
                    </CardContent>
                </Card>
            </div>
        </div>
    </AdminLayout>
</template>
