<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>

<div class="row g-3 mb-3">
    <div class="col-12">
        <div class="card border-0 shadow-sm">
            <div class="card-body d-flex align-items-center justify-content-between py-3 flex-wrap gap-2">
                <div>
                    <h5 class="mb-0 fw-semibold text-primary">
                        <i class="bi bi-file-earmark-text me-2"></i>Kalender Pendidikan (Daftar Kegiatan)
                    </h5>
                    <small class="text-muted">Tahun Ajaran <strong><?= esc($year['name']) ?></strong></small>
                </div>
                <div>
                    <a href="/academic-calendar/preview-report/<?= $year['id'] ?>" target="_blank" class="btn btn-sm btn-success">
                        <i class="bi bi-printer me-1"></i>Preview & Cetak
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <!-- KOLOM KIRI: DAFTAR KEGIATAN PER BULAN -->
    <div class="col-12 col-lg-8">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white py-3">
                <h6 class="mb-0 fw-bold text-dark">
                    <i class="bi bi-calendar-event me-2 text-primary"></i>Rincian Kegiatan per Bulan
                </h6>
            </div>
            <div class="card-body p-0">
                <div class="accordion accordion-flush" id="accordionMonths">
                    <?php foreach ($months as $index => $m): ?>
                        <?php
                            $key   = $m['year'] . '-' . $m['month'];
                            $items = $eventsByMonth[$key] ?? [];
                            $collapseId = 'collapse_' . $key;
                            $headingId = 'heading_' . $key;
                            $isCurrentMonth = ((int)date('n') === (int)$m['month'] && (int)date('Y') === (int)$m['year']);
                        ?>
                        <div class="accordion-item">
                            <h2 class="accordion-header" id="<?= $headingId ?>">
                                <button class="accordion-button <?= $isCurrentMonth ? '' : 'collapsed' ?> py-3 px-4 fw-semibold" 
                                        type="button" 
                                        data-bs-toggle="collapse" 
                                        data-bs-target="#<?= $collapseId ?>" 
                                        aria-expanded="<?= $isCurrentMonth ? 'true' : 'false' ?>" 
                                        aria-controls="<?= $collapseId ?>">
                                    <div class="d-flex align-items-center justify-content-between w-100 me-3">
                                        <span>
                                            <i class="bi bi-calendar3 me-2 text-secondary"></i>
                                            <?= cal_indo_month($m['month']) . ' ' . $m['year'] ?>
                                        </span>
                                        <span class="badge bg-primary bg-opacity-10 text-primary rounded-pill px-2.5 py-1" style="font-size: 0.72rem;">
                                            <?= count($items) ?> Kegiatan
                                        </span>
                                    </div>
                                </button>
                            </h2>
                            <div id="<?= $collapseId ?>" 
                                 class="accordion-collapse collapse <?= $isCurrentMonth ? 'show' : '' ?>" 
                                 aria-labelledby="<?= $headingId ?>" 
                                 data-bs-parent="#accordionMonths">
                                <div class="accordion-body p-0">
                                    <div class="table-responsive">
                                        <table class="table table-sm table-hover align-middle mb-0" style="font-size: 0.82rem;">
                                            <thead class="table-light">
                                                <tr>
                                                    <th class="ps-4 py-2" style="width: 50px;">No</th>
                                                    <th class="py-2" style="width: 180px;">Tanggal</th>
                                                    <th class="py-2">Uraian Kegiatan</th>
                                                    <th class="pe-4 py-2 text-end" style="width: 120px;">Kategori</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <?php if (empty($items)): ?>
                                                <tr>
                                                    <td colspan="4" class="text-center text-muted py-4 ps-4 pe-4">
                                                        <i class="bi bi-calendar-x d-block fs-4 mb-1 text-secondary"></i>
                                                        Tidak ada kegiatan khusus pada bulan ini.
                                                    </td>
                                                </tr>
                                            <?php else: ?>
                                                <?php $no = 1; foreach ($items as $it): ?>
                                                    <?php
                                                        $tglMulai   = date('d-m-Y', strtotime($it['start_date']));
                                                        $tglSelesai = date('d-m-Y', strtotime($it['end_date']));
                                                        $tanggal    = ($tglMulai === $tglSelesai) ? $tglMulai : ($tglMulai . ' s.d. ' . $tglSelesai);
                                                    ?>
                                                    <tr>
                                                        <td class="ps-4 text-muted"><?= $no++ ?></td>
                                                        <td class="fw-semibold text-dark"><?= $tanggal ?></td>
                                                        <td>
                                                            <div class="fw-medium"><?= esc($it['title']) ?></div>
                                                            <?php if (!empty($it['description'])): ?>
                                                                <small class="text-muted d-block mt-0.5" style="font-size: 0.75rem;"><?= esc($it['description']) ?></small>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td class="pe-4 text-end">
                                                            <span class="badge font-monospace px-2.5 py-1 text-white border-0 shadow-sm" style="background:<?= esc($it['color']) ?>; font-size: 0.7rem;">
                                                                <?= esc($it['category_code']) ?>
                                                            </span>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- KOLOM KANAN: REKAPITULASI & WIDGETS -->
    <div class="col-12 col-lg-4">
        <!-- REKAPITULASI HARI EFEKTIF -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white py-3">
                <h6 class="mb-0 fw-bold text-dark">
                    <i class="bi bi-calculator me-2 text-primary"></i>Rekapitulasi Hari Efektif
                </h6>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm table-hover align-middle mb-0" style="font-size: 0.8rem;">
                        <thead class="table-light">
                            <tr class="text-center">
                                <th class="ps-3 text-start py-2">Bulan</th>
                                <th class="py-2" style="width: 60px;">Hari</th>
                                <th class="py-2" style="width: 60px;">Libur</th>
                                <th class="pe-3 py-2 text-end" style="width: 70px;">Efektif</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php $totalHari = $totalLibur = $totalEfektif = 0;
                              foreach ($rekap as $r):
                                  $totalHari += $r['total']; $totalLibur += $r['libur']; $totalEfektif += $r['efektif']; ?>
                            <tr class="text-center">
                                <td class="ps-3 text-start fw-medium"><?= esc($r['label']) ?></td>
                                <td><?= $r['total'] ?></td>
                                <td class="text-danger"><?= $r['libur'] ?></td>
                                <td class="pe-3 text-end fw-bold text-success"><?= $r['efektif'] ?></td>
                            </tr>
                        <?php endforeach; ?>
                            <tr style="font-weight:bold;" class="table-group-divider table-light text-center">
                                <td class="ps-3 text-start">Jumlah</td>
                                <td><?= $totalHari ?></td>
                                <td class="text-danger"><?= $totalLibur ?></td>
                                <td class="pe-3 text-end text-success"><?= $totalEfektif ?></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <?php if (!empty($settings['effective_day_note'])): ?>
                    <div class="p-3 bg-light bg-opacity-50 text-muted small border-top" style="font-size: 0.72rem; line-height: 1.35;">
                        <i class="bi bi-info-circle me-1"></i>
                        <?= esc($settings['effective_day_note']) ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- LEGENDA KATEGORI -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white py-3">
                <h6 class="mb-0 fw-bold text-dark">
                    <i class="bi bi-info-circle me-2 text-primary"></i>Kategori Kegiatan
                </h6>
            </div>
            <div class="card-body p-2">
                <div class="list-group list-group-flush">
                    <?php foreach ($categories as $c): ?>
                        <div class="list-group-item d-flex align-items-center py-2 px-3 border-0 rounded bg-light bg-opacity-25 mb-1.5">
                            <span class="d-inline-block rounded me-2.5" style="width:16px;height:16px;background:<?= esc($c['color']) ?>; flex-shrink: 0; border: 1px solid rgba(0,0,0,0.1);"></span>
                            <div class="text-truncate">
                                <strong class="small" style="font-size: 0.8rem;"><?= esc($c['code']) ?></strong>
                                <span class="text-muted d-block text-truncate" style="font-size: 0.72rem;" title="<?= esc($c['name']) ?>"><?= esc($c['name']) ?></span>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <?php if (!empty($settings['source_reference'])): ?>
                    <div class="p-2.5 text-muted small border-top mt-2" style="font-size: 0.72rem;">
                        Sumber acuan: <?= esc($settings['source_reference']) ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
