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

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

<div class="content-header">
    <div class="header-title">
        <h2>Detail Peserta Kelas</h2>
        <p>Mengelola KRS Mahasiswa untuk kelas <?= esc($kelas['nama_kelas_kuliah']) ?> (<?= esc($kelas['nama_mata_kuliah']) ?>)</p>
    </div>
    <div class="header-actions">
        <a href="<?= base_url('kelas-perkuliahan') ?>" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Kembali
        </a>
    </div>
</div>

<?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="card" style="background: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 1.5rem; overflow: hidden;">
    <div class="card-header" style="background: #f8f9fa; padding: 15px 20px; border-bottom: 1px solid #eee;">
        <h3 style="margin: 0; font-size: 16px; font-weight: bold;">Tambah Peserta (KRS)</h3>
    </div>
    <div class="card-body" style="padding: 20px;">
        
        <!-- Filter Form -->
        <form action="" method="GET" style="margin-bottom: 15px; padding-bottom: 15px; border-bottom: 1px dashed #ddd;">
            <div style="display: flex; gap: 10px; align-items: flex-end;">
                <div class="form-group" style="flex: 1; margin-bottom: 0;">
                    <label style="font-weight: 500; font-size: 13px; display: block; margin-bottom: 5px;">Filter Program Studi</label>
                    <select name="prodi" class="form-control" onchange="this.form.submit()">
                        <option value="">Semua Program Studi</option>
                        <?php foreach ($list_prodi as $p) : ?>
                            <option value="<?= esc($p['id']) ?>" <?= ($filterProdi == $p['id']) ? 'selected' : '' ?>>
                                <?= esc($p['nama_prodi']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group" style="flex: 1; margin-bottom: 0;">
                    <label style="font-weight: 500; font-size: 13px; display: block; margin-bottom: 5px;">Filter Angkatan</label>
                    <select name="angkatan" class="form-control" onchange="this.form.submit()">
                        <option value="">Semua Angkatan</option>
                        <?php foreach ($list_angkatan as $a) : ?>
                            <option value="<?= esc($a['angkatan']) ?>" <?= ($filterAngkatan == $a['angkatan']) ? 'selected' : '' ?>>
                                <?= esc($a['angkatan']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button type="submit" class="btn btn-secondary" style="height: 38px;">Filter</button>
            </div>
        </form>

        <form action="<?= base_url('kelas-perkuliahan/peserta/add') ?>" method="POST" style="display: flex; gap: 10px; align-items: flex-end;">
            <?= csrf_field() ?>
            <input type="hidden" name="id_kelas_kuliah_pddikti" value="<?= esc($kelas['id_kelas_kuliah_pddikti']) ?>">
            
            <div class="form-group" style="flex: 1; margin-bottom: 0;">
                <label style="font-weight: bold; display: block; margin-bottom: 8px;">Pilih Mahasiswa</label>
                <select name="id_mahasiswa_pddikti" class="form-control" required style="width: 100%;">
                    <option value="">-- Pilih Mahasiswa --</option>
                    <?php foreach ($mahasiswaList as $m) : ?>
                        <option value="<?= esc($m['id_mahasiswa_pddikti']) ?>"><?= esc($m['nim']) ?> - <?= esc($m['nama_lengkap']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <button type="submit" class="btn btn-primary" style="height: 38px;">
                <i class="fas fa-plus"></i> Tambahkan ke Kelas
            </button>
        </form>
    </div>
</div>

<div class="card" style="background: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); overflow: hidden;">
    <div class="card-header" style="background: #f8f9fa; padding: 15px 20px; border-bottom: 1px solid #eee;">
        <h3 style="margin: 0; font-size: 16px; font-weight: bold;">Daftar Mahasiswa yang Terdaftar</h3>
    </div>
    <div class="table-responsive" style="padding: 20px;">
        <table class="table table-bordered table-striped" style="width: 100%;">
            <thead style="background-color: #f8f9fa;">
                <tr>
                    <th style="width: 5%; text-align: center;">NO</th>
                    <th style="width: 20%;">NIM</th>
                    <th style="width: 60%;">NAMA MAHASISWA</th>
                    <th style="width: 15%; text-align: center;">AKSI</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($peserta)) : ?>
                    <tr>
                        <td colspan="4" style="text-align: center; padding: 20px; color: #6c757d;">
                            Belum ada mahasiswa di kelas ini.
                        </td>
                    </tr>
                <?php else : ?>
                    <?php $no = 1; foreach ($peserta as $p) : ?>
                        <tr>
                            <td style="text-align: center;"><?= $no++ ?></td>
                            <td><strong><?= esc($p['nim']) ?></strong></td>
                            <td><?= esc($p['nama_mahasiswa']) ?></td>
                            <td style="text-align: center;">
                                <a href="<?= base_url('kelas-perkuliahan/peserta/delete/' . $p['id']) ?>" 
                                   class="btn btn-danger btn-sm"
                                   onclick="return confirm('Apakah Anda yakin ingin mengeluarkan mahasiswa ini dari kelas?');">
                                    <i class="fas fa-trash"></i> Hapus
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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