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

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

<div class="container-fluid px-4 py-4">
    <!-- Header -->
    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="fw-bold text-dark mb-1 d-flex align-items-center gap-2">
                <i class="bi bi-cpu text-crimson"></i> Rencana Pelaksanaan Mengajar (RPM) / Modul Ajar
            </h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 small">
                    <li class="breadcrumb-item"><a href="/dashboard" class="text-decoration-none text-muted">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="/guru/learning-devices" class="text-decoration-none text-muted">Perangkat Pembelajaran</a></li>
                    <li class="breadcrumb-item active text-dark fw-medium" aria-current="page">RPM</li>
                </ol>
            </nav>
        </div>
        <a href="/guru/learning-devices" class="btn btn-light border-0 shadow-sm text-muted hover-bg-light transition-all rounded-pill px-3">
            <i class="bi bi-arrow-left me-1"></i> Kembali
        </a>
    </div>

    <!-- Alert Messages -->
    <div id="alert-container"></div>

    <div class="row g-4">
        <div class="col-12">
            <div class="card shadow-sm border-0 rounded-3">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center gap-2 mb-4 bg-light p-3 rounded" style="border-left: 4px solid #e83e8c;">
                        <i class="bi bi-magic text-crimson fs-4"></i>
                        <div>
                            <h6 class="fw-bold text-dark mb-1">Integrasi Karakter Profil Pelajar Pancasila</h6>
                            <p class="text-muted small mb-0">AI akan otomatis menanamkan dimensi Profil Pelajar Pancasila serta nilai-nilai moderasi beragama P5RA (jika tipe madrasah) ke dalam skenario pembelajaran Modul Ajar Anda.</p>
                        </div>
                    </div>

                    <div class="table-responsive">
                        <table class="table table-hover align-middle table-sm border">
                            <thead class="table-light">
                                <tr>
                                    <th width="40" class="text-center">No</th>
                                    <th>Bab / Materi Pokok</th>
                                    <th width="180">Tujuan Pembelajaran (TP)</th>
                                    <th width="100" class="text-center">Total JP</th>
                                    <th width="100" class="text-center">Estimasi Pertemuan</th>
                                    <th width="160" class="text-center">Status Modul</th>
                                    <th width="200" class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (!empty($chaptersData)): ?>
                                    <?php foreach ($chaptersData as $cd):
                                        $ci = $cd['index'];
                                        $totalJp = $cd['totalJp'];
                                        $estimasiPertemuan = $cd['totalPertemuan'] ?? 0;
                                        $hasModul = isset($modulMap[$ci]);
                                        $tpCodes = array_map(function($tp) { return $tp['code']; }, $cd['tps']);
                                    ?>
                                    <tr>
                                        <td class="text-center fw-bold text-secondary"><?= $ci + 1 ?></td>
                                        <td>
                                            <span class="fw-bold text-dark"><?= esc($cd['name']) ?></span>
                                        </td>
                                        <td>
                                            <span class="small text-muted"><?= esc(implode(', ', $tpCodes)) ?></span>
                                        </td>
                                        <td class="text-center"><?= $totalJp ?> JP</td>
                                        <td class="text-center"><?= $estimasiPertemuan ?> × pertemuan</td>
                                        <td class="text-center">
                                            <?php if ($hasModul): ?>
                                                <span class="badge bg-success bg-opacity-10 text-success px-3 py-2" style="border-radius: 30px;"><i class="bi bi-check-circle-fill me-1"></i> Modul Siap</span>
                                            <?php else: ?>
                                                <span class="badge bg-secondary bg-opacity-10 text-secondary px-3 py-2" style="border-radius: 30px;"><i class="bi bi-clock me-1"></i> Belum Dibuat</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center">
                                                <!-- Only Edit action is retained; preview is handled via the unified preview-all screen -->
                                                <a href="/guru/learning-devices/rpm/<?= $device['id'] ?>/edit/<?= $ci ?>" class="btn btn-sm fw-semibold" style="border-radius: 6px; background-color: #e83e8c; border-color: #e83e8c; color: #fff;">
                                                    <i class="bi bi-pencil-fill"></i> <?= $hasModul ? 'Edit Modul' : 'Buat Modul' ?>
                                                </a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="7" class="text-center text-muted py-4">Belum ada Bab/Materi Pokok yang dirumuskan. Silakan buat TP & ATP terlebih dahulu.</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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