<?php
session_start();
require_once __DIR__ . '/config/config.php';

/* =========================
   PROTEKSI LOGIN
========================= */
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    die("Akses ditolak!");
}

if (!isset($_SESSION['role'])) {
    die("Role tidak ditemukan, silakan login ulang.");
}

$role     = $_SESSION['role'];
$username = $_SESSION['username']; // NPSN sekolah

/* =========================
   QUERY SESUAI ROLE
========================= */
if ($role === 'admin') {
    $stmt = $conn->prepare("SELECT * FROM sekolah ORDER BY id_sekolah DESC");
} else {
    $stmt = $conn->prepare("SELECT * FROM sekolah WHERE npsn = ? LIMIT 1");
    $stmt->bind_param("s", $username);
}

$stmt->execute();
$data = $stmt->get_result();
$jumlah_data = $data->num_rows;
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Data Sekolah</title>
<link rel="icon" href="assets/images/logomu.ico">
<!-- BOOTSTRAP -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">

<style>
    body { font-family: "Segoe UI", Arial; }
    .card-header-primary {
        background: #0d6efd;
        color: #fff;
        font-weight: 600;
    }
    .table th {
        text-align: center;
        vertical-align: middle;
        white-space: nowrap;
    }
    .table td {
        vertical-align: middle;
    }
    .btn-aksi {
        width: 32px;
        height: 32px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 6px;
    }
	.table{
    font-size:13px;
    white-space:nowrap;
    margin-bottom:0;
}
</style>
</head>

<body class="bg-light">

<div class="container-fluid mt-4">

    <!-- NAVIGASI -->
    <div class="mb-3">
        <a href="<?= $role==='admin'?'dashboard_admin.php':'dashboard.php' ?>"
           class="btn btn-outline-secondary btn-sm">
            ⬅ Dashboard
        </a>
    </div>

    <!-- CARD -->
    <div class="card shadow-sm">

        <!-- HEADER -->
        <div class="card-header card-header-primary d-flex justify-content-between align-items-center">
            <span>🏫 Data Sekolah</span>

            <?php if ($role === 'admin'): ?>
                <a href="tambah_sekolah.php" class="btn btn-light btn-sm fw-semibold">
                    ➕ Tambah Sekolah
                </a>
            <?php elseif ($jumlah_data == 0): ?>
                <a href="tambah_sekolah.php" class="btn btn-light btn-sm fw-semibold">
                    ➕ Lengkapi Data
                </a>
            <?php endif; ?>
        </div>

        <!-- BODY -->
        <div class="card-body">

            <div class="table-responsive">
                <table class="table table-bordered table-striped table-hover table-sm align-middle w-100">
                    <thead class="table-secondary">
                        <tr>
                            <th>No</th>
                            <th>NPSN</th>
                            <th>Nama Sekolah</th>
                            <th>Alamat</th>
                            <th>Kepala Sekolah</th>
                            <th>Bendahara</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>

                    <tbody>
                    <?php if ($jumlah_data > 0): ?>
                        <?php $no=1; while($r = $data->fetch_assoc()): ?>
                        <tr>
                            <td class="text-center"><?= $no++ ?></td>
                            <td><?= htmlspecialchars($r['npsn']) ?></td>
                            <td><?= htmlspecialchars($r['nama_sekolah']) ?></td>
                            <td><?= htmlspecialchars($r['alamat']) ?></td>
                            <td><?= htmlspecialchars($r['kepala_sekolah']) ?></td>
                            <td><?= htmlspecialchars($r['bendahara']) ?></td>
                            <td class="text-center">

                                <a href="edit_sekolah.php?id=<?= $r['id_sekolah'] ?>"
                                   class="btn btn-sm btn-outline-primary btn-aksi"
                                   title="Edit">
                                   ✏️
                                </a>

                                <?php if ($role === 'admin'): ?>
                                <button
                                    class="btn btn-sm btn-outline-danger btn-aksi"
                                    data-bs-toggle="modal"
                                    data-bs-target="#modalHapus"
                                    data-id="<?= $r['id_sekolah'] ?>"
                                    data-nama="<?= htmlspecialchars($r['nama_sekolah']) ?>"
                                    title="Hapus">
                                    🗑️
                                </button>
                                <?php endif; ?>

                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted">
                                Data sekolah belum tersedia
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>

<!-- =======================
     MODAL HAPUS
======================= -->
<div class="modal fade" id="modalHapus" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content shadow">

      <div class="modal-header bg-danger text-white">
        <h5 class="modal-title">⚠️ Konfirmasi Hapus</h5>
        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
      </div>

      <div class="modal-body text-center">
        <p>Yakin ingin menghapus data sekolah:</p>
        <strong id="namaSekolah" class="text-danger"></strong>
        <p class="text-muted mt-2">Data yang dihapus tidak dapat dikembalikan.</p>
      </div>

      <div class="modal-footer justify-content-center">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
          ❌ Batal
        </button>
        <a href="#" id="btnHapus" class="btn btn-danger">
          🗑️ Ya, Hapus
        </a>
      </div>

    </div>
  </div>
</div>

<!-- JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
const modalHapus = document.getElementById('modalHapus');
modalHapus.addEventListener('show.bs.modal', function (event) {
    const button = event.relatedTarget;
    document.getElementById('namaSekolah').textContent =
        button.getAttribute('data-nama');
    document.getElementById('btnHapus').href =
        'hapus_sekolah.php?id=' + button.getAttribute('data-id');
});
</script>

</body>
</html>

