<?= $this->extend('layout/default') ?>

<?= $this->section('content') ?>
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h4 class="card-title mb-0">Pemetaan Kurikulum per Angkatan</h4>
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalTambah">
                    <i class="ti ti-plus"></i> Tambah Pemetaan
                </button>
            </div>
            <div class="card-body">
                <?php if (session()->getFlashdata('success')) : ?>
                    <div class="alert alert-success"><?= session()->getFlashdata('success') ?></div>
                <?php endif; ?>
                <?php if (session()->getFlashdata('error')) : ?>
                    <div class="alert alert-danger"><?= session()->getFlashdata('error') ?></div>
                <?php endif; ?>

                <div class="table-responsive">
                    <table class="table table-bordered table-striped datatable">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>Program Studi</th>
                                <th>Angkatan</th>
                                <th>Kurikulum yang Berlaku</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $no = 1; foreach ($pemetaan as $p) : ?>
                            <tr>
                                <td><?= $no++ ?></td>
                                <td><?= $p['nama_program_studi'] ?></td>
                                <td><?= $p['angkatan'] ?></td>
                                <td><?= $p['nama_kurikulum'] ?></td>
                                <td>
                                    <form action="/pemetaan-kurikulum/hapus/<?= $p['id'] ?>" method="post" class="d-inline">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Hapus pemetaan ini?')">
                                            <i class="ti ti-trash"></i> Hapus
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal Tambah -->
<div class="modal fade" id="modalTambah" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Atur Pemetaan Kurikulum</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="/pemetaan-kurikulum/simpan" method="post">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Program Studi</label>
                        <select name="prodi_id" class="form-select" required>
                            <option value="">-- Pilih Program Studi --</option>
                            <?php foreach ($listProdi as $prodi) : ?>
                                <option value="<?= $prodi['id'] ?>"><?= $prodi['nama_prodi'] ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Angkatan (Tahun)</label>
                        <input type="number" name="angkatan" class="form-control" placeholder="Contoh: 2024" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Kurikulum</label>
                        <select name="kurikulum_id" class="form-select" required>
                            <option value="">-- Pilih Kurikulum --</option>
                            <?php foreach ($kurikulum as $k) : ?>
                                <option value="<?= $k['id'] ?>"><?= $k['nama_kurikulum'] ?> (<?= $k['nama_program_studi'] ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <small class="text-muted">Catatan: Jika sudah ada aturan untuk prodi dan angkatan yang sama, data akan diupdate ke kurikulum yang baru dipilih.</small>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

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