<?php
include "classes/class.loader.php";
$db = new db();
$calls = new functions();
$booking_id = $_POST['booking_id'];
$wallet_amount = $calls->get_wallet_amount($_SESSION['user_id']);
$park_price = $calls->get_park_price() * 1.15;
$park_group_price = $calls->get_park_group_price() * 1.15;
$credits = $calls->user_park_credits($_SESSION['user_id']);


?>


<!-- <style>
    body {
        background-color: black;
    }
</style>
<div id="overlay"
    style="display:block;position:fixed;top:0;left:0;width:100%;height:100%;background-color:rgba(0,0,0,0.5);z-index:9999;">
    <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;">
        <p style="color:white;font-size:1.5em;">Loading...</p>
    </div>
</div> -->
<?php

if ($_POST['type'] == 'PARK-GROUP') {

    if (($wallet_amount - $park_group_price) >= 0) {
        $db->query("INSERT INTO `visit_trans`(`amount`,  `user_id`, `booking_id`, `payment_id`,`type`) VALUES ('1','{$_SESSION['user_id']}',$booking_id,-1,'{$_POST['type']}')");
        $db->query("INSERT INTO `visit_trans`(`amount`,  `user_id`, `booking_id`, `payment_id`,`type`) VALUES ('-1','{$_SESSION['user_id']}','$booking_id',0,'{$_POST['type']}')");
        $db->query("INSERT INTO `payments`(`zar`,  `user_id`, `booking_id`) VALUES ('-$park_group_price','{$_SESSION['user_id']}','$booking_id')");
        $db->query("UPDATE bookings SET status = 1 WHERE record_id = '$booking_id'");
        ?>
        <form action="send_confirmation.php" method="post">
            <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
            <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
            <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
            <input type="hidden" name="paid" value="1">
        </form>
        <script>document.forms[0].submit();</script>
        <?php
    } else {
        ?>
        <form action="payment.php" method="post">
            <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
            <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
            <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
            <input type="hidden" name="paid" value="0">
        </form>
        <script>document.forms[0].submit();</script>
        <?php
    }

} else if ($credits < 1) {
    if (($wallet_amount - $park_price) >= 0) {
        $wallet_amount = ($wallet_amount - $park_price);
        $db->query("INSERT INTO `visit_trans`(`amount`,  `user_id`, `booking_id`, `payment_id`,`type`) VALUES ('1','{$_SESSION['user_id']}',$booking_id,-1,'{$_POST['type']}')");
        $db->query("INSERT INTO `visit_trans`(`amount`,  `user_id`, `booking_id`, `payment_id`,`type`) VALUES ('-1','{$_SESSION['user_id']}','$booking_id',0,'{$_POST['type']}')");
        $db->query("INSERT INTO `payments`(`zar`,  `user_id`, `booking_id`) VALUES ('-$park_price','{$_SESSION['user_id']}','$booking_id')");
        if ($_POST['service'] != 0) {
            // get service price
            $service_res = $db->query("SELECT * FROM services WHERE record_id = '{$_POST['service']}'");
            $service = $service_res->fetch_assoc();
            if (($wallet_amount - ($service['price'] * 1.15)) >= 0) {
                $db->query("INSERT INTO `payments`(`zar`,  `user_id`, `booking_id`) VALUES ('" . "-" . ($service['price'] * 1.15) . "','{$_SESSION['user_id']}','$booking_id')");
                $db->query("UPDATE bookings SET status = 1 WHERE record_id = '$booking_id'");
                ?>
                    <form action="send_confirmation.php" method="post">
                        <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                        <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                        <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                        <input type="hidden" name="paid" value="1">
                    </form>
                    <script>document.forms[0].submit();</script>
                <?php
            } else {
                ?>

                    <form action="payment.php" method="post">
                        <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                        <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                        <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                        <input type="hidden" name="service_part_paid" value="<?php echo $_POST['service']; ?>">
                        <input type="hidden" name="paid" value="1">
                    </form>
                    <script>document.forms[0].submit();</script>
                <?php
            }
        } else {
            $db->query("UPDATE bookings SET status = 1 WHERE record_id = '$booking_id'");
            ?>
                <form action="send_confirmation.php" method="post">
                    <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                    <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                    <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                    <input type="hidden" name="paid" value="1">
                </form>
                <script>document.forms[0].submit();</script>
            <?php
        }
    } else {
        ?>
            <form action="payment.php" method="post">
                <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                <input type="hidden" name="paid" value="0">
            </form>
            <script>document.forms[0].submit();</script>
        <?php
    }
} else {
    $db->query("INSERT INTO `visit_trans`(`amount`,  `user_id`, `booking_id`, `payment_id`,`type`) VALUES ('-1','{$_SESSION['user_id']}','$booking_id',0,'{$_POST['type']}')");
    if ($_POST['service'] != 0) {
        // get service price
        $service_res = $db->query("SELECT * FROM services WHERE record_id = '{$_POST['service']}'");
        $service = $service_res->fetch_assoc();
        if (($wallet_amount - ($service['price'] * 1.15)) >= 0) {
            $db->query("INSERT INTO `payments`(`zar`,  `user_id`, `booking_id`) VALUES ('" . "-" . ($service['price'] * 1.15) . "','{$_SESSION['user_id']}','$booking_id')");
            $db->query("UPDATE bookings SET status = 1 WHERE record_id = '$booking_id'");
            ?>
                <form action="send_confirmation.php" method="post">
                    <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                    <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                    <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                    <input type="hidden" name="paid" value="1">
                </form>
                <script>document.forms[0].submit();</script>
            <?php
        } else {
            ?>

                <form action="payment.php" method="post">
                    <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                    <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                    <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                    <input type="hidden" name="service_part_paid" value="<?php echo $_POST['service']; ?>">
                    <input type="hidden" name="paid" value="1">
                </form>
                <script>document.forms[0].submit();</script>
            <?php
        }
    } else {
        $db->query("UPDATE bookings SET status = 1 WHERE record_id = '$booking_id'");
        ?>

            <form action="send_confirmation.php" method="post">
                <input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
                <input type="hidden" name="type" value="<?php echo $_POST['type']; ?>">
                <input type="hidden" name="service" value="<?php echo $_POST['service']; ?>">
                <input type="hidden" name="paid" value="1">
            </form>
            <script>document.forms[0].submit();</script>
    <?php }


}