<?= $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">Riwayat Laporan</h1>
            <p class="mb-0 text-muted">Daftar laporan kinerja yang telah dikirim</p>
        </div>
        <a href="<?= base_url('opd_kinerja/dashboard') ?>" class="btn btn-secondary shadow-sm">
            <i class="fas fa-arrow-left me-1"></i> Kembali
        </a>
    </div>

    <div class="card shadow mb-4">
        <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>Sub Kegiatan</th>
                            <th>Triwulan</th>
                            <th>Kinerja Fisik</th>
                            <th>Realisasi Anggaran</th>
                            <th>Status</th>
                            <th>Tanggal Kirim</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($riwayat)): ?>
                            <tr>
                                <td colspan="8" class="text-center py-5 text-muted">Belum ada riwayat laporan.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach($riwayat as $index => $r): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= $r['nama_sub_kegiatan'] ?></td>
                                <td>TW <?= $r['triwulan'] ?></td>
                                <td><?= $r['realisasi_kinerja'] ?> <?= $r['satuan'] ?></td>
                                <td>Rp <?= number_format($r['realisasi_anggaran'], 0, ',', '.') ?></td>
                                <td>
                                    <?php 
                                        $badgeClass = 'bg-secondary';
                                        if($r['status'] == 'Diajukan') $badgeClass = 'bg-info';
                                        elseif($r['status'] == 'Disetujui') $badgeClass = 'bg-success';
                                        elseif($r['status'] == 'Revisi') $badgeClass = 'bg-warning text-dark';
                                        elseif($r['status'] == 'Ditolak') $badgeClass = 'bg-danger';
                                    ?>
                                    <span class="badge <?= $badgeClass ?>"><?= $r['status'] ?></span>
                                </td>
                                <td><?= date('d M Y H:i', strtotime($r['created_at'])) ?></td>
                                <td class="text-center">
                                    <?php if($r['status'] == 'Revisi'): ?>
                                        <a href="<?= base_url('opd_kinerja/edit/'.$r['id']) ?>" class="btn btn-warning btn-sm" title="Perbaiki Laporan">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                    <?php else: ?>
                                        <button class="btn btn-secondary btn-sm" disabled><i class="fas fa-lock"></i></button>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
