<style>
.master-wrapper { max-width: 960px; margin: 0 auto; padding: 0 12px; }
.master-card { border-radius: 14px; border: 1.5px solid #e5e7eb; box-shadow: 0 2px 8px rgba(0,0,0,.06); background: #fff; overflow: hidden; }
.master-header { background: linear-gradient(135deg, #1abc9c 0%, #16a085 100%); padding: 16px 22px; display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.master-header h5 { color: #fff; font-size: 1rem; font-weight: 800; margin: 0; display: flex; align-items: center; gap: 8px; }
.master-table th { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: #6b7280; font-weight: 700; padding: 10px 16px; background: #f8fffe; border-bottom: 1.5px solid #e5e7eb; white-space: nowrap; }
.master-table td { padding: 10px 16px; vertical-align: middle; font-size: .84rem; border-bottom: 1px solid #f3f4f6; }
.master-table tbody tr:last-child td { border-bottom: none; }
.master-table tbody tr:hover { background: #f8fffe; }
.badge-aktif { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; border-radius: 999px; padding: 2px 10px; font-size: .72rem; font-weight: 700; }
.badge-nonaktif { background: #f3f4f6; color: #6b7280; border: 1px solid #e5e7eb; border-radius: 999px; padding: 2px 10px; font-size: .72rem; font-weight: 700; }
.btn-act { font-size:.78rem; padding:5px 10px; border-radius:7px; font-weight:600; white-space:nowrap; display:inline-flex; align-items:center; gap:4px; }
.btn-act-edit { background:#fef3c7; border:1px solid #fde68a; color:#92400e; }
.btn-act-edit:hover { background:#fde68a; color:#78350f; }
.btn-act-delete { background:#fee2e2; border:1px solid #fecaca; color:#991b1b; }
.btn-act-delete:hover { background:#fecaca; color:#7f1d1d; }
.btn-act-toggle { background:#e0f2fe; border:1px solid #bae6fd; color:#0284c7; }
.btn-act-toggle:hover { background:#bae6fd; color:#0369a1; }
.empty-state { text-align: center; padding: 40px 20px; color: #9ca3af; }
.modal-content { border-radius: 14px; border: none; box-shadow: 0 8px 32px rgba(0,0,0,.12); }
.form-control:focus, .form-select:focus { border-color: #20c997; box-shadow: 0 0 0 3px rgba(32,201,151,.12); }
.form-control, .form-select { border-radius: 8px; border: 1.5px solid #e5e7eb; font-size: .88rem; padding: 9px 12px; }
.form-label { font-size: .8rem; font-weight: 700; color: #374151; margin-bottom: 4px; }
</style>

<?php if($this->session->flashdata('success')): ?>
<div class="alert alert-success alert-dismissible fade show mx-3 mt-3" style="border-radius:10px; font-size:.84rem; border:1.5px solid #6ee7b7;">
    <i class="bi bi-check-circle me-2"></i><?php echo $this->session->flashdata('success'); ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible fade show mx-3 mt-3" style="border-radius:10px; font-size:.84rem;">
    <i class="bi bi-exclamation-circle me-2"></i><?php echo $this->session->flashdata('error'); ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="container-fluid px-2 px-md-3 py-3">
<div class="master-wrapper">

    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
        <div>
            <div style="font-size:.8rem; color:#6b7280;">
                <a href="<?php echo base_url('dashboard'); ?>" class="text-decoration-none" style="color:#6b7280;">Dashboard</a>
                <i class="bi bi-chevron-right mx-1" style="font-size:.65rem;"></i>
                <span>Master Pendidikan</span>
            </div>
            <h5 class="mb-0 fw-bold text-dark mt-1"><i class="bi bi-mortarboard me-2" style="color:#20c997;"></i>Master Data Pendidikan</h5>
        </div>
        <button class="btn btn-sm fw-bold d-flex align-items-center gap-1"
            style="background:#20c997; color:#fff; border-radius:8px; padding:8px 16px;"
            data-bs-toggle="modal" data-bs-target="#modalTambah">
            <i class="bi bi-plus-circle"></i> Tambah Pendidikan
        </button>
    </div>

    <!-- Card Tabel -->
    <div class="master-card">
        <div class="master-header">
            <h5><i class="bi bi-mortarboard"></i> Daftar Pendidikan</h5>
            <span style="background:rgba(255,255,255,.22); border-radius:8px; padding:4px 12px; color:#fff; font-size:.78rem; font-weight:700;">
                <?php echo count($list); ?> data
            </span>
        </div>
        <?php if (!empty($list)): ?>
        <div class="table-responsive">
            <table class="table master-table mb-0">
                <thead>
                    <tr>
                        <th style="width:50px;">No</th>
                        <th>Nama Pendidikan</th>
                        <th style="width:80px;">Urutan</th>
                        <th style="width:100px;">Status</th>
                        <th class="text-center" style="width:140px;">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                <?php $no = 1; foreach ($list as $row): ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><strong><?php echo htmlspecialchars($row->nama); ?></strong></td>
                    <td><span class="badge" style="background:#f3f4f6; color:#374151; border:1px solid #e5e7eb;"><?php echo $row->urutan; ?></span></td>
                    <td>
                        <?php if($row->status == 1): ?>
                        <span class="badge-aktif"><i class="bi bi-check-circle me-1"></i>Aktif</span>
                        <?php else: ?>
                        <span class="badge-nonaktif"><i class="bi bi-x-circle me-1"></i>Nonaktif</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-center">
                        <div class="d-flex gap-1 justify-content-center">
                            <button class="btn-act btn-act-edit btn"
                                onclick="editData(<?php echo $row->id; ?>, '<?php echo addslashes($row->nama); ?>', <?php echo $row->urutan; ?>, <?php echo $row->status; ?>)">
                                <i class="bi bi-pencil"></i>
                            </button>
                            <a href="<?php echo base_url('master-pendidikan/toggle-status/'.$row->id); ?>"
                               class="btn-act btn-act-toggle btn" title="<?php echo $row->status?'Nonaktifkan':'Aktifkan'; ?>">
                                <i class="bi bi-toggle-<?php echo $row->status?'on':'off'; ?>"></i>
                            </a>
                            <a href="<?php echo base_url('master-pendidikan/hapus/'.$row->id); ?>"
                               class="btn-act btn-act-delete btn"
                               data-confirm="Yakin ingin menghapus '<?php echo addslashes($row->nama); ?>'?"
                               data-title="Hapus Pendidikan" data-type="danger">
                                <i class="bi bi-trash"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="bi bi-mortarboard fs-1 d-block mb-2 opacity-50"></i>
            <div class="fw-semibold mb-1">Belum ada data pendidikan</div>
            <button class="btn btn-sm mt-2" style="background:#20c997; color:#fff; border-radius:8px;"
                data-bs-toggle="modal" data-bs-target="#modalTambah">
                <i class="bi bi-plus-circle me-1"></i>Tambah Sekarang
            </button>
        </div>
        <?php endif; ?>
    </div>

</div>
</div>

<!-- Modal Tambah -->
<div class="modal fade" id="modalTambah" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header" style="background:linear-gradient(135deg,#1abc9c,#16a085);">
                <h6 class="modal-title text-white fw-bold"><i class="bi bi-plus-circle me-2"></i>Tambah Pendidikan</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="<?php echo base_url('master-pendidikan/tambah'); ?>" method="post">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Pendidikan <span class="text-danger">*</span></label>
                        <input type="text" name="nama" class="form-control" required placeholder="cth: S1, SMA/SMK Sederajat..." maxlength="100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Urutan <small class="text-muted">(opsional, untuk sortir tampilan)</small></label>
                        <input type="number" name="urutan" class="form-control" min="0" value="0" placeholder="0">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal" style="border-radius:8px;">Batal</button>
                    <button type="submit" class="btn btn-sm fw-bold" style="background:#20c997; color:#fff; border-radius:8px; padding:7px 20px;">
                        <i class="bi bi-save me-1"></i>Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Modal Edit -->
<div class="modal fade" id="modalEdit" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header" style="background:#fffbeb; border-bottom:1.5px solid #fde68a;">
                <h6 class="modal-title fw-bold" style="color:#92400e;"><i class="bi bi-pencil me-2"></i>Edit Pendidikan</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="formEdit" action="" method="post">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nama Pendidikan <span class="text-danger">*</span></label>
                        <input type="text" name="nama" id="e_nama" class="form-control" required maxlength="100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Urutan</label>
                        <input type="number" name="urutan" id="e_urutan" class="form-control" min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Status</label>
                        <select name="status" id="e_status" class="form-select">
                            <option value="1">Aktif</option>
                            <option value="0">Nonaktif</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal" style="border-radius:8px;">Batal</button>
                    <button type="submit" class="btn btn-sm fw-bold" style="background:#f59e0b; color:#fff; border:none; border-radius:8px; padding:7px 20px;">
                        <i class="bi bi-save me-1"></i>Update
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function editData(id, nama, urutan, status) {
    document.getElementById('e_nama').value   = nama;
    document.getElementById('e_urutan').value = urutan;
    document.getElementById('e_status').value = status;
    document.getElementById('formEdit').action = '<?php echo base_url("master-pendidikan/edit/"); ?>' + id;
    new bootstrap.Modal(document.getElementById('modalEdit')).show();
}
</script>
