<?php include 'koneksi.php';
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}

$query = "SELECT * FROM tb_guru WHERE jabatan = 'kepalasekolah'";
$result = mysqli_query($host, $query);
$row = mysqli_fetch_assoc($result);
$nama_kepsek = htmlspecialchars($row['nama_guru']);
$gambar_kepsek = htmlspecialchars($row['gambar_guru']);

$query_paragraf = "SELECT * FROM tb_katakata_kepsek";
$result_paragraf = mysqli_query($host, $query_paragraf);
$row_paragraf = mysqli_fetch_assoc($result_paragraf);
$paragraf1 = htmlspecialchars($row_paragraf['paragraf_1']);
$paragraf2 = htmlspecialchars($row_paragraf['paragraf_2']);
$paragraf3 = htmlspecialchars($row_paragraf['paragraf_3']);
$paragraf4 = htmlspecialchars($row_paragraf['paragraf_4']);
$paragraf5 = htmlspecialchars($row_paragraf['paragraf_5']);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = mysqli_real_escape_string($host, $_POST['nama']);
    $paragraf1 = mysqli_real_escape_string($host, $_POST['paragraf1']);
    $paragraf2 = mysqli_real_escape_string($host, $_POST['paragraf2']);
    $paragraf3 = mysqli_real_escape_string($host, $_POST['paragraf3']);
    $paragraf4 = mysqli_real_escape_string($host, $_POST['paragraf4']);
    $paragraf5 = mysqli_real_escape_string($host, $_POST['paragraf5']);
    $gambar = $gambar_kepsek; // default to existing

    if (!empty($_FILES['gambar']['name'])) {
        $target_dir = "upload/";
        $unique_name = time() . '_' . basename($_FILES["gambar"]["name"]);
        $target_file = $target_dir . $unique_name;
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

        $check = getimagesize($_FILES["gambar"]["tmp_name"]);
        if ($check !== false && in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
            if (move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file)) {
                $gambar = $unique_name;
            }
        }
    }

    $query = "UPDATE tb_guru SET nama_guru='$nama', gambar_guru='$gambar' WHERE jabatan='kepalasekolah'";
    if (mysqli_query($host, $query)) {
        $query_paragraf = "UPDATE tb_katakata_kepsek SET paragraf_1='$paragraf1', paragraf_2='$paragraf2', paragraf_3='$paragraf3', paragraf_4='$paragraf4', paragraf_5='$paragraf5' WHERE id_kepsek=1";
        if (mysqli_query($host, $query_paragraf)) {
            echo "<script>alert('Update berhasil!'); window.location.href='edit_detail_kepsek.php';</script>";
        } else {
            echo "<script>alert('Error updating paragraf: " . mysqli_error($host) . "');</script>";
        }
    } else {
        echo "<script>alert('Error updating guru: " . mysqli_error($host) . "');</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head><link rel="icon" href="upload/bn2.png" type="image/png">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Detail Kepala Sekolah</title>
    <link rel="stylesheet" href="style_admin.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
        }
        .container {
            max-width: 1000px;
            margin: 50px 3% 20px auto;
            padding: 30px;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
        }
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #0037fd, #5b8cffff);
            border-radius: 15px 15px 0 0;
        }
        h1 {
            text-align: center;
            color: #0037fd;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 25px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
            font-size: 16px;
        }
        .current-image {
            max-width: 200px;
            margin-bottom: 20px;
            border: 2px solid #ddd;
            border-radius: 8px;
        }
        input[type="text"], input[type="file"] {
            width: 100%;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
        }
        textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-family: 'Arial', sans-serif;
            font-size: 14px;
            resize: vertical;
        }
        input:focus, textarea:focus {
            outline: none;
            border-color: #0037fd;
            box-shadow: 0 0 5px rgba(0,55,253,0.3);
        }
        button {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
            display: block;
            margin: 0 0 0 auto;
        }
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .note {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .breadcrumb {
            margin: 20px 10% 10px auto;
            max-width: 900px;
            font-size: 14px;
            color: #666;
        }
        .breadcrumb a {
            color: #0037fd;
            text-decoration: none;
        }
        .breadcrumb a:hover {
            text-decoration: underline;
        }
        .breadcrumb span {
            color: #333;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="breadcrumb">
        <a href="dashboard_admin.php">Dashboard</a> > <a href="profil_admin.php">Profil</a> > <span>Edit Detail Kepala Sekolah</span>
    </div>
    <div class="container">
        <h1>Edit Detail Kepala Sekolah</h1>
        <form method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>Gambar Kepala Sekolah Saat Ini:</label>
                <img src="upload/<?php echo $gambar_kepsek; ?>" alt="Kepala Sekolah" class="current-image">
            </div>
            <div class="form-group">
                <label for="gambar">Pilih Gambar Baru :</label>
                <input type="file" id="gambar" name="gambar" accept="image/*">
                <div class="note">Biarkan kosong jika tidak ingin mengubah gambar.</div>
            </div>
            <div class="form-group">
                <label for="nama">Nama Kepala Sekolah:</label>
                <input type="text" id="nama" name="nama" value="<?php echo $nama_kepsek; ?>" required>
            </div>
            <div class="form-group">
                <label for="paragraf1">Paragraf 1:</label>
                <textarea id="paragraf1" name="paragraf1"><?php echo $paragraf1; ?></textarea>
                <div class="note">Biarkan kosong jika tidak ingin ditampilkan.</div>
            </div>
            <div class="form-group">
                <label for="paragraf2">Paragraf 2:</label>
                <textarea id="paragraf2" name="paragraf2"><?php echo $paragraf2; ?></textarea>
                <div class="note">Biarkan kosong jika tidak ingin ditampilkan.</div>
            </div>
            <div class="form-group">
                <label for="paragraf3">Paragraf 3:</label>
                <textarea id="paragraf3" name="paragraf3"><?php echo $paragraf3; ?></textarea>
                <div class="note">Biarkan kosong jika tidak ingin ditampilkan.</div>
            </div>
            <div class="form-group">
                <label for="paragraf4">Paragraf 4:</label>
                <textarea id="paragraf4" name="paragraf4"><?php echo $paragraf4; ?></textarea>
                <div class="note">Biarkan kosong jika tidak ingin ditampilkan.</div>
            </div>
            <div class="form-group">
                <label for="paragraf5">Paragraf 5:</label>
                <textarea id="paragraf5" name="paragraf5"><?php echo $paragraf5; ?></textarea>
                <div class="note">Biarkan kosong jika tidak ingin ditampilkan.</div>
            </div>
            <button type="submit">Update Detail</button>
        </form>
    </div>
</body>
</html>

