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

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

$query = "SELECT * FROM tb_siswa WHERE id_siswa = $id_siswa";
$result = mysqli_query($host, $query);
if (!$result || mysqli_num_rows($result) == 0) {
    header('Location: siswa_admin.php');
    exit;
}
$row = mysqli_fetch_assoc($result);
$nama_siswa = htmlspecialchars($row['nama_siswa']);
$kelas = htmlspecialchars($row['kelas']);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_siswa_new = mysqli_real_escape_string($host, $_POST['nama_siswa']);
    $kelas_new = mysqli_real_escape_string($host, $_POST['kelas']);

    if (!empty($nama_siswa_new) && !empty($kelas_new)) {
        $update_query = "UPDATE tb_siswa SET nama_siswa = '$nama_siswa_new', kelas = '$kelas_new' WHERE id_siswa = $id_siswa";
        if (mysqli_query($host, $update_query)) {
            header('Location: siswa_admin.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 Siswa</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: 60%;
            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 {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .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 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>Edit Data Siswa</h1>
    </div>
    <div class="card-form">
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST" action="">
            <div class="form-group">
                <label for="nama_siswa">Nama Siswa</label>
                <input type="text" id="nama_siswa" name="nama_siswa" value="<?php echo $nama_siswa; ?>" required>
            </div>
            <div class="form-group">
                <label for="kelas">Kelas</label>
                <select id="kelas" name="kelas" required>
                    <option value="">Pilih Kelas</option>
                    <option  <?php if ($kelas == '10 rpl a') echo 'selected'; ?>>10 rpl a</option>
                    <option  <?php if ($kelas == '10 rpl b') echo 'selected'; ?>>10 rpl b</option>
                    <option  <?php if ($kelas == '10 bdp') echo 'selected'; ?>>10 bdp</option>
                    <option  <?php if ($kelas == '10 akl') echo 'selected'; ?>>10 akl</option>
                    <option  <?php if ($kelas == '11 rpl') echo 'selected'; ?>>11 rpl</option>
                    <option  <?php if ($kelas == '11 bdp') echo 'selected'; ?>>11 bdp</option>
                    <option  <?php if ($kelas == '11 akl') echo 'selected'; ?>>11 akl</option>
                    <option  <?php if ($kelas == '12 rpl') echo 'selected'; ?>>12 rpl</option>
                    <option  <?php if ($kelas == '12 bdp') echo 'selected'; ?>>12 bdp</option>
                    <option  <?php if ($kelas == '12 akl') echo 'selected'; ?>>12 akl</option>
                </select>
            </div>
            <button type="submit" class="btn-submit">Update Siswa</button>
            <a href="siswa_admin.php" class="btn-back">Kembali</a>
        </form>
    </div>
</body>
</html>

