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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_jurusan = mysqli_real_escape_string($host, $_POST['nama_jurusan']);
    $deskripsi_jurusan = mysqli_real_escape_string($host, $_POST['deskripsi_jurusan']);

    $gambar_jurusan = '';
    if (isset($_FILES['gambar_jurusan']) && $_FILES['gambar_jurusan']['error'] == 0) {
        $allowed = ['jpg', 'jpeg', 'png', 'gif'];
        $filename = $_FILES['gambar_jurusan']['name'];
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

        if (in_array($ext, $allowed)) {
            $new_filename = time() . '_' . uniqid() . '.' . $ext;
            $upload_path = 'upload/' . $new_filename;

            if (move_uploaded_file($_FILES['gambar_jurusan']['tmp_name'], $upload_path)) {
                $gambar_jurusan = $new_filename;
            } else {
                echo "<script>alert('Gagal mengupload gambar!');</script>";
                $gambar_jurusan = '';
            }
        } else {
            echo "<script>alert('Format gambar tidak didukung! Gunakan JPG, PNG, atau GIF.');</script>";
        }
    }

    if (!empty($nama_jurusan) && !empty($deskripsi_jurusan)) {
        $query = "INSERT INTO tb_jurusan (nama_jurusan, deksripsi, gambar_jurusan) VALUES ('$nama_jurusan', '$deskripsi_jurusan', '$gambar_jurusan')";
        if (mysqli_query($host, $query)) {
            echo "<script>alert('Jurusan berhasil ditambahkan!'); window.location.href='manage_jurusan.php';</script>";
        } else {
            echo "<script>alert('Error: " . mysqli_error($host) . "');</script>";
        }
    } else {
        echo "<script>alert('Nama jurusan dan deskripsi harus diisi!');</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>Tambah Jurusan | SMK Budiniah 2</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: 600px;
            margin: 50px auto;
            padding: 30px;
            background: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        h1 {
            text-align: center;
            color: #0037fd;
            margin-bottom: 30px;
            font-size: 28px;
            font-weight: bold;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .btn {
            background: linear-gradient(135deg, #0037fd, #5b8cffff);
            color: #fff;
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,55,253,0.4);
        }
        .btn-secondary {
            background: #6c757d;
            margin-left: 10px;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .form-actions {
            text-align: center;
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <div class="container">
        <h1>Tambah Jurusan Baru</h1>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="nama_jurusan">Nama Jurusan:</label>
                <input type="text" id="nama_jurusan" name="nama_jurusan" required>
            </div>
            <div class="form-group">
                <label for="deskripsi_jurusan">Deskripsi Jurusan:</label>
                <textarea id="deskripsi_jurusan" name="deskripsi_jurusan" required></textarea>
            </div>
            <div class="form-group">
                <label for="gambar_jurusan">Gambar Jurusan:</label>
                <input type="file" id="gambar_jurusan" name="gambar_jurusan" accept="image/*" required>
            </div>
            <div class="form-actions">
                <button type="submit" class="btn">Tambah Jurusan</button>
                <a href="manage_jurusan.php" class="btn btn-secondary">Batal</a>
            </div>
        </form>
    </div>
</body>
</html>

