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

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

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
    <h1 class="page-title" style="margin-bottom: 0;">Kartu Rencana Studi (KRS)</h1>
    <?php if (!$krs || in_array($krs['status'], ['Draft', 'Ditolak'])) : ?>
        <a href="<?= base_url('krs/pilih-kelas') ?>" class="btn btn-primary">
            <i class='bx bx-plus'></i> Pilih Mata Kuliah
        </a>
    <?php endif; ?>
</div>

<?php if(session()->getFlashdata('success')) : ?>
    <div class="alert alert-success" style="background-color: #d1fae5; color: #065f46; padding: 1rem; border-radius: 6px; margin-bottom: 1rem; border: 1px solid #a7f3d0;">
        <?= session()->getFlashdata('success') ?>
    </div>
<?php endif; ?>

<?php if(session()->getFlashdata('error')) : ?>
    <div class="alert alert-danger" style="background-color: #fee2e2; color: #991b1b; padding: 1rem; border-radius: 6px; margin-bottom: 1rem; border: 1px solid #fecaca;">
        <?= session()->getFlashdata('error') ?>
    </div>
<?php endif; ?>

<div class="card glass" style="margin-bottom: 20px;">
    <div class="card-body">
        <h5 style="margin-top:0; color: #1e293b;">Informasi Akademik</h5>
        <table style="width: 100%; max-width: 600px;">
            <tr>
                <td style="padding: 5px 0; width: 150px; font-weight: bold; color: #64748b;">NIM / Nama</td>
                <td>: <?= esc($mahasiswa['nim']) ?> / <?= esc($mahasiswa['nama_lengkap']) ?></td>
            </tr>
            <tr>
                <td style="padding: 5px 0; font-weight: bold; color: #64748b;">Semester Aktif</td>
                <td>: <?= esc($ta['nama_ta']) ?></td>
            </tr>
            <tr>
                <td style="padding: 5px 0; font-weight: bold; color: #64748b;">Status KRS</td>
                <td>: 
                    <?php if(!$krs || $krs['status'] == 'Draft'): ?>
                        <span style="background-color: #f1f5f9; color: #475569; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">Draft</span>
                    <?php elseif($krs['status'] == 'Menunggu Persetujuan'): ?>
                        <span style="background-color: #fef3c7; color: #d97706; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">Menunggu Persetujuan</span>
                    <?php elseif($krs['status'] == 'Disetujui'): ?>
                        <span style="background-color: #d1fae5; color: #059669; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">Disetujui</span>
                    <?php else: ?>
                        <span style="background-color: #fee2e2; color: #dc2626; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">Ditolak</span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php if($krs && $krs['catatan']): ?>
            <tr>
                <td style="padding: 5px 0; font-weight: bold; color: #64748b; vertical-align: top;">Catatan Dosen PA</td>
                <td style="color: #dc2626;">: <?= nl2br(esc($krs['catatan'])) ?></td>
            </tr>
            <?php endif; ?>
        </table>
    </div>
</div>

<div class="card glass">
    <div class="card-header" style="display: flex; justify-content: space-between; align-items: center;">
        <h5 class="m-0">Daftar Mata Kuliah Diambil</h5>
        <div style="font-weight: bold; color: #3b82f6;">Total SKS: <?= $totalSks ?> / 24</div>
    </div>
    <div class="card-body" style="padding: 0;">
        <div class="table-responsive">
            <table class="table" style="width: 100%; border-collapse: collapse; margin: 0;">
                <thead>
                    <tr style="background-color: #f8fafc; border-bottom: 2px solid #e2e8f0;">
                        <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">NO</th>
                        <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">MATA KULIAH</th>
                        <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">KELAS</th>
                        <th style="padding: 1rem; text-align: center; color: #64748b; font-weight: 600; font-size: 0.875rem;">SKS</th>
                        <?php if (!$krs || in_array($krs['status'], ['Draft', 'Ditolak'])) : ?>
                        <th style="padding: 1rem; text-align: center; color: #64748b; font-weight: 600; font-size: 0.875rem;">AKSI</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php $i=1; foreach($pesertaKelas as $pk): ?>
                    <tr style="border-bottom: 1px solid #e2e8f0;">
                        <td style="padding: 1rem;"><?= $i++ ?></td>
                        <td style="padding: 1rem; font-weight: 500;"><?= esc($pk['nama_mata_kuliah']) ?></td>
                        <td style="padding: 1rem;"><?= esc($pk['nama_kelas_kuliah']) ?></td>
                        <td style="padding: 1rem; text-align: center; font-weight: bold;"><?= esc($pk['sks']) ?></td>
                        <?php if (!$krs || in_array($krs['status'], ['Draft', 'Ditolak'])) : ?>
                        <td style="padding: 1rem; text-align: center;">
                            <a href="<?= base_url('krs/hapus-kelas/'.$pk['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Hapus kelas ini dari KRS?')">
                                <i class='bx bx-trash'></i>
                            </a>
                        </td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($pesertaKelas)): ?>
                    <tr>
                        <td colspan="<?= (!$krs || in_array($krs['status'], ['Draft', 'Ditolak'])) ? '5' : '4' ?>" style="text-align: center; padding: 2rem; color: #64748b;">
                            Belum ada mata kuliah yang diambil. Silakan tambah mata kuliah.
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <?php if ($krs && in_array($krs['status'], ['Draft', 'Ditolak']) && !empty($pesertaKelas)) : ?>
    <div class="card-footer" style="padding: 1.5rem; background: #f8fafc; border-top: 1px solid #e2e8f0; text-align: right;">
        <form action="<?= base_url('krs/ajukan') ?>" method="POST">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-success" onclick="return confirm('Apakah Anda yakin ingin mengajukan KRS ini ke Dosen PA? Setelah diajukan, KRS tidak dapat diubah.')">
                <i class='bx bx-send'></i> Ajukan KRS
            </button>
        </form>
    </div>
    <?php endif; ?>
</div>

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