<?= $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">Dashboard OPD</h1>
            <p class="mb-0 text-muted">Selamat datang, <?= session()->get('username') ?></p>
        </div>
        <a href="<?= base_url('opd_kinerja/input') ?>" class="btn btn-primary shadow-sm">
            <i class="fas fa-plus-circle me-1"></i> Input Laporan Baru
        </a>
    </div>

    <!-- Stats Cards -->
    <div class="row mb-4">
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total Laporan Dikirim</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['laporan_dikirim'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-paper-plane fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Perlu Revisi</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['perlu_revisi'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-edit fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Laporan Disetujui</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?= $stats['disetujui'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Activity -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Aktivitas Laporan Terakhir</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Sub Kegiatan</th>
                            <th>Triwulan</th>
                            <th>Status</th>
                            <th>Tanggal Update</th>
                            <th>Catatan Admin</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($recent)): ?>
                            <tr>
                                <td colspan="5" class="text-center text-muted">Belum ada aktivitas laporan.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach($recent as $r): ?>
                            <tr>
                                <td><?= $r['nama_sub_kegiatan'] ?></td>
                                <td>TW <?= $r['triwulan'] ?></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', strtotime($r['updated_at'] ?? $r['created_at'])) ?></td>
                                <td>
                                    <?php if($r['catatan']): ?>
                                        <small class="text-danger fst-italic"><i class="fas fa-comment-alt me-1"></i> <?= $r['catatan'] ?></small>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
