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

$id_guru = isset($_GET['id_guru']) ? (int)$_GET['id_guru'] : 0;
if ($id_guru <= 0) {
    header('Location: manage_guru.php');
    exit;
}

$query = "SELECT * FROM tb_guru WHERE jabatan='Guru' AND id_guru = $id_guru";
$result = mysqli_query($host, $query);
if (!$result || mysqli_num_rows($result) == 0) {
    header('Location: manage_guru.php');
    exit;
}
$row = mysqli_fetch_assoc($result);
$nama_guru = htmlspecialchars($row['nama_guru']);
$jabatan = htmlspecialchars($row['jabatan']);
$matapelajaran = htmlspecialchars($row['matapelajaran']);
$gambar_guru = htmlspecialchars($row['gambar_guru']);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_guru_new = mysqli_real_escape_string($host, $_POST['nama_guru']);
    $jabatan_new = mysqli_real_escape_string($host, $_POST['jabatan']);
    $matapelajaran_new = mysqli_real_escape_string($host, $_POST['matapelajaran']);
    $gambar_guru_new = $gambar_guru; // default to existing

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

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

    if (!empty($nama_guru_new) && !empty($jabatan_new) && !empty($matapelajaran_new)) {
        $update_query = "UPDATE tb_guru SET nama_guru = '$nama_guru_new', jabatan = '$jabatan_new', matapelajaran = '$matapelajaran_new', gambar_guru = '$gambar_guru_new' WHERE id_guru = $id_guru";
        if (mysqli_query($host, $update_query)) {
            header('Location: manage_guru.php');
            exit;
        } else {
            $error = "Error: " . mysqli_error($host);
        }
    } else {
        $error = "Semua field harus diisi.";
    }
}
?>
<!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 Data Guru</title>
    <style>
        body {
            margin: 0;
            font-family: 'Times New Roman', Times, serif;
            background: #eef5ff;
        }
        .hero-caption {
            width: 100%;
            background: #eef5ff;
            justify-content: center;
            text-align: center;
            padding: 60px 0 120px;
            padding-left: 5%;
            padding-right: 5%;
        }
        .hero-caption h1 {
            margin-bottom: 20px;
            font-size: 50px;
            font-weight: bold;
            text-transform: capitalize;
            color: #131a33;
        }
        .card-form {
            margin: -80px 10% 40px auto;
            background: #fff;
            padding: 40px;
            border-radius: 22px;
            max-width: 1050px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            position: relative;
            z-index: 10;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #131a33;
        }
        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .form-group textarea {
            height: 100px;
            resize: vertical;
        }
        .btn-submit {
            background-color: #28a745;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }
        .btn-submit:hover {
            background-color: #218838;
        }
        .btn-back {
            background-color: #6c757d;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            text-decoration: none;
            display: inline-block;
            margin-left: 10px;
            transition: background-color 0.3s ease;
        }
        .btn-back:hover {
            background-color: #5a6268;
        }
        .error {
            color: #dc3545;
            margin-bottom: 20px;
        }
        @media (max-width: 768px) {
            .hero-caption {
                padding: 40px 5%;
            }
            .hero-caption h1 {
                font-size: 30px;
            }
            .card-form {
                max-width: 600px;
                margin:20px 5% 40px 15%;
                padding: 20px;
            }
            .form-group {
                margin-bottom: 10px;
            }
            .form-group label {
                font-size: 14px;
            }
            .form-group input, .form-group select, .form-group textarea {
                font-size: 14px;
                margin:5px;
            }
            .btn-submit {
                padding: 8px 16px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <?php require "navbar_admin.php" ?>
    <div class="hero-caption">
        <h1>Edit Data Guru</h1>
    </div>
    <div class="card-form">
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="nama_guru">Nama Guru</label>
                <input type="text" id="nama_guru" name="nama_guru" value="<?php echo $nama_guru; ?>" required>
            </div>
            <div class="form-group">
                <label for="jabatan">Jabatan</label>
                <select id="jabatan" name="jabatan" required>
                    <option value="">Pilih Jabatan</option>
                    <option value="Kepala Sekolah" <?php if ($jabatan == 'Kepala Sekolah') echo 'selected'; ?>>Kepala Sekolah</option>
                    <option value="Guru" <?php if ($jabatan == 'Guru') echo 'selected'; ?>>Guru</option>
                </select>
            </div>
            <div class="form-group">
                <label for="matapelajaran">Mata Pelajaran</label>
                <input type="text" id="matapelajaran" name="matapelajaran" value="<?php echo $matapelajaran; ?>" required>
            </div>
            <div class="form-group">
                <label for="gambar_guru">Upload Gambar Guru (Opsional)</label>
                <input type="file" id="gambar_guru" name="gambar_guru" accept="image/*">
                <?php if (!empty($gambar_guru)): ?>
                    <p>Gambar saat ini: <img src="upload/<?php echo $gambar_guru; ?>" alt="Gambar Guru" style="max-width: 100px;"></p>
                <?php endif; ?>
            </div>
            <button type="submit" class="btn-submit">Update Guru</button>
            <a href="manage_guru.php" class="btn-back">Kembali</a>
        </form>
    </div>
</body>
</html>

