<?php
require_once __DIR__ . '/config/config.php';
$id = $_GET['id'];
$data = $conn->query("SELECT * FROM sekolah WHERE id_sekolah=$id")->fetch_assoc();

if(isset($_POST['update'])){
    $stmt = $conn->prepare("
        UPDATE sekolah SET
        npsn=?, nama_sekolah=?, alamat=?, desa=?, kecamatan=?, kabupaten=?,
        provinsi=?, kepala_sekolah=?, nip_kepsek=?, bendahara=?, nip_bendahara=?
        WHERE id_sekolah=?
    ");
    $stmt->bind_param("sssssssssssi",
        $_POST['npsn'],$_POST['nama_sekolah'],$_POST['alamat'],$_POST['desa'],
        $_POST['kecamatan'],$_POST['kabupaten'],$_POST['provinsi'],
        $_POST['kepala_sekolah'],$_POST['nip_kepsek'],
        $_POST['bendahara'],$_POST['nip_bendahara'],$id
    );
    $stmt->execute();
    header("Location: list_sekolah.php");
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Edit Sekolah</title>

    <!-- 🔥 WAJIB: BOOTSTRAP CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- 🔥 CSS TAMBAHAN -->
    <style>
        body{
            background:#f4f6f9;
        }
        .form-card{
            max-width: 900px;
            margin:auto;
            border-radius: 10px;
        }
        .form-card .card-header{
            font-size: 16px;
            font-weight: 600;
        }
        label{
            font-size: 13px;
        }
    </style>
</head>

<body>

<div class="container mt-5">
    <div class="card shadow form-card">
        <div class="card-header bg-primary text-white">
            ✏ Edit Data Sekolah
        </div>

        <div class="card-body">
            <form method="post">
                <div class="row g-3">
                    <?php foreach($data as $k=>$v): if($k!='id_sekolah'): ?>
                        <div class="col-md-6">
                            <label class="form-label">
                                <?= ucwords(str_replace('_',' ',$k)) ?>
                            </label>
                            <input 
                                type="text" 
                                name="<?= $k ?>" 
                                value="<?= htmlspecialchars($v) ?>" 
                                class="form-control"
                            >
                        </div>
                    <?php endif; endforeach ?>
                </div>

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

</body>
</html>
