<?php
require_once __DIR__ . '/includes/functions.php';
requireRole(['admin']);
$pageTitle = 'Pengaturan Sekolah';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $logoFile = null;
    if (!empty($_FILES['logo']['name'])) {
        $ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);
        $logoFile = 'logo_' . time() . '.' . $ext;
        move_uploaded_file($_FILES['logo']['tmp_name'], __DIR__ . '/assets/uploads/logo/' . $logoFile);
    }

    $params = [
        'nama_sekolah' => trim($_POST['nama_sekolah']),
        'npsn' => trim($_POST['npsn']),
        'alamat' => trim($_POST['alamat']),
        'telp' => trim($_POST['telp']),
        'email' => trim($_POST['email']),
        'website' => trim($_POST['website']),
        'kepala_sekolah' => trim($_POST['kepala_sekolah']),
        'nip_kepala' => trim($_POST['nip_kepala']),
        'bendahara' => trim($_POST['bendahara']),
        'nip_bendahara' => trim($_POST['nip_bendahara']),
    ];

    $sql = "UPDATE pengaturan SET nama_sekolah=:nama_sekolah, npsn=:npsn, alamat=:alamat, telp=:telp, email=:email, website=:website,
            kepala_sekolah=:kepala_sekolah, nip_kepala=:nip_kepala, bendahara=:bendahara, nip_bendahara=:nip_bendahara";
    if ($logoFile) { $sql .= ", logo=:logo"; $params['logo'] = $logoFile; }
    $pdo->prepare($sql)->execute($params);

    flash('success', 'Pengaturan sekolah berhasil disimpan.');
    header('Location: pengaturan.php');
    exit;
}

$pengaturan = $pdo->query("SELECT * FROM pengaturan LIMIT 1")->fetch();

include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';
?>

<h5 class="mb-3">Pengaturan Profil Sekolah</h5>

<div class="card">
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-8">
                    <div class="mb-3"><label class="form-label">Nama Sekolah</label><input type="text" name="nama_sekolah" class="form-control" value="<?= htmlspecialchars($pengaturan['nama_sekolah']) ?>" required></div>
                    <div class="mb-3"><label class="form-label">NPSN</label><input type="text" name="npsn" class="form-control" value="<?= htmlspecialchars($pengaturan['npsn']) ?>"></div>
                    <div class="mb-3"><label class="form-label">Alamat</label><textarea name="alamat" class="form-control" rows="2"><?= htmlspecialchars($pengaturan['alamat']) ?></textarea></div>
                    <div class="row">
                        <div class="col-md-4 mb-3"><label class="form-label">Telepon</label><input type="text" name="telp" class="form-control" value="<?= htmlspecialchars($pengaturan['telp']) ?>"></div>
                        <div class="col-md-4 mb-3"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= htmlspecialchars($pengaturan['email']) ?>"></div>
                        <div class="col-md-4 mb-3"><label class="form-label">Website</label><input type="text" name="website" class="form-control" value="<?= htmlspecialchars($pengaturan['website']) ?>"></div>
                    </div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Logo Sekolah</label>
                    <?php if (!empty($pengaturan['logo'])): ?>
                        <div class="mb-2"><img src="assets/uploads/logo/<?= htmlspecialchars($pengaturan['logo']) ?>" style="height:80px"></div>
                    <?php endif; ?>
                    <input type="file" name="logo" class="form-control">
                </div>
            </div>
            <hr>
            <h6>Penandatangan Laporan</h6>
            <div class="row">
                <div class="col-md-3 mb-3"><label class="form-label">Nama Kepala Sekolah</label><input type="text" name="kepala_sekolah" class="form-control" value="<?= htmlspecialchars($pengaturan['kepala_sekolah']) ?>"></div>
                <div class="col-md-3 mb-3"><label class="form-label">NIP Kepala Sekolah</label><input type="text" name="nip_kepala" class="form-control" value="<?= htmlspecialchars($pengaturan['nip_kepala']) ?>"></div>
                <div class="col-md-3 mb-3"><label class="form-label">Nama Bendahara</label><input type="text" name="bendahara" class="form-control" value="<?= htmlspecialchars($pengaturan['bendahara']) ?>"></div>
                <div class="col-md-3 mb-3"><label class="form-label">NIP Bendahara</label><input type="text" name="nip_bendahara" class="form-control" value="<?= htmlspecialchars($pengaturan['nip_bendahara']) ?>"></div>
            </div>
            <button class="btn btn-primary"><i class="bi bi-save"></i> Simpan Pengaturan</button>
        </form>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
