<?php
require_once '../../config/database.php';
require_once '../../includes/auth.php';

// Check if ID is provided
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error'] = "ID tidak valid";
    header("Location: index.php");
    exit();
}

$id_barang = intval($_GET['id']);

// Get item data to delete photo
$stmt = $conn->prepare("SELECT foto FROM pdta_inventaris_barang WHERE id_barang = ?");
$stmt->bind_param("i", $id_barang);
$stmt->execute();
$result = $stmt->get_result();
$barang = $result->fetch_assoc();

// Delete item
$stmt = $conn->prepare("DELETE FROM pdta_inventaris_barang WHERE id_barang = ?");
$stmt->bind_param("i", $id_barang);

if ($stmt->execute()) {
    // Delete photo if exists
    if ($barang && $barang['foto'] && file_exists('../../uploads/' . $barang['foto'])) {
        unlink('../../uploads/' . $barang['foto']);
    }
    $_SESSION['success'] = "Data barang berhasil dihapus";
} else {
    $_SESSION['error'] = "Gagal menghapus data barang: " . $conn->error;
}

header("Location: index.php");
exit();
?>