<?php
include 'connect.php';
$order_id = $_POST['order_id'];
$query_email="SELECT UserEmail FROM a_orders WHERE OrderID=$order_id";
$result_email=mysqli_query($con,$query_email);
$row1=mysqli_fetch_assoc($result_email);
$userEmail=$row1['UserEmail'];
$query = "SELECT D_SpecificationID_FK, D_Quantity FROM a_orderdetails WHERE OrderID_FK = $order_id";
$result_q = mysqli_query($con, $query);

while ($row = mysqli_fetch_assoc($result_q)) {
    $speciID = $row['D_SpecificationID_FK'];
    $qty = $row['D_Quantity'];

    $sql1 = "UPDATE a_specifications SET S_quantity = S_quantity + $qty WHERE SpecificationID = $speciID";
    $result1 = mysqli_query($con, $sql1);
}

$sql = "DELETE FROM a_orders WHERE OrderID = $order_id";
$result = mysqli_query($con, $sql);

// if ($result) {
//     echo "Order with ID $order_id has been deleted.";
// } else {
//     echo "Deletion failed: " . mysqli_error($con);
// }
$to = $userEmail; // Replace with the recipient's email address
$subject = "Order Declined";
$message = "Your order has been declined. Please contact our support team for more information.";

$headers = "From: namliyatseti@gmail.com"; // Replace with your email address

// Send the email
if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}
?>
