<?php

session_start();


if(!isset($_SESSION['user_id'])){

    header("Location: login.php");
    exit;

}


require_once "config.php";



$id=$_GET['id'] ?? 0;



// GET ITEM DETAILS BEFORE DELETE

$stmt=$pdo->prepare("

SELECT *

FROM expense_items

WHERE id=?

");


$stmt->execute([$id]);


$item=$stmt->fetch();



if(!$item){

    header("Location: items.php");
    exit;

}





// DELETE ITEM


$stmt=$pdo->prepare("

DELETE FROM expense_items

WHERE id=?

");


$stmt->execute([$id]);





// ================================
// EMAIL ALERT
// ================================


$to="giantplusmw@gmail.com";


$subject="Item Deleted - Expense Management System";



$message="

An item has been deleted from the Expense Management System.


--------------------------------

Item Details

--------------------------------


Item Name:
".$item['item_name']."


Registration Number:
".$item['registration_no']."


Item Type:
".$item['item_type']."


Status:
".($item['status']==1?'Active':'Inactive')."


Deleted By User ID:
".$_SESSION['user_id']."


Deleted Date:

".date("d M Y H:i:s")."


--------------------------------


System Notification

";





$headers="MIME-Version: 1.0\r\n";

$headers.="Content-type:text/plain;charset=UTF-8\r\n";

$headers.="From: Expense System <noreply@giantepos.space>\r\n";




// SEND EMAIL

@mail(

$to,

$subject,

$message,

$headers

);





// REDIRECT BACK

header("Location: items.php");

exit;



?>