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

/* =========================
   PROTEKSI LOGIN
========================= */
if (!isset($_SESSION['login'])) {
    header("Location: index.php");
    exit;
}

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

/* =========================
   SEKOLAH TIDAK BOLEH DUPLIKAT
========================= */
if ($role !== 'admin') {
    $cek = $conn->prepare("SELECT id_sekolah FROM sekolah WHERE npsn = ? LIMIT 1");
    $cek->bind_param("s", $npsn);
    $cek->execute();
    $cek->store_result();

    if ($cek->num_rows > 0) {
        die("❌ Data sekolah sudah ada, silakan edit data!");
    }
}

/* =========================
   SIMPAN DATA
========================= */
if (isset($_POST['simpan'])) {

    // NPSN: admin ambil dari form, sekolah dari session
    $npsn_simpan = ($role === 'admin') ? $_POST['npsn'] : $npsn;

    $stmt = $conn->prepare("
        INSERT INTO sekolah
        (npsn,nama_sekolah,alamat,desa,kecamatan,kabupaten,provinsi,
         kepala_sekolah,nip_kepsek,bendahara,nip_bendahara)
        VALUES (?,?,?,?,?,?,?,?,?,?,?)
    ");

    $stmt->bind_param(
        "sssssssssss",
        $npsn_simpan,
        $_POST['nama_sekolah'],
        $_POST['alamat'],
        $_POST['desa'],
        $_POST['kecamatan'],
        $_POST['kabupaten'],
        $_POST['provinsi'],
        $_POST['kepala_sekolah'],
        $_POST['nip_kepsek'],
        $_POST['bendahara'],
        $_POST['nip_bendahara']
    );

    if ($stmt->execute()) {
        header("Location: list_sekolah.php");
        exit;
    } else {
        die("Gagal menyimpan data: " . $conn->error);
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Tambah Sekolah</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body{background:#f4f6f9}
.form-card{max-width:900px;margin:auto;border-radius:10px}
label{font-size:13px}
</style>
</head>

<body>

<div class="container mt-5">
<div class="card shadow form-card">
<div class="card-header bg-success text-white">
➕ Tambah Data Sekolah
</div>

<div class="card-body">
<form method="post">
<div class="row g-3">

<!-- NPSN -->
<div class="col-md-6">
<label>NPSN</label>
<input type="text"
       name="npsn"
       class="form-control"
       value="<?= htmlspecialchars($npsn) ?>"
       <?= $role !== 'admin' ? 'readonly' : 'required' ?>>
</div>

<?php
$fields = [
    'nama_sekolah','alamat','desa','kecamatan','kabupaten',
    'provinsi','kepala_sekolah','nip_kepsek','bendahara','nip_bendahara'
];
foreach($fields as $f):
?>
<div class="col-md-6">
<label><?= ucwords(str_replace('_',' ',$f)) ?></label>
<input type="text" name="<?= $f ?>" class="form-control" required>
</div>
<?php endforeach ?>

</div>

<div class="mt-4 d-flex gap-2">
<button name="simpan" class="btn btn-success px-4">💾 Simpan</button>
<a href="list_sekolah.php" class="btn btn-outline-secondary">↩ Kembali</a>
</div>

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

</body>
</html>
