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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_guru = mysqli_real_escape_string($host, $_POST['nama_guru']);
    $jabatan = mysqli_real_escape_string($host, $_POST['jabatan']);
    $matapelajaran = mysqli_real_escape_string($host, $_POST['matapelajaran']);
    $gambar_guru = '';

    if (!empty($_FILES['gambar_guru']['name'])) {
        $target_dir = "upload/";
        $target_file = $target_dir . basename($_FILES["gambar_guru"]["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 = basename($_FILES["gambar_guru"]["name"]);
            }
        }
    }

    if (!empty($nama_guru) && !empty($jabatan) && !empty($matapelajaran)) {
        $query = "INSERT INTO tb_guru (nama_guru, jabatan, matapelajaran, gambar_guru) VALUES ('$nama_guru', '$jabatan', '$matapelajaran', '$gambar_guru')";
        if (mysqli_query($host, $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>Tambah 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: #0037fd;
            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: #0026b3;
        }
        .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 label {
                font-size: 14px;
            }
            .form-group input, .form-group select {
                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>Tambah 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" required>
            </div>
            <div class="form-group">
                <label for="jabatan">Jabatan</label>
                <select id="jabatan" name="jabatan" required>
                    <option value="">Pilih Jabatan</option>
                    <option value="Guru">Guru</option>
                </select>
            </div>
            <div class="form-group">
                <label for="matapelajaran">Mata Pelajaran</label>
                <input type="text" id="matapelajaran" name="matapelajaran" required>
            </div>
            <div class="form-group">
                <label for="gambar_guru">Upload Gambar Guru</label>
                <input type="file" id="gambar_guru" name="gambar_guru" accept="image/*">
            </div>
            <button type="submit" class="btn-submit">Tambah Guru</button>
            <a href="manage_guru.php" class="btn-back">Kembali</a>
        </form>
    </div>
</body>
</html>

