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

<?= $this->section('content') ?>
<div class="content-header">
    <div class="header-title">
        <h2>Data Kurikulum</h2>
        <p>Kelola data kurikulum program studi</p>
    </div>
    <div class="header-actions">
        <a href="<?= base_url('kurikulum/sync') ?>" class="btn btn-primary" onclick="return confirm('Proses sinkronisasi mungkin membutuhkan waktu beberapa menit. Lanjutkan?')">
            <i class="fas fa-sync-alt"></i> Sinkronisasi PDDIKTI
        </a>
    </div>
</div>

<?php if (session()->getFlashdata('pesan')) : ?>
    <div class="alert alert-success">
        <?= session()->getFlashdata('pesan') ?>
    </div>
<?php endif; ?>
<?php if (session()->getFlashdata('error')) : ?>
    <div class="alert alert-danger">
        <?= session()->getFlashdata('error') ?>
    </div>
<?php endif; ?>

<div class="filter-section" style="background: white; padding: 1.5rem; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 1.5rem;">
    <form action="" method="GET" class="filter-form" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap;">
        <div class="form-group" style="flex: 1; min-width: 200px;">
            <label for="search">Pencarian</label>
            <input type="text" name="search" id="search" class="form-control" placeholder="Nama Kurikulum..." value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
        </div>
        <div class="form-group" style="flex: 1; min-width: 200px;">
            <label for="prodi">Program Studi</label>
            <select name="prodi" id="prodi" class="form-control">
                <option value="">-- Semua Program Studi --</option>
                <?php foreach ($list_prodi as $p) : ?>
                    <option value="<?= $p['id'] ?>" <?= (isset($_GET['prodi']) && $_GET['prodi'] == $p['id']) ? 'selected' : '' ?>>
                        <?= esc($p['nama_prodi']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group" style="flex: 0 0 100px;">
            <label for="per_page">Tampilkan</label>
            <select name="per_page" id="per_page" class="form-control">
                <option value="10" <?= (isset($per_page) && $per_page == 10) ? 'selected' : '' ?>>10</option>
                <option value="25" <?= (isset($per_page) && $per_page == 25) ? 'selected' : '' ?>>25</option>
                <option value="50" <?= (isset($per_page) && $per_page == 50) ? 'selected' : '' ?>>50</option>
                <option value="100" <?= (isset($per_page) && $per_page == 100) ? 'selected' : '' ?>>100</option>
            </select>
        </div>
        <div class="form-actions" style="margin-bottom: 4px;">
            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> Filter</button>
            <a href="<?= base_url('kurikulum') ?>" class="btn btn-secondary"><i class="fas fa-undo"></i> Reset</a>
        </div>
    </form>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Nama Kurikulum</th>
                        <th>Program Studi</th>
                        <th>Mulai Berlaku</th>
                        <th>Total SKS Lulus</th>
                        <th>SKS Wajib</th>
                        <th>SKS Pilihan</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($kurikulum)) : ?>
                        <tr>
                            <td colspan="7" class="text-center">Tidak ada data kurikulum. Silakan lakukan sinkronisasi.</td>
                        </tr>
                    <?php else : ?>
                        <?php 
                        $page = isset($_GET['page_kurikulum']) ? $_GET['page_kurikulum'] : 1;
                        $no = ($page - 1) * $per_page + 1;
                        foreach ($kurikulum as $k) : 
                        ?>
                            <tr>
                                <td><?= $no++ ?></td>
                                <td><?= esc($k['nama_kurikulum']) ?></td>
                                <td><?= esc($k['nama_prodi'] ?? $k['nama_program_studi']) ?></td>
                                <td><?= esc($k['mulai_berlaku']) ?></td>
                                <td><?= esc($k['sks_lulus']) ?></td>
                                <td><?= esc($k['sks_wajib']) ?></td>
                                <td><?= esc($k['sks_pilihan']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <div class="pagination-container" style="padding: 1.5rem; border-top: 1px solid #e2e8f0; background: #f8fafc;">
            <?= $pager->links('kurikulum', 'custom_pagination') ?>
        </div>
    </div>
</div>

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