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

$surat = [
    ['SURAT KETERANGAN PENGHASILAN ORANG TUA','ri-money-dollar-circle-fill','Penghasilan orang tua','penghasilan_ortu'],
    ['SURAT KETERANGAN USAHA','ri-store-2-fill','Usaha warga','usaha'],
    ['SURAT KETERANGAN TIDAK MAMPU','ri-hand-coin-fill','Bantuan sosial','tidak_mampu'],
    ['SURAT KETERANGAN GADAI','ri-exchange-dollar-fill','Gadai barang','gadai'],
    ['SURAT KETERANGAN DOMISILI','ri-map-pin-2-fill','Domisili penduduk','domisili'],
    ['SURAT KETERANGAN PINDAH','ri-truck-fill','Pindah penduduk','pindah'],
    ['SURAT BELUM MENIKAH','ri-heart-2-fill','Status perkawinan','belum_menikah'],
    ['SURAT BELUM BEKERJA','ri-briefcase-4-fill','Status pekerjaan','belum_bekerja'],
    ['SURAT PENGANTAR HEWAN','ri-bear-smile-fill','Pengantar hewan','pengantar_hewan'],
    ['SURAT PENGANTAR KAYU','ri-tree-fill','Pengantar kayu','pengantar_kayu'],
    ['SURAT KELAKUAN BAIK','ri-shield-check-fill','Kelakuan baik','kelakuan_baik'],
    ['SURAT KETERANGAN HILANG','ri-error-warning-fill','Kehilangan','hilang'],
    ['SURAT KETERANGAN PENGHASILAN','ri-money-dollar-box-fill','Penghasilan','penghasilan'],
    ['SURAT PENGANTAR NIKAH','ri-user-heart-fill','Nikah','nikah'],
    ['SURAT PERNYATAAN KESANGGUPAN','ri-file-text-fill','Kesanggupan','pernyataan_kesanggupan'],
];

$totalPending = 0;
foreach ($surat as $i => $s) {
    $jml = 0;
    if (mambu_table_exists($mysqli, 'pengajuan_surat')) {
        $stmt = $mysqli->prepare("SELECT COUNT(*) jml FROM pengajuan_surat WHERE jenis_surat=? AND status='pending'");
        $stmt->bind_param('s', $s[3]);
        $stmt->execute();
        $jml = (int)($stmt->get_result()->fetch_assoc()['jml'] ?? 0);
        $stmt->close();
    }
    $surat[$i]['pending'] = $jml;
    $totalPending += $jml;
}
?>

<section class="page-hero">
    <div class="page-title">
        <span class="page-title-icon"><i class="ri-file-list-3-fill"></i></span>
        <div>
            <h1>Administrasi Surat</h1>
            <p>Pilih jenis surat untuk melihat daftar pengajuan dan proses administrasi warga.</p>
        </div>
    </div>
    <div class="page-actions">
        <a class="action-btn secondary" href="pengajuan_menu.php"><i class="ri-inbox-archive-line"></i> Pengajuan Masuk</a>
    </div>
</section>

<div class="stats-grid compact">
    <article class="stat-card tone-blue"><span class="stat-icon"><i class="ri-file-copy-2-fill"></i></span><div><div class="stat-label">Template Surat</div><div class="stat-value"><?= count($surat) ?></div><div class="stat-sub">jenis tersedia</div></div></article>
    <article class="stat-card tone-orange"><span class="stat-icon"><i class="ri-alarm-warning-fill"></i></span><div><div class="stat-label">Pending</div><div class="stat-value"><?= mambu_fmt($totalPending) ?></div><div class="stat-sub">pengajuan warga</div></div></article>
    <article class="stat-card tone-green"><span class="stat-icon"><i class="ri-printer-fill"></i></span><div><div class="stat-label">Cetak Surat</div><div class="stat-value">PDF</div><div class="stat-sub">melalui halaman detail</div></div></article>
</div>

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

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