<?php

session_start();


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

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

}


require_once "config.php";



// =============================
// GET EXPENSE ID
// =============================

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



if(!$id){

    $_SESSION['error']="Invalid expense ID";

    header("Location: expenses.php");

    exit;

}



try{


// =============================
// GET EXPENSE DETAILS
// =============================


$stmt=$pdo->prepare("

SELECT

e.*,

c.category_name,

v.vendor_name


FROM expenses e


LEFT JOIN expense_categories c

ON e.category_id=c.id


LEFT JOIN vendors v

ON e.vendor_id=v.id


WHERE e.id=?

");


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


$expense=$stmt->fetch();



if(!$expense){


    $_SESSION['error']="Expense record not found";

    header("Location: expenses.php");

    exit;


}





// =============================
// GET COMPANY SETTINGS
// =============================


$stmt=$pdo->query("

SELECT *

FROM settings

LIMIT 1

");


$settings=$stmt->fetch();



$companyName=$settings['company_name'] ?? 'Expense Management System';

$companyEmail=$settings['email'] ?? '';

$companyPhone=$settings['phone'] ?? '';

$companyAddress=$settings['address'] ?? '';

$currency=$settings['currency'] ?? 'MWK';





// =============================
// SEND EMAIL NOTIFICATION
// =============================


$recipients=[];


// company email

if(!empty($companyEmail)){

    $recipients[]=$companyEmail;

}


// GiantPlus email

$recipients[]="giantplusmw@gmail.com";





$subject="Expense Deleted Notification - ".$companyName;




$message="

<!DOCTYPE html>

<html>

<head>

<style>


body{

font-family:Arial,Helvetica,sans-serif;

background:#f4f7f9;

padding:20px;

}


.container{

max-width:650px;

margin:auto;

background:white;

border-radius:12px;

overflow:hidden;

box-shadow:0 5px 20px rgba(0,0,0,.15);

}



.header{

background:#059669;

color:white;

padding:25px;

text-align:center;

}



.header h2{

margin:0;

}



.body{

padding:25px;

}



table{

width:100%;

border-collapse:collapse;

}



td{

padding:12px;

border-bottom:1px solid #eee;

}



.label{

font-weight:bold;

color:#065f46;

}



.amount{

font-size:18px;

font-weight:bold;

color:#059669;

}



.warning{

margin-top:20px;

padding:15px;

background:#fee2e2;

color:#991b1b;

border-radius:8px;

}



.footer{

background:#f8fafc;

padding:20px;

text-align:center;

font-size:12px;

color:#64748b;

}



</style>

</head>


<body>


<div class='container'>


<div class='header'>


<h2>

Expense Deleted

</h2>


<p>

".$companyName."

</p>


</div>




<div class='body'>


<p>

A user has deleted an expense record from the system.

</p>



<table>


<tr>

<td class='label'>
Description
</td>

<td>
".htmlspecialchars($expense['description'])."
</td>

</tr>




<tr>

<td class='label'>
Category
</td>

<td>
".htmlspecialchars($expense['category_name'] ?? 'N/A')."
</td>

</tr>




<tr>

<td class='label'>
Vendor
</td>

<td>
".htmlspecialchars($expense['vendor_name'] ?? 'N/A')."
</td>

</tr>




<tr>

<td class='label'>
Amount
</td>

<td class='amount'>

".$currency." ".number_format($expense['total'])."

</td>

</tr>




<tr>

<td class='label'>
Expense Date
</td>

<td>

".$expense['expense_date']."

</td>

</tr>




<tr>

<td class='label'>
Deleted By
</td>

<td>

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

</td>

</tr>



</table>




<div class='warning'>


<strong>
Important:
</strong>

<br>

This expense transaction has been permanently deleted.


</div>



</div>






<div class='footer'>


<strong>

".$companyName."

</strong>


<br>


".$companyAddress."


<br>


".$companyPhone."


<br>


".$companyEmail."



<br><br>


This is an automated system notification.


</div>



</div>



</body>


</html>

";





// EMAIL HEADERS


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

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


if(!empty($companyEmail)){

    $headers.="From: ".$companyName." <".$companyEmail.">\r\n";

}

else{

    $headers.="From: noreply@giantepos.space\r\n";

}





// SEND EMAIL

foreach($recipients as $recipient){


    mail(

        $recipient,

        $subject,

        $message,

        $headers

    );


}





// =============================
// DELETE EXPENSE
// =============================


$stmt=$pdo->prepare("

DELETE FROM expenses

WHERE id=?

");


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




$_SESSION['success']="Expense deleted successfully";



}

catch(PDOException $e){


$_SESSION['error']="Unable to delete expense";


}





header("Location: expenses.php");

exit;


?>