<?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;
}

// Check if student exists
$query = "SELECT nama_siswa 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']);

// Handle delete
if (isset($_POST['confirm_delete'])) {
    $delete_query = "DELETE FROM tb_siswa WHERE id_siswa = $id_siswa";
    if (mysqli_query($host, $delete_query)) {
        header('Location: siswa_admin.php');
        exit;
    } else {
        $error = "Error: " . mysqli_error($host);
    }
}
?>

<!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>Hapus 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 40px auto;
            background: #fff;
            padding: 40px;
            border-radius: 22px;
            max-width: 600px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            position: relative;
            z-index: 10;
            text-align: center;
        }
        .warning {
            color: #856404;
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .error {
            color: #dc3545;
            margin-bottom: 20px;
        }
        .btn-confirm {
            background-color: #dc3545;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
            transition: background-color 0.3s ease;
        }
        .btn-confirm:hover {
            background-color: #c82333;
        }
        .btn-cancel {
            background-color: #6c757d;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            text-decoration: none;
            display: inline-block;
            transition: background-color 0.3s ease;
        }
        .btn-cancel:hover {
            background-color: #5a6268;
        }
        @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;
            }
            .warning {
                padding: 10px;
            }
            .error {
                margin-bottom: 10px;
            }
            .btn-confirm {
                margin-right: 5px;
            }
            .btn-cancel {
                margin-left: 5px;
            }
        }
    </style>
</head>
<body>
    <?php require 'navbar_admin.php'; ?>
    <div class="hero-caption">
        <h1>Hapus Data Siswa</h1>
    </div>
    <div class="card-form">
        <div class="warning">
            <strong>Perhatian!</strong> Anda akan menghapus data siswa: <strong><?php echo $nama_siswa; ?></strong>.<br>
            Tindakan ini tidak dapat dibatalkan. Apakah Anda yakin ingin melanjutkan?
        </div>
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST" action="">
            <button type="submit" name="confirm_delete" class="btn-confirm">Ya, Hapus</button>
            <a href="siswa_admin.php" class="btn-cancel">Batal</a>
        </form>
    </div>
</body>
</html>

