<?= $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">Rencana Kerja OPD</h1>
            <p class="mb-0 text-muted">Daftar Program dan Kegiatan OPD Binaan</p>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-success shadow-sm" data-bs-toggle="modal" data-bs-target="#modalTambahProgram"><i class="fas fa-plus me-1"></i> Tambah Program</button>
            <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>

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Data Program & Kegiatan</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" width="100%" cellspacing="0">
                    <thead class="bg-light">
                        <tr>
                            <th width="5%">No</th>
                            <th>Nama OPD</th>
                            <th>Kode Program</th>
                            <th>Nama Program</th>
                            <th>Jumlah Kegiatan</th>
                            <th>Pagu Anggaran</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($programs)): ?>
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">
                                    <i class="fas fa-clipboard-list fa-3x mb-3 d-block"></i>
                                    Belum ada data Rencana Kerja. Silakan import data terlebih dahulu.
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach($programs as $index => $p): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= $p['nama_opd'] ?></td>
                                <td><?= $p['kode_program'] ?></td>
                                <td><?= $p['nama_program'] ?></td>
                                <td><?= $p['jumlah_kegiatan'] ?? 0 ?></td>
                                <td><?= 'Rp ' . number_format((float)($p['total_pagu'] ?? 0), 0, ',', '.') ?></td>
                                <td class="text-center">
                                    <a href="<?= base_url('admin_bidang/program_detail/'.$p['id']) ?>" class="btn btn-info btn-sm"><i class="fas fa-eye"></i> Detail</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="modal fade" id="modalTambahProgram" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="<?= base_url('admin_bidang/program/store') ?>" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Program OPD</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Pilih OPD</label>
                            <select name="opd_id" class="form-select" required>
                                <option value="">-- Pilih OPD --</option>
                                <?php foreach($opdList as $o): ?>
                                    <option value="<?= $o['id'] ?>"><?= $o['nama_opd'] ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Kode Program</label>
                            <input type="text" name="kode_program" class="form-control" placeholder="Contoh: 1.02.03">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Nama Program</label>
                            <input type="text" name="nama_program" 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>
<?= $this->endSection() ?>
