<?php
/**
 * @var array $tahun_akademik
 */
?>
<?= $this->extend('layout/default') ?>

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

<div class="card glass">
    <div class="card-header" style="display: flex; justify-content: space-between; align-items: center;">
        <h5 class="m-0" style="color: #3b82f6;">Peraturan Semester Berlaku</h5>
        <a href="<?= base_url('tahun-akademik/sync-pddikti') ?>" class="btn btn-warning btn-sm" onclick="return confirm('Proses ini akan menarik data Semester dari PDDIKTI. Lanjutkan?');">
            <i class='bx bx-refresh'></i> Sinkronisasi Data Semester
        </a>
    </div>
    <div class="card-body">
        
        <?php if(session()->getFlashdata('pesan')) : ?>
            <div class="alert alert-success">
                <?= session()->getFlashdata('pesan'); ?>
            </div>
        <?php endif; ?>

        <form action="<?= base_url('tahun-akademik/set-aktif') ?>" method="post" style="max-width: 600px; margin: 20px auto; padding: 20px; border: 1px solid #e5e7eb; border-radius: 8px; background-color: #f9fafb;">
            <?= csrf_field() ?>
            
            <div class="form-group" style="margin-bottom: 20px;">
                <label for="id_ta" style="display: block; margin-bottom: 8px; font-weight: 500; color: #374151;">Semester Berlaku:</label>
                <div style="position: relative;">
                    <select name="id_ta" id="id_ta" class="form-control" style="width: 100%; padding: 10px 12px; border: 1px solid #0f172a; border-radius: 4px; background-color: white; appearance: none; cursor: pointer; color: #0f172a;">
                        <?php 
                        // Find currently active
                        $activeId = '';
                        foreach($tahun_akademik as $t) {
                            if($t['status'] == 'Aktif') {
                                $activeId = $t['id'];
                                break;
                            }
                        }
                        ?>
                        <?php foreach($tahun_akademik as $t) : ?>
                            <option value="<?= $t['id']; ?>" <?= ($t['id'] == $activeId) ? 'selected' : ''; ?>>
                                <?= $t['nama_ta']; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <!-- Custom arrow icon to match the style -->
                    <div style="position: absolute; right: 12px; top: 50%; transform: translateY(-50%); pointer-events: none;">
                        <i class='bx bxs-down-arrow' style="font-size: 12px; color: #64748b;"></i>
                    </div>
                </div>
            </div>

            <div style="text-align: center; margin-top: 30px;">
                <button type="submit" class="btn btn-info" style="background-color: #38bdf8; border-color: #38bdf8; color: white; padding: 8px 30px; border-radius: 4px; font-weight: 500;">
                    <i class='bx bx-save'></i> SIMPAN
                </button>
            </div>
        </form>

    </div>
</div>

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