<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<div class="card shadow-sm border-0 mb-4">
    <div class="card-body">
        <form action="" method="GET" class="row g-2 align-items-end">
            <?php if (session()->get('level') == 'kabupaten' || session()->get('level') == 'Superadmin'): ?>
                <div class="col-md-4">
                    <label class="small fw-bold">Pilih OPD</label>
                    <select name="id_opd" class="form-select form-select-sm" required>
                        <option value="">-- Pilih Instansi --</option>
                        <?php foreach ($list_opd as $o): ?>
                            <?php $selected_opd = $selected_opd ?? ''; ?>

                            <option value="<?= $o['id_opd'] ?>" <?= ($selected_opd == $o['id_opd']) ? 'selected' : '' ?>>
                                <?= $o['nama_opd'] ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            <?php endif; ?>
            <div class="col-md-2">
                <label class="small fw-bold">Bulan</label>
                <select name="bulan" class="form-select form-select-sm">
                    <?php for ($m = 1; $m <= 12; $m++): ?>
                        <option value="<?= $m ?>" <?= ($bulan == $m) ? 'selected' : '' ?>><?= date('F', mktime(0, 0, 0, $m, 10)) ?></option>
                    <?php endfor; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="small fw-bold">Tahun</label>
                <input type="number" name="tahun" class="form-control form-control-sm" value="<?= $tahun ?>">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary btn-sm w-100"><i class="fas fa-search me-1"></i> Tampilkan</button>
            </div>
            <div class="col-md-2">
                <button type="button" onclick="window.print()" class="btn btn-outline-secondary btn-sm w-100"><i class="fas fa-print me-1"></i> Cetak</button>
            </div>
        </form>
    </div>
</div>

<?php if (!empty($rekap)): ?>
    <div class="card border-0 shadow-sm overflow-hidden">
        <div class="table-responsive">
            <table class="table table-bordered table-sm text-center small align-middle mb-0" style="font-size: 0.7rem;">
                <thead class="bg-light">
                    <tr>
                        <th rowspan="2" class="align-middle">No</th>
                        <th rowspan="2" class="align-middle text-start ps-2" style="min-width: 180px;">Nama Pegawai</th>
                        <?php foreach ($dates as $d): ?>
                            <th style="min-width: 22px; <?= (date('N', strtotime($d)) == 7) ? 'background:#ffebee;' : '' ?>">
                                <?= date('d', strtotime($d)) ?>
                            </th>
                        <?php endforeach; ?>
                        <th colspan="5" class="bg-primary text-white">Total</th>
                    </tr>
                    <tr>
                        <?php foreach ($dates as $d): ?>
                            <th style="font-size: 0.6rem; <?= (date('N', strtotime($d)) == 7) ? 'background:#ffebee;' : '' ?>">
                                <?= substr(date('D', strtotime($d)), 0, 1) ?>
                            </th>
                        <?php endforeach; ?>
                        <th class="bg-success text-white">H</th>
                        <th class="bg-warning text-dark">T</th>
                        <th class="bg-info text-white">S</th>
                        <th class="bg-primary text-white">I</th>
                        <th class="bg-dark text-white">A</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $no = 1;
                    foreach ($rekap as $r): ?>
                        <tr>
                            <td><?= $no++ ?></td>
                            <td class="text-start ps-2">
                                <div class="fw-bold"><?= $r['nama'] ?></div>
                                <div class="text-muted" style="font-size: 0.6rem;"><?= $r['nip'] ?></div>
                            </td>
                            <?php foreach ($dates as $tgl): ?>
                                <?php $h = $r['harian'][$tgl]; ?>
                                <td class="<?= $h['bg'] ?> text-white fw-bold" style="padding: 2px;">
                                    <?= $h['code'] ?>
                                </td>
                            <?php endforeach; ?>

                            <td class="fw-bold"><?= $r['h'] ?></td>
                            <td class="fw-bold"><?= $r['t'] ?></td>
                            <td class="fw-bold"><?= $r['s'] ?></td>
                            <td class="fw-bold"><?= $r['i'] ?></td>
                            <td class="fw-bold text-danger"><?= $r['a'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php else: ?>
    <div class="alert alert-info text-center">Silakan pilih OPD untuk melihat rekapitulasi.</div>
<?php endif; ?>

<style>
    /* Styling khusus untuk cetak */
    @media print {
        body * {
            visibility: hidden;
        }

        .table-responsive,
        .table-responsive * {
            visibility: visible;
        }

        .table-responsive {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
        }

        .no-print {
            display: none;
        }
    }
</style>
<?= $this->endSection() ?>