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

$id_ruang = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id_ruang <= 0) {
    header('Location: manage_ruangkelas.php');
    exit;
}

$query = "SELECT * FROM tb_ruang WHERE id_ruang = $id_ruang";
$result = mysqli_query($host, $query);
if (!$result || mysqli_num_rows($result) == 0) {
    header('Location: manage_ruangkelas.php');
    exit;
}
$row = mysqli_fetch_assoc($result);
$nama_ruang = htmlspecialchars($row['nama_ruang']);
$kelas = htmlspecialchars($row['kelas']);
$kapasitas = intval($row['kapasitas']);
$fasilitas = htmlspecialchars($row['fasilitas']);

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

    if (!empty($nama_ruang_new) && !empty($kelas_new) && $kapasitas_new > 0) {
        $update_query = "UPDATE tb_ruang SET nama_ruang = '$nama_ruang_new', kelas = '$kelas_new', kapasitas = $kapasitas_new, fasilitas = '$fasilitas_new' WHERE id_ruang = $id_ruang";
        if (mysqli_query($host, $update_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>Edit 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: #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;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .hero-caption {
                padding: 40px 0 80px;
                padding-left: 5%;
                padding-right: 5%;
            }
            .hero-caption h1 {
                font-size: 36px;
            }
            .card-form {
                margin: 20px 5% 40px 15%;
                max-width: 600px;
                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 {
                max-width: 600px;
                margin: 20px 5% 40px 15%;
                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>Edit 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 Kelas</label>
                <input type="text" id="nama_ruang" name="nama_ruang" value="<?php echo $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" <?php if ($kelas == '10') echo 'selected'; ?>>10</option>
                    <option value="11" <?php if ($kelas == '11') echo 'selected'; ?>>11</option>
                    <option value="12" <?php if ($kelas == '12') echo 'selected'; ?>>12</option>
                </select>
            </div>
            <div class="form-group">
                <label for="kapasitas">Kapasitas</label>
                <input type="number" id="kapasitas" name="kapasitas" value="<?php echo $kapasitas; ?>" min="1" required>
            </div>
            <div class="form-group">
                <label for="fasilitas">Fasilitas</label>
                <textarea id="fasilitas" name="fasilitas"><?php echo $fasilitas; ?></textarea>
            </div>
            <button type="submit" class="btn-submit">Update Ruang Kelas</button>
            <a href="manage_ruangkelas.php" class="btn-back">Kembali</a>
        </form>
    </div>
</body>
</html>

