<?php
include ('functions.php');
include ('header.php');
// print_r($_COOKIE);


if (isset($_GET['check']) && $_GET['check'] == "credit") {
    $operation_order_id = $_GET['operation_order_id'];
    $payment = $_GET['check'];
    $client_address_id = $_GET['client_address_id']? : "";
    $client_id = $_GET['client_id'];
    $cart_id = get_user_cart($client_id);
    $cart_id_all = explode(',', $cart_id);
    foreach ($cart_id_all as $one) {
        $query_update_cart = $con->query("UPDATE `cart` SET `status`=1 WHERE `cart_id`='$one'");
    }

    $region_id = get_region_id($client_id, $client_address_id);
    $get_charge = get_charge($region_id)? : 0;


    $total_amount = orderPrice($cart_id);


    $setting = setting(); //to get vat and discount
    $discount = $setting['discount'] ? $setting['discount_percentage'] : 0; //disocunt percentage

    $discount_percentage_amount = (($discount / 100) * $total_amount);

    $net_price = number_format((float) ($total_amount - $discount_percentage_amount), 3, '.', '');
    $vat = $setting['vat']; //vat percentage
    $vat_added = (($vat / 100) * $net_price);
    $net_price_after_vat = number_format((float) ($net_price + $vat_added + $get_charge), 3, '.', '');



    $result = $con->query("INSERT INTO `orders`(`cart_id`, `client_id`, `client_address_id`, `total_price`,`vat` ,`vat_percentage`,`charge_cost`, `discount_percentage`, `net_price`, `order_status`, `order_follow`, `payment`, `mobile_type`, `date`)
                                                        VALUES ('$cart_id','$client_id','$client_address_id','$total_amount','$vat_added','$vat','$get_charge','$discount','$net_price_after_vat','0','0','$payment','Web','" . date("Y-m-d H:i:s") . "')");

    $order_id = mysqli_insert_id($con);
    $con->query("UPDATE `payment` SET `order_id`='$order_id' where `operation_order_id`='$operation_order_id' ");


    if ($result) {

        //$to = "";
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        $to = "apps@emcan-group.com";
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        $to = $user_email;
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        header("Location:http://aldahiyabh.com/finish_online_order.php?order_id=" . $order_id);
    } else {
        ?>
        <script type='text/javascript'>
            // console.log("order Finished With Error");
            alert('Order Error, Please Contact Us!');
        </script>

        <?php
    }
}


if (isset($_GET['paymentid']) && $_GET['paymentid'] != '' && $_GET['check'] == "debit") {
    $payment = $_GET['check'];
    $payment_id = $_GET['paymentid'];
    $client_address_id = $_GET['client_address_id']? : "";
    $client_id = $_GET['client_id'];

    $cart_id = get_user_cart($client_id);

    $cart_id_all = explode(',', $cart_id);
    foreach ($cart_id_all as $one) {
        $query_update_cart = $con->query("UPDATE `cart` SET `status`=1 WHERE `cart_id`='$one'");
    }

    $region_id = get_region_id($client_id, $client_address_id);
    $get_charge = get_charge($region_id)? : 0;

    $total_amount = orderPrice($cart_id);

    $setting = setting(); //to get vat and discount
    $discount = $setting['discount'] ? $setting['discount_percentage'] : 0; //disocunt percentage

    $discount_percentage_amount = (($discount / 100) * $total_amount);

    $net_price = number_format((float) ($total_amount - $discount_percentage_amount), 3, '.', '');

    $vat = $setting['vat']; //vat percentage
    $vat_added = (($vat / 100) * $net_price);
    $net_price_after_vat = number_format((float) ($net_price + $vat_added + $get_charge), 3, '.', '');

    $result = $con->query("INSERT INTO `orders`(`cart_id`, `client_id`, `client_address_id`, `total_price`,`vat` ,`vat_percentage`,`charge_cost`, `discount_percentage`, `net_price`, `order_status`, `order_follow`, `payment`, `mobile_type`, `date`)
                                                        VALUES ('$cart_id','$client_id','$client_address_id','$total_amount','$vat_added' ,'$vat','$get_charge','$discount','$net_price_after_vat','0','0','$payment','Web','" . date("Y-m-d H:i:s") . "')");

    if ($result) {

        $order_id = mysqli_insert_id($con);

        $con->query("INSERT INTO payment(client_id,order_id,operation_order_id,result_indicator,payment_id,value,result,payment_type,date) VALUES('$client_id','$order_id','','','$payment_id','$net_price_after_vat','success','debit','" . date("Y-m-d H:i:s") . "')");


        //$to = "";
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        $to = "apps@emcan-group.com";
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        $to = $user_email;
        $subject = "Al Dahiya Restaurant Website - New Order";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        $headers .="From: Al Dahiya Restaurant Website" . " Info@emcan-group.com\r\n" . "Reply-To:Info@emcan-group.com\r\n";
        $message = send_email($order_id);
        mail($to, $subject, $message, $headers);


        header("Location:http://aldahiyabh.com/finish_online_order.php?order_id=" . $order_id);
    } else {
        ?>
        <script type='text/javascript'>
            // console.log("order Finished With Error");
            alert('Order Error, Please Contact Us!');
        </script>

        <?php
    }
}
?>
<style>
    /* Edit Image of Logo and bg Area */
    .top-header .logo-area .logo { background-image: url(<?php echo $main_data['image']; ?>); }
    .bg-area{ background-image: url(<?php echo $main_data['image_backGround']; ?>); }
</style>

<?php if ($_GET['order_id'] && $_GET['order_id'] != '') { ?>
    <div class="row branches pt-5">

        <div class="container py-3 px-0">
            <div class="row" id="checkout-confirm-finish">

                <h3> Your Order In Preperation, We Will deliver Your Order As Soon As Possible</h3> <h2> Order Number #  <?php echo $_GET['order_id']; ?></h2>
            </div>
        </div>

    </div>
<?php } else { ?>
    <div class="row branches pt-5">

        <div class="container py-3 px-0">
            <div class="row" id="checkout-confirm-finish">

                <h3>Order Error, Please Contact Us !</h3>
            </div>
        </div>

    </div>


<?php } ?>
<script type='text/javascript'>

    var link = 'http://aldahiyabh.com';
    setInterval(function () {
        location.href = link
    }, 6000);

</script>

<?php include ('footer.php'); ?>