<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>
Daftar Pegawai
<?= $this->endSection() ?>

<?= $this->section('page_title') ?>
Manajemen Pegawai
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="row">
    <div class="col-lg-12">
        <div class="d-flex justify-content-end mb-4 gap-2">
            <button type="button" class="btn btn-success px-4 shadow-sm fw-bold"
                data-bs-toggle="modal" data-bs-target="#modalImport">
                <i class="bi bi-file-earmark-spreadsheet-fill me-2"></i> IMPORT DATA
            </button>
            <a href="<?= base_url('admin/pegawai/add') ?>" class="btn btn-primary px-4 shadow-sm fw-bold">
                <i class="bi bi-person-plus-fill me-2"></i> TAMBAH PEGAWAI
            </a>
        </div>

        <!-- Modal Import -->
        <div class="modal fade" id="modalImport" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content border-0 shadow-lg rounded-4">
                    <form action="<?= base_url('admin/pegawai/import') ?>" method="POST" enctype="multipart/form-data">
                        <div class="modal-header bg-success text-white p-4 border-0">
                            <h5 class="modal-title fw-bold"><i class="bi bi-upload me-2"></i> Import Massal Pegawai</h5>
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body p-4">
                            <div class="alert alert-info border-0 rounded-4 p-3 mb-4">
                                <div class="fw-bold small mb-1"><i class="bi bi-info-circle-fill me-2"></i> PETUNJUK:
                                </div>
                                <ol class="small mb-0 opacity-75 ps-3">
                                    <li>Gunakan file dengan format <strong>.csv</strong>.</li>
                                    <li>Pastikan urutan kolom sesuai dengan <strong>Template Biodata Lengkap</strong>.
                                    </li>
                                    <li>Format tanggal wajib <strong>YYYY-MM-DD</strong> (Contoh: 1990-12-31).</li>
                                </ol>
                            </div>

                            <label class="form-label small fw-bold">PILIH FILE (CSV/EXCEL)</label>
                            <input type="file" name="file_import" class="form-control px-4 py-2"
                                accept=".csv" required>

                            <div class="mt-4 text-center">
                                <a href="<?= base_url('admin/pegawai/template') ?>"
                                    class="text-decoration-none small text-success fw-bold">
                                    <i class="bi bi-download me-1"></i> Unduh Format Contoh (.csv)
                                </a>
                            </div>
                        </div>
                        <div class="modal-footer p-4 border-0 pt-0">
                            <button type="button" class="btn btn-light px-4"
                                data-bs-dismiss="modal">BATAL</button>
                            <button type="submit" class="btn btn-success px-5 shadow-sm fw-bold">MULAI
                                IMPORT</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <?php if (session()->getFlashdata('success')): ?>
            <div class="alert alert-success px-4" role="alert">
                <?= session()->getFlashdata('success') ?>
            </div>
        <?php endif; ?>

        <div class="glass-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr class="text-secondary small fw-bold opacity-75">
                            <th>#ID</th>
                            <th>NIP & NAMA</th>
                            <th>JABATAN</th>
                            <th>UNIT KERJA</th>
                            <th>USER AKTIF</th>
                            <th class="text-center">AKSI</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($pegawai)): ?>
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">Belum ada data pegawai. Silakan
                                    tambahkan!</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($pegawai as $row): ?>
                                <tr>
                                    <td>#<?= $row['id'] ?></td>
                                    <td>
                                        <div class="fw-bold"><?= $row['nama'] ?></div>
                                        <span class="small text-muted"><?= $row['nip_baru'] ?></span>
                                    </td>
                                    <td>
                                        <div class="small fw-bold"><?= $row['jabatan'] ?></div>
                                        <?php if ($row['jenis_jabatan']): ?>
                                            <div class="badge bg-info bg-opacity-25 text-info small border border-info mb-1"
                                                style="font-size: 0.75rem;"><?= $row['jenis_jabatan'] ?></div>
                                        <?php endif; ?>
                                        <div class="text-muted small"><?= $row['gol_akhir'] ?> (TMT:
                                            <?= $row['tmt_golongan'] ?>)
                                        </div>
                                    </td>
                                    <td class="small"><?= $row['unor'] ?></td>
                                    <td>
                                        <span
                                            class="badge bg-success bg-opacity-25 text-success border border-success">Ready</span>
                                    </td>
                                    <td class="text-center">
                                        <div class="btn-group overflow-hidden shadow-sm">
                                            <a href="<?= base_url('admin/pegawai/edit/' . $row['id']) ?>"
                                                class="btn btn-light btn-sm"><i
                                                    class="bi bi-pencil-square text-primary"></i></a>
                                            <a href="<?= base_url('admin/pegawai/delete/' . $row['id']) ?>"
                                                class="btn btn-light btn-sm"
                                                onclick="return confirm('Hapus data ini? Akun login juga akan ikut terhapus.')"><i
                                                    class="bi bi-trash-fill text-danger"></i></a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>