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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_ruang = mysqli_real_escape_string($host, $_POST['nama_ruang']);
    $kelas = mysqli_real_escape_string($host, $_POST['kelas']);
    $kapasitas = intval($_POST['kapasitas']);
    $fasilitas = mysqli_real_escape_string($host, $_POST['fasilitas']);

    if (!empty($nama_ruang) && !empty($kelas) && $kapasitas > 0) {
        $query = "INSERT INTO tb_ruang (nama_ruang, kelas, kapasitas, fasilitas) VALUES ('$nama_ruang', '$kelas', $kapasitas, '$fasilitas')";
        if (mysqli_query($host, $query)) {
            header('Location: manage_ruangkelas.php');
            exit;
        } else {
            $error = "Error: " . mysqli_error($host);
        }
    } else {
        $error = "Nama ruang, kelas, dan kapasitas 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 Ruang Kelas</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 auto;
            background: #fff;
            padding: 40px;
            border-radius: 22px;
            max-width: 900px;
            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 textarea, .form-group select {
            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;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .hero-caption {
                padding: 40px 5% 80px;
                padding-left: 5%;
                padding-right: 5%;
            }
            .hero-caption h1 {
                font-size: 36px;
            }
            .card-form {
                max-width: 600px;
                margin: 20px 5% 40px 15%;
                padding: 30px 20px;
                max-width: none;
            }
            .form-group input, .form-group textarea, .form-group select {
                font-size: 14px;
            }
            .btn-submit, .btn-back {
                padding: 12px 18px;
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {
            .hero-caption {
                padding: 30px 0 60px;
                padding-left: 5%;
                padding-right: 5%;
            }
            .hero-caption h1 {
                font-size: 28px;
            }
            .card-form {
                margin: -40px 5% 20px auto;
                padding: 20px 15px;
            }
            .form-group {
                margin-bottom: 15px;
            }
            .form-group input, .form-group textarea, .form-group select {
                padding: 8px;
                font-size: 14px;
            }
            .form-group textarea {
                height: 80px;
            }
            .btn-submit, .btn-back {
                padding: 10px 16px;
                font-size: 14px;
                margin-left: 0;
                margin-top: 10px;
                display: block;
                width: 100%;
                text-align: center;
            }
            .btn-back {
                margin-top: 10px;
            }
        }
    </style>
</head>
<body>
    <?php require 'navbar_admin.php'; ?>
    <div class="hero-caption">
        <h1>Tambah Ruang Kelas</h1>
    </div>
    <div class="card-form">
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label for="nama_ruang">Nama Ruang</label>
                <input type="text" id="nama_ruang" name="nama_ruang" required>
            </div>
            <div class="form-group">
                <label for="kelas">Kelas</label>
                <select id="kelas" name="kelas" required>
                    <option value="">Pilih Kelas</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                </select>
            </div>
            <div class="form-group">
                <label for="kapasitas">Kapasitas</label>
                <input type="number" id="kapasitas" name="kapasitas" min="1" required>
            </div>
            <div class="form-group">
                <label for="fasilitas">Fasilitas</label>
                <textarea id="fasilitas" name="fasilitas"></textarea>
            </div>
            <button type="submit" class="btn-submit">Tambah Ruang Kelas</button>
            <a href="manage_ruangkelas.php" class="btn-back">Kembali</a>
        </form>
    </div>
</body>
</html>

