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

<?= $this->section('content') ?>
<style>
@media print {
    .no-print { display: none !important; }
    .table th, .table td { border: 1px solid #000 !important; }
}
</style>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-3 no-print">
        <div>
            <h1 class="h4 mb-0 fw-bold">Preview Ekspor PDF</h1>
            <div class="text-muted small">Tahun <?= $tahun ?> • Triwulan <?= $triwulan ?></div>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-primary" id="btnDownload"><i class="fas fa-file-pdf me-1"></i> Download PDF</button>
            <a href="<?= base_url('laporan/ekspor') ?>" class="btn btn-secondary">Kembali</a>
        </div>
    </div>

    <div class="card shadow-sm">
        <div class="card-header bg-white">
            <h6 class="m-0 fw-bold text-primary">Rekapitulasi Capaian Kinerja</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped align-middle">
                    <thead class="bg-light">
                        <tr>
                            <th>OPD</th>
                            <th>Program</th>
                            <th>Kegiatan</th>
                            <th>Kode Sub</th>
                            <th>Sub Kegiatan</th>
                            <th>Target</th>
                            <th>Satuan</th>
                            <th>Realisasi</th>
                            <th>Realisasi Anggaran</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($rows)): ?>
                        <tr><td colspan="10" class="text-center text-muted">Belum ada data pada filter ini.</td></tr>
                        <?php else: foreach($rows as $r): ?>
                        <tr>
                            <td><?= $r['nama_opd'] ?></td>
                            <td><?= $r['nama_program'] ?></td>
                            <td><?= $r['nama_kegiatan'] ?></td>
                            <td><?= $r['kode_sub_kegiatan'] ?></td>
                            <td><?= $r['nama_sub_kegiatan'] ?></td>
                            <td><?= $r['target_master'] ?></td>
                            <td><?= $r['satuan_master'] ?></td>
                            <td><?= $r['realisasi_kinerja'] ?></td>
                            <td><?= 'Rp ' . number_format((float)$r['realisasi_anggaran'], 0, ',', '.') ?></td>
                            <td><?= $r['status'] ?></td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    (function(){
        const btn = document.getElementById('btnDownload');
        btn.addEventListener('click', function(e){
            e.preventDefault();
            const params = new URLSearchParams();
            params.set('tahun', '<?= $tahun ?>');
            params.set('triwulan', '<?= $triwulan ?>');
            <?php if(!empty($opd_id)): ?>
            params.set('opd_id', '<?= $opd_id ?>');
            <?php endif; ?>
            window.location.href = "<?= base_url('laporan/ekspor/pdf') ?>?" + params.toString();
        });
    })();
    </script>
<?= $this->endSection() ?>
