<?= $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">Program & Kegiatan</h1>
            <p class="mb-0 text-muted">Daftar Program, Kegiatan, dan Sub Kegiatan OPD</p>
        </div>
        <div>
            <a href="<?= base_url('program/template') ?>" class="btn btn-success shadow-sm me-2"><i class="fas fa-file-excel me-1"></i> Template Import</a>
            <a href="<?= base_url('program/import') ?>" class="btn btn-primary shadow-sm"><i class="fas fa-file-import me-1"></i> Import Excel</a>
        </div>
    </div>

    <?php if(session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show shadow-sm border-0 border-start border-success border-4" role="alert">
            <i class="fas fa-check-circle me-2"></i> <?= session()->getFlashdata('success') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php if(session()->getFlashdata('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show shadow-sm border-0 border-start border-danger border-4" role="alert">
            <i class="fas fa-times-circle me-2"></i> <?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php
        $totalPrograms = count($programs);
        $totalKegiatan = 0;
        $totalSub = 0;
        $totalPagu = 0.0;
        foreach ($programs as $p) {
            $totalKegiatan += count($p['kegiatan']);
            foreach ($p['kegiatan'] as $k) {
                $totalSub += count($k['sub_kegiatan']);
                foreach ($k['sub_kegiatan'] as $sk) {
                    $totalPagu += (float)$sk['pagu_anggaran'];
                }
            }
        }
    ?>
    <div class="row g-3 mb-4">
        <div class="col-12 col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Program</div>
                        <div class="h4 fw-bold mb-0"><?= $totalPrograms ?></div>
                    </div>
                    <div class="rounded-circle bg-primary bg-opacity-10 p-3 text-primary"><i class="fas fa-list"></i></div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Kegiatan</div>
                        <div class="h4 fw-bold mb-0"><?= $totalKegiatan ?></div>
                    </div>
                    <div class="rounded-circle bg-info bg-opacity-10 p-3 text-info"><i class="fas fa-tasks"></i></div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Sub Kegiatan</div>
                        <div class="h4 fw-bold mb-0"><?= $totalSub ?></div>
                    </div>
                    <div class="rounded-circle bg-warning bg-opacity-10 p-3 text-warning"><i class="fas fa-sitemap"></i></div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-3">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Total Pagu</div>
                        <div class="h4 fw-bold mb-0">Rp <?= number_format($totalPagu, 0, ',', '.') ?></div>
                    </div>
                    <div class="rounded-circle bg-success bg-opacity-10 p-3 text-success"><i class="fas fa-coins"></i></div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4 rounded-3">
        <div class="card-header bg-white py-3 border-0">
            <div class="row g-2 align-items-center">
                <div class="col">
                    <h6 class="m-0 fw-bold text-primary"><i class="fas fa-tasks me-2"></i>Data Program</h6>
                </div>
                <div class="col-12 col-md-auto">
                    <form action="" method="get" class="d-flex align-items-center">
                        <div class="input-group input-group-sm">
                            <select name="opd_id" class="form-select bg-light border-0 small" aria-label="Filter OPD">
                                <option value="">Semua OPD</option>
                                <?php foreach($opd_list as $opd): ?>
                                    <option value="<?= $opd['id'] ?>" <?= $selected_opd == $opd['id'] ? 'selected' : '' ?>><?= $opd['nama_opd'] ?></option>
                                <?php endforeach; ?>
                            </select>
                            <button class="btn btn-primary" type="submit">Filter</button>
                        </div>
                    </form>
                </div>
                <div class="col-12 col-md">
                    <input type="text" id="searchPrograms" class="form-control form-control-sm" placeholder="Cari program/kegiatan/sub">
                </div>
            </div>
        </div>
        <div class="card-body">
            <?php if(empty($programs)): ?>
                <div class="text-center py-5">
                    <img src="https://img.freepik.com/free-vector/no-data-concept-illustration_114360-536.jpg" alt="No Data" style="max-width: 200px; opacity: 0.8;">
                    <h5 class="mt-3 text-gray-800 fw-bold">Belum ada data program</h5>
                    <p class="text-muted mb-4">Silakan import data program & kegiatan menggunakan file Excel.</p>
                    <a href="<?= base_url('program/import') ?>" class="btn btn-primary"><i class="fas fa-upload me-2"></i>Upload File Excel</a>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-bordered table-hover align-middle" width="100%" cellspacing="0" id="programsTable">
                        <thead class="bg-light align-middle sticky-top">
                            <tr>
                                <th style="width:15%">Kode</th>
                                <th>Uraian</th>
                                <th style="width:25%">OPD</th>
                                <th style="width:20%" class="text-end">Pagu</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($programs as $p): ?>
                                <?php foreach($p['kegiatan'] as $k): ?>
                                    <?php foreach($k['sub_kegiatan'] as $sk): ?>
                                        <tr>
                                            <td><?= (!empty($sk['kode_sub_kegiatan']) && strtoupper((string)$sk['kode_sub_kegiatan']) !== 'N/A') ? $sk['kode_sub_kegiatan'] : '—' ?></td>
                                            <td>
                                                <?= $sk['nama_sub_kegiatan'] ?>
                                                <div class="small text-muted">Program: <?= $p['nama_program'] ?> • Kegiatan: <?= $k['nama_kegiatan'] ?></div>
                                                <div class="small text-muted">Target: <?= $sk['target_kinerja'] ?> <?= $sk['satuan'] ?></div>
                                            </td>
                                            <td><?= $p['nama_opd'] ?></td>
                                            <td class="text-end">Rp <?= number_format((float)$sk['pagu_anggaran'], 0, ',', '.') ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endforeach; ?>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<script>
    const s = document.getElementById('searchPrograms');
    if (s) {
        s.addEventListener('input', function () {
            const q = this.value.toLowerCase();
            const rows = document.querySelectorAll('#programsTable tbody tr');
            rows.forEach(r => {
                const text = r.innerText.toLowerCase();
                r.style.display = text.indexOf(q) !== -1 ? '' : 'none';
            });
        });
    }
</script>
<?= $this->endSection() ?>
