<?= $this->extend('layout/template') ?>

<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-1 text-gray-800 fw-bold">Dashboard Admin Bidang</h1>
            <p class="mb-0 text-muted">Pantau progres kinerja OPD di bawah bidang Anda</p>
        </div>
        <form class="d-flex align-items-end gap-2" method="get" action="<?= base_url('admin_bidang/dashboard') ?>">
            <div>
                <label for="tahun" class="form-label mb-1 small">Tahun</label>
                <input type="number" class="form-control" id="tahun" name="tahun" value="<?= $tahun ?>" style="width: 120px;">
            </div>
            <div>
                <label for="triwulan" class="form-label mb-1 small">Triwulan</label>
                <select class="form-select" id="triwulan" name="triwulan" style="width: 140px;">
                    <option value="1" <?= $triwulan == 1 ? 'selected' : '' ?>>Triwulan 1</option>
                    <option value="2" <?= $triwulan == 2 ? 'selected' : '' ?>>Triwulan 2</option>
                    <option value="3" <?= $triwulan == 3 ? 'selected' : '' ?>>Triwulan 3</option>
                    <option value="4" <?= $triwulan == 4 ? 'selected' : '' ?>>Triwulan 4</option>
                </select>
            </div>
            <div class="pt-4">
                <button type="submit" class="btn btn-primary">Terapkan</button>
            </div>
        </form>
    </div>

    <!-- Stats Cards -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total OPD Binaan</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['totalOpd'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-building fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Perlu Verifikasi</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['pending'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-exclamation-circle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Laporan Disetujui</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['disetujui'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Total Laporan Masuk</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['totalLaporan'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-file-alt fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- OPD List -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Daftar OPD Binaan</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th width="5%">No</th>
                            <th>Nama OPD</th>
                            <th class="text-center">Status Laporan Triwulan Ini</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($opdList)): ?>
                            <tr>
                                <td colspan="4" class="text-center">Tidak ada OPD di bawah bidang ini.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach($opdList as $index => $opd): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= $opd['nama_opd'] ?></td>
                                <td class="text-center">
                                    <?php
                                        $sid = $opd['id'];
                                        $s = $statusPerOpd[$sid] ?? ['Diajukan' => 0, 'Disetujui' => 0, 'Draft' => 0, 'total' => 0];
                                        if (($s['Diajukan'] ?? 0) > 0) {
                                            echo '<span class="badge bg-warning text-dark">Perlu Verifikasi ('.($s['Diajukan'] ?? 0).')</span>';
                                        } elseif (($s['Disetujui'] ?? 0) > 0) {
                                            echo '<span class="badge bg-success">Disetujui ('.($s['Disetujui'] ?? 0).')</span>';
                                        } elseif (($s['total'] ?? 0) > 0) {
                                            echo '<span class="badge bg-info text-dark">Proses</span>';
                                        } else {
                                            echo '<span class="badge bg-secondary">Belum Ada Laporan</span>';
                                        }
                                    ?>
                                </td>
                                <td class="text-center">
                                    <a href="<?= base_url('admin_bidang/verifikasi?opd_id='.$opd['id'].'&tahun='.$tahun.'&triwulan='.$triwulan) ?>" class="btn btn-sm btn-info"><i class="fas fa-eye"></i> Detail</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
