<?= $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">Input Progres: <?= $opd['nama_opd'] ?></h1>
            <p class="mb-0 text-muted">Isi realisasi kinerja dan anggaran per Sub Kegiatan</p>
        </div>
        <a href="<?= base_url('admin_bidang/input_progres') ?>" class="btn btn-secondary shadow-sm">Kembali</a>
    </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; ?>

    <div class="card shadow-sm border-0 mb-4 rounded-3">
        <div class="card-body">
            <form action="<?= base_url('admin_bidang/input_progres/form/'.$opd['id']) ?>" method="get" class="row g-3 align-items-end mb-3">
                <div class="col-auto">
                    <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 class="col-auto">
                    <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="col-auto">
                    <label for="program_id" class="form-label mb-1 small">Program</label>
                    <select class="form-select" id="program_id" name="program_id" style="min-width: 260px;" onchange="this.form.kegiatan_id.value=''; this.form.submit()">
                        <option value="">Semua Program</option>
                        <?php foreach($programs as $pr): ?>
                            <option value="<?= $pr['id'] ?>" <?= ($program_id == $pr['id']) ? 'selected' : '' ?>><?= $pr['kode_program'] ?> - <?= $pr['nama_program'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-auto">
                    <label for="kegiatan_id" class="form-label mb-1 small">Kegiatan</label>
                    <select class="form-select" id="kegiatan_id" name="kegiatan_id" style="min-width: 260px;" <?= empty($program_id) ? 'disabled' : '' ?> onchange="this.form.submit()">
                        <option value="">Semua Kegiatan</option>
                        <?php if(!empty($kegiatan_list)): foreach($kegiatan_list as $kg): ?>
                            <option value="<?= $kg['id'] ?>" <?= ($kegiatan_id == $kg['id']) ? 'selected' : '' ?>><?= $kg['kode_kegiatan'] ?> - <?= $kg['nama_kegiatan'] ?></option>
                        <?php endforeach; endif; ?>
                    </select>
                </div>
                <div class="col-auto">
                    <button type="submit" class="btn btn-primary">Terapkan</button>
                </div>
            </form>

            <form action="<?= base_url('admin_bidang/input_progres/save') ?>" method="post">
                <input type="hidden" name="opd_id" value="<?= $opd['id'] ?>">
                <input type="hidden" name="tahun" value="<?= $tahun ?>">
                <input type="hidden" name="triwulan" value="<?= $triwulan ?>">

                <div class="d-flex gap-2 mb-3">
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="status" id="statusDiajukan" value="Diajukan" checked>
                        <label class="form-check-label" for="statusDiajukan">Kirim untuk Verifikasi</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="status" id="statusDraft" value="Draft">
                        <label class="form-check-label" for="statusDraft">Simpan sebagai Draft</label>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-bordered table-hover align-middle">
                        <thead class="bg-light">
                            <tr>
                                <th width="20%">Kegiatan</th>
                                <th width="20%">Sub Kegiatan</th>
                                <th>Target</th>
                                <th width="10%">Satuan</th>
                                <th width="15%">Realisasi Kinerja</th>
                                <th width="20%">Realisasi Anggaran</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(empty($subs)): ?>
                            <tr><td colspan="6" class="text-center text-muted">Belum ada sub kegiatan di OPD ini</td></tr>
                            <?php else: ?>
                            <?php foreach($subs as $s): 
                                $ex = $realisasiMap[$s['id']] ?? null; ?>
                            <tr>
                                <td><?= $s['nama_kegiatan'] ?></td>
                                <td>
                                    <div class="fw-bold"><?= $s['nama_sub_kegiatan'] ?></div>
                                    <div class="text-muted small"><?= $s['kode_sub_kegiatan'] ?></div>
                                </td>
                                <td>
                                    <input type="text" class="form-control form-control-sm" name="target[<?= $s['id'] ?>]" value="<?= esc($s['target_kinerja'] ?? '') ?>">
                                </td>
                                <td>
                                    <input type="text" class="form-control form-control-sm" name="satuan[<?= $s['id'] ?>]" value="<?= esc($s['satuan'] ?? '') ?>">
                                </td>
                                <td>
                                    <input type="text" class="form-control form-control-sm" name="rk[<?= $s['id'] ?>]" value="<?= esc($ex['realisasi_kinerja'] ?? '') ?>" placeholder="angka / deskripsi">
                                </td>
                                <td>
                                    <input type="text" class="form-control form-control-sm" name="ra[<?= $s['id'] ?>]" value="<?= esc($ex['realisasi_anggaran'] ?? '') ?>" placeholder="contoh: 1.500.000">
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-end">
                    <button type="submit" class="btn btn-primary px-4"><i class="fas fa-save me-1"></i> Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
