<?php
/**
 * @var string $search
 * @var array $listJenis
 * @var string $jenis
 * @var array $listStatus
 * @var string $status
 * @var int $per_page
 * @var array $dosen
 * @var \CodeIgniter\Pager\Pager $pager
 */
?>
<?= $this->extend('layout/default') ?>

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

<div class="card-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
    <h1 class="page-title" style="margin-bottom: 0;">Data Master Dosen</h1>
    <div>
        <a href="<?= base_url('dosen/create') ?>" class="btn btn-primary" style="margin-right: 10px;">
            <i class='bx bx-plus'></i> Tambah Dosen
        </a>
        <a href="<?= base_url('dosen/sync-pddikti') ?>" class="btn btn-warning" onclick="return confirm('Proses ini akan menarik data Dosen dari PDDIKTI dan menggenerate akun login (Username NIDN/NUPTK, Password YYYYMMDD). Lanjutkan?');">
            <i class='bx bx-refresh'></i> Sinkronisasi PDDIKTI
        </a>
    </div>
</div>

<?php if(session()->getFlashdata('success')) : ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert" 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 alert-dismissible fade show" role="alert" 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="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" id="filterForm">
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; align-items: end;">
            
            <div class="form-group" style="margin: 0;">
                <label style="font-size: 0.85rem; font-weight: 600; color: #475569; margin-bottom: 5px; display: block;">Cari Dosen</label>
                <div style="position: relative;">
                    <i class='bx bx-search' style="position: absolute; left: 10px; top: 10px; color: #94a3b8;"></i>
                    <input type="text" name="search" value="<?= esc($search) ?>" placeholder="Nama, NIDN, atau NUPTK..." style="width: 100%; padding: 8px 10px 8px 30px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.9rem;">
                </div>
            </div>

            <div class="form-group" style="margin: 0;">
                <label style="font-size: 0.85rem; font-weight: 600; color: #475569; margin-bottom: 5px; display: block;">Jenis Dosen</label>
                <select name="jenis" style="width: 100%; padding: 8px 10px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.9rem; background: #fff;" onchange="document.getElementById('filterForm').submit();">
                    <option value="">Semua Jenis</option>
                    <?php foreach($listJenis as $j): ?>
                        <option value="<?= $j['jenis_dosen'] ?>" <?= $jenis == $j['jenis_dosen'] ? 'selected' : '' ?>><?= $j['jenis_dosen'] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group" style="margin: 0;">
                <label style="font-size: 0.85rem; font-weight: 600; color: #475569; margin-bottom: 5px; display: block;">Status Keaktifan</label>
                <select name="status" style="width: 100%; padding: 8px 10px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.9rem; background: #fff;" onchange="document.getElementById('filterForm').submit();">
                    <option value="">Semua Status</option>
                    <?php foreach($listStatus as $s): ?>
                        <option value="<?= $s['status_aktif'] ?>" <?= $status == $s['status_aktif'] ? 'selected' : '' ?>><?= $s['status_aktif'] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group" style="margin: 0;">
                <label style="font-size: 0.85rem; font-weight: 600; color: #475569; margin-bottom: 5px; display: block;">Tampilkan</label>
                <div style="display: flex; gap: 10px;">
                    <select name="per_page" style="width: 80px; padding: 8px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.9rem; background: #fff;" onchange="document.getElementById('filterForm').submit();">
                        <option value="10" <?= $per_page == 10 ? 'selected' : '' ?>>10</option>
                        <option value="25" <?= $per_page == 25 ? 'selected' : '' ?>>25</option>
                        <option value="50" <?= $per_page == 50 ? 'selected' : '' ?>>50</option>
                        <option value="100" <?= $per_page == 100 ? 'selected' : '' ?>>100</option>
                    </select>
                    <button type="submit" class="btn btn-primary" style="padding: 8px 15px;"><i class='bx bx-search-alt-2'></i> Cari</button>
                    <?php if(!empty($search) || !empty($jenis) || !empty($status)): ?>
                        <a href="<?= base_url('dosen') ?>" class="btn btn-light" style="padding: 8px 15px;" title="Reset Filter"><i class='bx bx-x'></i></a>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </form>
</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="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;">NIDN/NUPTK</th>
                    <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">NAMA DOSEN</th>
                    <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">USERNAME</th>
                    <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">JENIS</th>
                    <th style="padding: 1rem; text-align: left; color: #64748b; font-weight: 600; font-size: 0.875rem;">STATUS</th>
                    <th style="padding: 1rem; text-align: center; color: #64748b; font-weight: 600; font-size: 0.875rem;">AKSI</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                $page = isset($_GET['page_dosen']) ? $_GET['page_dosen'] : 1;
                $i = 1 + ($per_page * ($page - 1));
                foreach($dosen as $d) : 
                ?>
                    <tr style="border-bottom: 1px solid #e2e8f0;">
                        <td style="padding: 1rem;"><?= $i++ ?></td>
                        <td style="padding: 1rem; font-weight: 500;">
                            <?php if(!empty($d['nidn']) && !empty($d['nuptk'])): ?>
                                <div><?= $d['nidn'] ?></div>
                                <div style="color: #64748b; font-size: 0.85em;"><?= $d['nuptk'] ?></div>
                            <?php elseif(!empty($d['nidn'])): ?>
                                <?= $d['nidn'] ?>
                            <?php elseif(!empty($d['nuptk'])): ?>
                                <?= $d['nuptk'] ?>
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
                        <td style="padding: 1rem;">
                            <?= trim($d['gelar_depan'] . ' ' . $d['nama_lengkap'] . ' ' . $d['gelar_belakang']) ?>
                        </td>
                        <td style="padding: 1rem;">
                            <?php if($d['username']): ?>
                                <span style="background-color: #e0f2fe; color: #0284c7; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold; font-family: monospace;">
                                    <?= $d['username'] ?>
                                </span>
                            <?php else: ?>
                                <span style="color: #94a3b8; font-style: italic;">Belum ada akun</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding: 1rem;">
                            <?php if($d['jenis_dosen'] == 'Internal'): ?>
                                <span style="background-color: #f1f5f9; color: #475569; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">
                                    Internal
                                </span>
                            <?php else: ?>
                                <span style="background-color: #fef08a; color: #854d0e; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">
                                    Eksternal
                                </span>
                            <?php endif; ?>
                        </td>
                        <td style="padding: 1rem;">
                            <?php 
                            if($d['status_aktif'] == 'Aktif') {
                                $bg = '#d1fae5'; $color = '#059669';
                            } elseif($d['status_aktif'] == 'Cuti') {
                                $bg = '#fef3c7'; $color = '#d97706';
                            } elseif($d['status_aktif'] == 'Tugas Belajar') {
                                $bg = '#e0f2fe'; $color = '#0284c7';
                            } else {
                                $bg = '#fee2e2'; $color = '#dc2626';
                            }
                            ?>
                            <span style="background-color: <?= $bg ?>; color: <?= $color ?>; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: bold;">
                                <?= $d['status_aktif'] ?>
                            </span>
                        </td>
                        <td style="padding: 1rem; text-align: center; white-space: nowrap;">
                            <a href="<?= base_url('dosen/edit/'.$d['id']) ?>" class="btn btn-sm btn-warning" title="Edit Dosen">
                                <i class='bx bx-edit-alt'></i> Edit
                            </a>
                            <a href="<?= base_url('dosen/delete/'.$d['id']) ?>" class="btn btn-sm btn-danger" title="Hapus Dosen" onclick="return confirm('Apakah Anda yakin ingin menghapus dosen ini? Data akun (jika ada) juga akan terhapus.');">
                                <i class='bx bx-trash'></i> Hapus
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if(empty($dosen)) : ?>
                    <tr>
                        <td colspan="7" style="padding: 2rem; text-align: center; color: #64748b;">Belum ada data Dosen. Silakan tambah data atau sinkronisasi dari PDDIKTI.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- Pagination Container -->
    <div style="padding: 1.5rem; border-top: 1px solid #e2e8f0; background: #f8fafc;">
        <?= $pager->links('dosen', 'custom_pagination') ?>
    </div>
</div>

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