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

<div class="container-fluid">
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Master Materi & TP</h1>
    </div>

    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?= session()->getFlashdata('success') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">Daftar Master Materi & TP Sekolah</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped table-hover align-middle" id="dataTable" width="100%" cellspacing="0">
                    <thead class="table-light">
                        <tr>
                            <th width="5%" class="text-center">No</th>
                            <th width="15%">Tahun Ajaran</th>
                            <th width="15%">Mata Pelajaran</th>
                            <th width="10%">Kelas</th>
                            <th width="15%">Jumlah Bab</th>
                            <th width="15%">Jumlah TP</th>
                            <th width="15%">Dibuat Oleh</th>
                            <th width="10%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($tps)) : ?>
                            <?php foreach ($tps as $index => $item) : 
                                $chaptersCount = count(json_decode($item['chapters'], true) ?: []);
                                $tpsCount = count(json_decode($item['tps'], true) ?: []);
                            ?>
                                <tr>
                                    <td class="text-center"><?= $index + 1 ?></td>
                                    <td><?= esc($item['academic_year_name'] ?: 'N/A') ?></td>
                                    <td><?= esc($item['subject_name']) ?></td>
                                    <td><?= esc($item['grade_name']) ?></td>
                                    <td><span class="badge bg-info"><?= $chaptersCount ?> Bab</span></td>
                                    <td><span class="badge bg-success"><?= $tpsCount ?> TP</span></td>
                                    <td><?= esc($item['user_name'] ?: 'N/A') ?></td>
                                    <td class="text-center">
                                        <form action="/admin/tp-management/delete/<?= $item['id'] ?>" method="post" class="d-inline">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus data Master TP ini? Data yang dihapus tidak dapat dikembalikan.');" title="Hapus">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr>
                                <td colspan="8" class="text-center py-4 text-muted">Belum ada data Master Materi & TP yang tersimpan.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="mt-3 text-muted small">
                <i class="bi bi-info-circle me-1"></i> Data di atas dihasilkan secara otomatis ketika Guru menyimpan Tujuan Pembelajaran di perangkat masing-masing, agar dapat digunakan ulang oleh guru lain di tahun berikutnya.
            </div>
        </div>
    </div>
</div>

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