<?= $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">Detail Program</h1>
            <p class="mb-0 text-muted"><?= $program['nama_opd'] ?> • <?= $program['kode_program'] ?> • <?= $program['nama_program'] ?></p>
        </div>
        <div class="d-flex gap-2">
            <a href="<?= base_url('admin_bidang/renja') ?>" class="btn btn-secondary shadow-sm">Kembali</a>
            <a href="<?= base_url('program/import') ?>" class="btn btn-primary shadow-sm"><i class="fas fa-file-import me-1"></i> Import Renja</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; ?>

    <form class="d-flex align-items-end gap-2 mb-3" method="get" action="<?= current_url() ?>">
        <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>
            <a href="<?= base_url('admin_bidang/verifikasi?opd_id='.$program['opd_id'].'&tahun='.$tahun.'&triwulan='.$triwulan.'&program_id='.$program['id']) ?>" class="btn btn-info">Lihat Laporan</a>
        </div>
    </form>

    <div class="row">
        <div class="col-lg-12 mb-4">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-header bg-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <h6 class="m-0 fw-bold text-primary">Kegiatan</h6>
                        <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#modalTambahKegiatan">
                            <i class="fas fa-plus me-1"></i> Tambah Kegiatan
                        </button>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th width="20%">Kode Kegiatan</th>
                                    <th>Nama Kegiatan</th>
                                    <th width="12%" class="text-center">Diajukan</th>
                                    <th width="12%" class="text-center">Disetujui</th>
                                    <th width="10%" class="text-center">Draft</th>
                                    <th width="15%" class="text-center">Jumlah Sub</th>
                                    <th width="20%" class="text-end">Total Pagu</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(empty($kegiatan)): ?>
                                <tr>
                                    <td colspan="7" class="text-center text-muted">Belum ada kegiatan</td>
                                </tr>
                                <?php else: ?>
                                <?php foreach($kegiatan as $k): ?>
                                <tr>
                                    <td><?= $k['kode_kegiatan'] ?></td>
                                    <td><?= $k['nama_kegiatan'] ?></td>
                                    <?php $sm = $status_map[$k['id']] ?? ['Diajukan'=>0,'Disetujui'=>0,'Draft'=>0]; ?>
                                    <td class="text-center"><?= $sm['Diajukan'] ?? 0 ?></td>
                                    <td class="text-center"><?= $sm['Disetujui'] ?? 0 ?></td>
                                    <td class="text-center"><?= $sm['Draft'] ?? 0 ?></td>
                                    <td class="text-center"><?= $k['jml_sub'] ?? 0 ?></td>
                                    <td class="text-end"><?= 'Rp ' . number_format((float)($k['total_pagu'] ?? 0), 0, ',', '.') ?></td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-12">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-header bg-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <h6 class="m-0 fw-bold text-primary">Daftar Sub Kegiatan</h6>
                        <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#modalTambahSub">
                            <i class="fas fa-plus me-1"></i> Tambah Sub Kegiatan
                        </button>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th width="20%">Kode</th>
                                    <th>Nama Sub Kegiatan</th>
                                    <th width="15%">Target</th>
                                    <th width="10%">Satuan</th>
                                    <th width="20%" class="text-end">Pagu</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(empty($sub_list)): ?>
                                <tr>
                                    <td colspan="5" class="text-center text-muted">Belum ada sub kegiatan</td>
                                </tr>
                                <?php else: ?>
                                <?php foreach($sub_list as $s): ?>
                                <tr>
                                    <td><?= $s['kode_sub_kegiatan'] ?></td>
                                    <td><?= $s['nama_sub_kegiatan'] ?></td>
                                    <td><?= $s['target_kinerja'] ?></td>
                                    <td><?= $s['satuan'] ?></td>
                                    <td class="text-end"><?= 'Rp ' . number_format((float)$s['pagu_anggaran'], 0, ',', '.') ?></td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="modalTambahKegiatan" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="<?= base_url('admin_bidang/kegiatan/store') ?>" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Kegiatan</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="program_id" value="<?= $program['id'] ?>">
                        <div class="mb-3">
                            <label class="form-label">Kode Kegiatan</label>
                            <input type="text" name="kode_kegiatan" class="form-control" placeholder="Contoh: 1.02.03.01">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Nama Kegiatan</label>
                            <input type="text" name="nama_kegiatan" class="form-control" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="modalTambahSub" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content">
                <form action="<?= base_url('admin_bidang/sub_kegiatan/store') ?>" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Sub Kegiatan</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="program_id" value="<?= $program['id'] ?>">
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Pilih Kegiatan</label>
                                <select name="kegiatan_id" class="form-select" required>
                                    <option value="">-- Pilih --</option>
                                    <?php foreach($kegiatan as $k): ?>
                                        <option value="<?= $k['id'] ?>"><?= $k['kode_kegiatan'] ?> - <?= $k['nama_kegiatan'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">Kode Sub Kegiatan</label>
                                <input type="text" name="kode_sub_kegiatan" class="form-control" placeholder="Contoh: 1.02.03.04">
                            </div>
                            <div class="col-md-12">
                                <label class="form-label">Nama Sub Kegiatan</label>
                                <input type="text" name="nama_sub_kegiatan" class="form-control" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Target Kinerja</label>
                                <input type="text" name="target_kinerja" class="form-control">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Satuan</label>
                                <input type="text" name="satuan" class="form-control">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Pagu Anggaran</label>
                                <input type="text" name="pagu_anggaran" class="form-control" placeholder="Contoh: 1.500.000">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
