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

$id_guru = isset($_GET['id_guru']) ? (int)$_GET['id_guru'] : 0;

if($id_guru == 0){
    header('Location: manage_guru.php');
    exit;
}

$query = "SELECT * FROM tb_guru WHERE id_guru = $id_guru";
$result = mysqli_query($host, $query);
if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $gambar = htmlspecialchars($row['gambar_guru']);
    $delete_query = "DELETE FROM tb_guru WHERE id_guru = $id_guru";
    if (mysqli_query($host, $delete_query)) {
        if ((!empty($gambar)) && file_exists('upload/' . $gambar)) {
            unlink('upload/' . $gambar);
        }
        echo "<script>alert('Data Guru berhasil dihapus!'); window.location.href='manage_guru.php';</script>";
    }
} else {
    echo "<script>alert('Data guru tidak ditemukan!'); window.location.href='manage_guru.php';</script>";
    exit;
        }
    
?>
