<?php
include 'db.php';

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

    $sqlSelect = "SELECT initialPhoto, updatedPhoto, photo_perlakuan, photo_verifikasi FROM Report WHERE id = ?";
    $stmtSelect = $conn->prepare($sqlSelect);
    $stmtSelect->bind_param("s", $id);
    $stmtSelect->execute();
    $stmtSelect->bind_result($initialPhotoPath, $updatedPhotoPath, $photoPerlakuanPath, $photoVerifikasiPath);
    $stmtSelect->fetch();
    $stmtSelect->close();

    if ($initialPhotoPath && file_exists($initialPhotoPath)) {
        unlink($initialPhotoPath);
    }
    if ($updatedPhotoPath && file_exists($updatedPhotoPath)) {
        unlink($updatedPhotoPath);
    }
    if ($photoPerlakuanPath && file_exists($photoPerlakuanPath)) {
        unlink($photoPerlakuanPath);
    }
    if ($photoVerifikasiPath && file_exists($photoVerifikasiPath)) {
        unlink($photoVerifikasiPath);
    }

    $sqlDelete = "DELETE FROM Report WHERE id = ?";
    $stmtDelete = $conn->prepare($sqlDelete);
    $stmtDelete->bind_param("s", $id);

    if ($stmtDelete->execute()) {
        header("Location: index.php?active=kerusakan");
        exit();
    } else {
        echo "Error deleting record: " . $conn->error;
    }

    $stmtDelete->close();
    $conn->close();
}
?>