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

if (isset($_GET['id_guru'])) {
    $id_guru = intval($_GET['id_guru']);

    // Get the image filename before deleting
    $query = "SELECT gambar_guru 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 = $row['gambar_guru'];

        // Delete the record
        $delete_query = "DELETE FROM tb_guru WHERE id_guru = $id_guru";
        if (mysqli_query($host, $delete_query)) {
            // Delete the image file if it exists
            if (!empty($gambar) && file_exists("upload/$gambar")) {
                unlink("upload/$gambar");
            }
            echo "<script>alert('Tenaga pendidikan berhasil dihapus!'); window.location.href='manage_tenaga.php';</script>";
        } else {
            echo "<script>alert('Error: " . mysqli_error($host) . "'); window.location.href='manage_tenaga.php';</script>";
        }
    } else {
        echo "<script>alert('Data tidak ditemukan.'); window.location.href='manage_tenaga.php';</script>";
    }
} else {
    header('Location: manage_tenaga.php');
    exit;
}
?>

