<?php
include "classes/class.loader.php";
$db = new db();
$calls = new functions();

$db->query("UPDATE `bookings` SET `status` = 3 WHERE `record_id` = '{$_GET['record_id']}'");
$db->query("DELETE FROM `visit_trans` WHERE booking_id = '{$_GET['record_id']}'");
$db->query("DELETE FROM `payments` WHERE booking_id = '{$_GET['record_id']}'");
// check if the booking has a service attached 
$service_res = $db->query("SELECT * FROM `bookings` WHERE `record_id` = '{$_GET['record_id']}' LIMIT 1");
$service = $service_res->fetch_assoc();

// if ($service['service'] > 0) {
//     // get service details
//     $service_res = $db->query("SELECT * FROM `services` WHERE `record_id` = '{$service['service']}' LIMIT 1");
//     $service = $service_res->fetch_assoc();
//     $db->query("INSERT INTO payments (`user_id`,`zar`,`booking_id`) VALUES ('{$_SESSION['user_id']}','".($service['price'] * 1.15)."','{$_GET['record_id']}')");
// }
$booking = $db->query("SELECT * FROM `bookings` WHERE `record_id` = '{$_GET['record_id']}' LIMIT 1");

$booking = $booking->fetch_assoc();
$email = new email();
$email->add_address($calls->get_user_email($booking['user_id']), $calls->get_username($booking['user_id']));
$email->add_address("walruslector@gamil.com", "Jana");
$email->add_address("unleashed.offleashed@gamil.com", "Rika");
$email->subject("Unleashed - Booking Declined");
$email->body("<html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; }
                    h1 { color: #333; }
                    p { color: #555; }
                </style>
            </head>
            <body>
                <h1>Dear Customer,</h1>
                <p>Your booking has been canceled, Please rescedule on the app.</p>
                <p>Ballance has been credited to your account.</p>
                <p>If you have any questions, feel free to contact our support team.</p>
                <p>Best regards,</p>
                <p>The Unleashed Team</p>
            </body>
        </html>");
$email->send();
header("location: home.php");