<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once __DIR__.'/../includes/db.php';
require_once __DIR__.'/../includes/auth.php';
require_once __DIR__.'/../includes/kependudukan_helpers.php';
include __DIR__.'/../includes/header.php';

if (!function_exists('countPendingSafe')) {
    function countPendingSafe(mysqli $mysqli, string $table): int
    {
        if (!preg_match('/^[A-Za-z0-9_]+$/', $table) || !mambu_table_exists($mysqli, $table)) return 0;
        $q = $mysqli->query("SELECT COUNT(*) c FROM `$table` WHERE status='pending'");
        return $q ? (int)($q->fetch_assoc()['c'] ?? 0) : 0;
    }
}

$cards = [
    ['Surat Keterangan Usaha', 'Usaha warga dan pelaku ekonomi desa', 'ri-store-2-fill', 'pengajuan_surat_usaha', 'pengajuan_list_usaha.php', 'usaha'],
    ['Surat Penghasilan Orang Tua', 'Keterangan penghasilan orang tua/wali', 'ri-money-dollar-circle-fill', 'pengajuan_penghasilan_ortu', 'pengajuan_list.php', 'penghasilan_ortu'],
    ['Surat Tidak Mampu', 'Permohonan keterangan tidak mampu', 'ri-hand-coin-fill', 'pengajuan_surat_tidak_mampu', 'pengajuan_list_tidak_mampu.php', 'tidak_mampu'],
    ['Surat Domisili', 'Keterangan domisili penduduk', 'ri-map-pin-2-fill', 'pengajuan_surat_domisili', 'pengajuan_list_domisili.php', 'domisili'],
    ['Surat Keterangan Hilang', 'Laporan kehilangan dokumen/barang', 'ri-error-warning-fill', 'pengajuan_surat_hilang', 'pengajuan_list_hilang.php', 'hilang'],
    ['Surat Keterangan Pindah', 'Permohonan pindah penduduk', 'ri-truck-fill', 'pengajuan_surat_pindah', 'pengajuan_list_pindah.php', 'pindah'],
    ['Surat Izin Keramaian', 'Permohonan izin kegiatan/keramaian', 'ri-megaphone-fill', 'pengajuan_surat_ijin_keramaian', 'pengajuan_list_ijin_keramaian.php', 'ijin'],
    ['Surat Merantau', 'Keterangan perjalanan/merantau', 'ri-suitcase-3-fill', 'pengajuan_surat_merantau', 'pengajuan_list_merantau.php', 'merantau'],
    ['Surat Kuasa', 'Administrasi surat kuasa warga', 'ri-file-shield-2-fill', 'pengajuan_surat_kuasa', 'pengajuan_list_kuasa.php', 'kuasa'],
    ['Surat Pengantar Kayu', 'Pengantar pengangkutan kayu', 'ri-tree-fill', 'pengajuan_surat_pengantar_kayu', 'pengajuan_list_pengantar_kayu.php', 'kayu'],
];

$totalPending = 0;
foreach ($cards as $i => $card) {
    $cards[$i]['pending'] = countPendingSafe($mysqli, $card[3]);
    $totalPending += $cards[$i]['pending'];
}
?>

<section class="page-hero">
    <div class="page-title">
        <span class="page-title-icon"><i class="ri-inbox-archive-fill"></i></span>
        <div>
            <h1>Pengajuan Surat Warga</h1>
            <p>Monitoring permohonan surat masuk dari warga Desa Mambu berdasarkan jenis layanan.</p>
        </div>
    </div>
    <div class="page-actions">
        <a class="action-btn secondary" href="pengajuan_surat_menu.php"><i class="ri-file-list-3-line"></i> Template Surat</a>
    </div>
</section>

<div class="stats-grid compact">
    <article class="stat-card tone-blue"><span class="stat-icon"><i class="ri-file-list-3-fill"></i></span><div><div class="stat-label">Jenis Layanan</div><div class="stat-value"><?= count($cards) ?></div><div class="stat-sub">kategori surat</div></div></article>
    <article class="stat-card tone-orange"><span class="stat-icon"><i class="ri-time-fill"></i></span><div><div class="stat-label">Total Pending</div><div class="stat-value"><?= mambu_fmt($totalPending) ?></div><div class="stat-sub">menunggu verifikasi</div></div></article>
    <article class="stat-card tone-green"><span class="stat-icon"><i class="ri-shield-check-fill"></i></span><div><div class="stat-label">Status Sistem</div><div class="stat-value">Aktif</div><div class="stat-sub">layanan warga online</div></div></article>
</div>

<div class="panel-grid service-grid">
    <?php foreach($cards as $card): ?>
        <article class="info-card service-card">
            <div class="info-card-body">
                <div class="service-icon"><i class="<?= esc($card[2]) ?>"></i></div>
                <div class="service-card-title">
                    <h3><?= esc($card[0]) ?></h3>
                    <p><?= esc($card[1]) ?></p>
                </div>
                <div class="pending-wrap">
                    <span>Pending</span>
                    <span class="status-pill <?= $card['pending'] ? 'yellow' : 'green' ?>"><?= mambu_fmt($card['pending']) ?></span>
                </div>
                <a class="action-btn primary" href="<?= esc($card[4]) ?>"><i class="ri-arrow-right-line"></i> Lihat Data</a>
            </div>
        </article>
    <?php endforeach; ?>
</div>

<?php include __DIR__.'/../includes/footer.php'; ?>
