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

<?= $this->section('content') ?>
<div class="container-fluid">
    <h1 class="h4 fw-bold mb-3">Ekspor Laporan</h1>

    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <form action="<?= base_url('laporan/ekspor') ?>" method="get" id="formFilter">
                        <div class="mb-3">
                            <label for="tahun" class="form-label">Tahun</label>
                            <select name="tahun" id="tahun" class="form-select">
                                <?php for($i = date('Y'); $i >= date('Y')-4; $i--): ?>
                                    <option value="<?= $i ?>"><?= $i ?></option>
                                <?php endfor; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="triwulan" class="form-label">Triwulan</label>
                            <select name="triwulan" id="triwulan" class="form-select">
                                <option value="1">Triwulan 1</option>
                                <option value="2">Triwulan 2</option>
                                <option value="3">Triwulan 3</option>
                                <option value="4">Triwulan 4</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="opd_id" class="form-label">OPD (Opsional)</label>
                            <select name="opd_id" id="opd_id" class="form-select">
                                <option value="">Semua OPD</option>
                                <?php foreach($opd as $o): ?>
                                    <option value="<?= $o['id'] ?>"><?= $o['nama_opd'] ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="d-flex justify-content-end gap-2">
                            <a class="btn btn-success" id="btnExcel">Export Excel</a>
                            <a class="btn btn-danger" id="btnPdf">Export PDF</a>
                        </div>
            </form>
        </div>
    </div>
</div>
<script>
    const form = document.getElementById('formFilter');
    const btnExcel = document.getElementById('btnExcel');
    const btnPdf = document.getElementById('btnPdf');
    function buildQuery() {
        const params = new URLSearchParams(new FormData(form)).toString();
        return params;
    }
    btnExcel.addEventListener('click', function (e) {
        e.preventDefault();
        const q = buildQuery();
        window.location.href = "<?= base_url('laporan/ekspor/excel') ?>?" + q;
    });
    btnPdf.addEventListener('click', function (e) {
        e.preventDefault();
        const q = buildQuery();
        window.location.href = "<?= base_url('laporan/ekspor/pdf') ?>?" + q;
    });
</script>
<?= $this->endSection() ?>
