<?php

session_start();


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

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

}


require_once "config.php";



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



if(!$id){

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

}



// GET EXPENSE

$stmt=$pdo->prepare("

SELECT *

FROM expenses

WHERE id=?

");


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


$expense=$stmt->fetch();



if(!$expense){

    die("Expense not found");

}






// UPDATE EXPENSE
// UPDATE EXPENSE


if(isset($_POST['update'])){


$date=$_POST['expense_date'];

$category=$_POST['category_id'];

$vendor=$_POST['vendor_id'];

$description=$_POST['description'];

$total=$_POST['total'];

$payment=$_POST['payment_method'];



// UPDATE DATABASE

$stmt=$pdo->prepare("

UPDATE expenses SET


expense_date=?,

category_id=?,

vendor_id=?,

description=?,

total=?,

payment_method=?


WHERE id=?

");


$stmt->execute([

$date,

$category,

$vendor,

$description,

$total,

$payment,

$id

]);





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


$stmt=$pdo->query("

SELECT *

FROM settings

LIMIT 1

");


$settings=$stmt->fetch();



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

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

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

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

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




// =============================
// EMAIL RECIPIENTS
// =============================


$emails=[];



if(!empty($companyEmail)){

    $emails[]=$companyEmail;

}



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





// =============================
// EMAIL CONTENT
// =============================


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



$message="

<html>

<head>

<style>

body{

font-family:Arial;

background:#f4f7f9;

padding:20px;

}


.box{

max-width:650px;

margin:auto;

background:white;

border-radius:12px;

overflow:hidden;

box-shadow:0 5px 20px #0002;

}


.header{

background:#059669;

color:white;

padding:25px;

text-align:center;

}


.content{

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;

}



.footer{

background:#f8fafc;

padding:15px;

text-align:center;

font-size:12px;

color:#64748b;

}


</style>

</head>


<body>


<div class='box'>


<div class='header'>

<h2>
Expense Updated
</h2>

<p>
".$companyName."
</p>

</div>




<div class='content'>


<p>
An expense record has been updated.
</p>



<table>


<tr>

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

<td>
".$date."
</td>

</tr>



<tr>

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

<td>
".htmlspecialchars($description)."
</td>

</tr>




<tr>

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

<td class='amount'>

".$currency." ".number_format($total)."

</td>

</tr>



<tr>

<td class='label'>
Payment Method
</td>

<td>

".$payment."

</td>

</tr>



<tr>

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

<td>

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

</td>

</tr>



</table>




</div>



<div class='footer'>

<strong>
".$companyName."
</strong>

<br>

".$address."

<br>

".$phone."

<br>

".$companyEmail."

<br><br>

Automated Expense Management Notification

</div>



</div>


</body>

</html>

";





// =============================
// 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@yourdomain.com\r\n";

}





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


foreach($emails as $email){


mail(

$email,

$subject,

$message,

$headers

);


}





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


header("Location: expenses.php");

exit;


}






// Categories

$categories=$pdo->query("

SELECT *

FROM expense_categories

WHERE status=1

ORDER BY category_name

")->fetchAll();





// Vendors

$vendors=$pdo->query("

SELECT *

FROM vendors

WHERE status=1

ORDER BY vendor_name

")->fetchAll();



?>

<!DOCTYPE html>

<html>

<head>


<meta name="viewport"
content="width=device-width,initial-scale=1">


<title>
Edit Expense
</title>



<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">


<link rel="stylesheet" href="assets/css/sidebar.css">


<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">



<style>


*{

margin:0;

padding:0;

box-sizing:border-box;

font-family:Poppins;

}




body{

background:#f4f7f9;

transition:.3s;

}




body.dark{

background:#111827;

color:white;

}




.content{

margin-left:260px;

padding:25px;

}




.topbar{

display:flex;

justify-content:space-between;

align-items:center;

margin-bottom:20px;

}




.mobile-btn{

display:none;

font-size:25px;

cursor:pointer;

color:#065f46;

}



.toggle{

font-size:25px;

cursor:pointer;

color:#059669;

}




.card{

background:white;

padding:25px;

border-radius:15px;

max-width:750px;

box-shadow:0 5px 20px #0002;

}



body.dark .card{

background:#1f2937;

}





.title{

display:flex;

align-items:center;

gap:10px;

color:#065f46;

margin-bottom:20px;

}




.form-grid{


display:grid;

grid-template-columns:1fr 1fr;

gap:15px;


}




.form-group{

margin-bottom:15px;

}




.full{

grid-column:1/-1;

}




label{

font-weight:600;

display:block;

margin-bottom:5px;

}



input,
select,
textarea{


width:100%;

padding:12px;

border-radius:10px;

border:1px solid #ddd;

outline:none;

}



textarea{

height:120px;

resize:none;

}



.buttons{

display:flex;

gap:10px;

margin-top:20px;

}




.btn{


background:#059669;

color:white;

border:none;

padding:12px 20px;

border-radius:10px;

cursor:pointer;

}




.back{


background:#64748b;

color:white;

padding:12px 20px;

border-radius:10px;

text-decoration:none;

}




.overlay{

display:none;

position:fixed;

inset:0;

background:#0008;

z-index:999;

}



.overlay.show{

display:block;

}





@media(max-width:768px){


.sidebar{

left:-280px;

}


.sidebar.active{

left:0;

}




.content{

margin-left:0;

padding:15px;

}



.mobile-btn{

display:block;

}



.form-grid{

grid-template-columns:1fr;

}



.card{

padding:18px;

}




.buttons{

flex-direction:column;

}




.btn,
.back{

width:100%;

text-align:center;

}


}





</style>


</head>



<body>




<?php include "sidebar.php"; ?>






<div class="content">



<div class="topbar">



<i class="fa fa-bars mobile-btn"
onclick="openMenu()"></i>




<h2>

<i class="fa fa-edit"></i>

Edit Expense

</h2>





<i class="fa fa-moon toggle"
onclick="theme()"></i>



</div>







<div class="card">



<div class="title">


<i class="fa fa-file-invoice-dollar"></i>


<h3>
Update Expense
</h3>


</div>





<form method="POST">



<div class="form-grid">





<div class="form-group">


<label>
Expense Date
</label>


<input type="date"
name="expense_date"
value="<?=$expense['expense_date']?>"
required>


</div>







<div class="form-group">


<label>
Amount
</label>


<input type="number"
name="total"
value="<?=$expense['total']?>"
required>


</div>







<div class="form-group">


<label>
Category
</label>



<select name="category_id" required>



<?php foreach($categories as $c){ ?>


<option value="<?=$c['id']?>"

<?=($expense['category_id']==$c['id'])?'selected':''?>

>


<?=$c['category_name']?>


</option>



<?php } ?>



</select>



</div>









<div class="form-group">


<label>
Vendor
</label>


<select name="vendor_id">


<option value="">
Select Vendor
</option>



<?php foreach($vendors as $v){ ?>


<option value="<?=$v['id']?>"

<?=($expense['vendor_id']==$v['id'])?'selected':''?>

>


<?=$v['vendor_name']?>


</option>


<?php } ?>



</select>



</div>









<div class="form-group">


<label>
Payment Method
</label>



<select name="payment_method">


<option <?=($expense['payment_method']=="Cash")?'selected':''?>>
Cash
</option>


<option <?=($expense['payment_method']=="Bank")?'selected':''?>>
Bank
</option>


<option <?=($expense['payment_method']=="Mobile Money")?'selected':''?>>
Mobile Money
</option>


<option <?=($expense['payment_method']=="Card")?'selected':''?>>
Card
</option>



</select>



</div>








<div class="form-group full">


<label>
Description
</label>


<textarea name="description"><?=$expense['description']?></textarea>


</div>




</div>







<div class="buttons">


<button class="btn"
name="update">


<i class="fa fa-save"></i>

Update Expense


</button>



<a href="javascript:history.back()" class="btn">

<i class="fa fa-arrow-left"></i>

Back

</a>



</div>





</form>



</div>



</div>







<script>



function openMenu(){


document.getElementById("sidebar")
.classList.add("active");



document.getElementById("overlay")
.classList.add("show");


}





function closeMenu(){


document.getElementById("sidebar")
.classList.remove("active");



document.getElementById("overlay")
.classList.remove("show");


}





function theme(){


document.body.classList.toggle("dark");


localStorage.setItem(
"dark",
document.body.classList.contains("dark")
);


}



if(localStorage.getItem("dark")=="true"){


document.body.classList.add("dark");


}



</script>




</body>

</html>