<?php
include ('functions.php');
include ('header.php');
$accept_order = $setting['accept_orders'];

$item_in_cart = get_item_count();
if (!$item_in_cart || !$accept_order) {
    header("Location: index.php");
    exit();
}if (!$item_in_cart) {
    header("Location: index.php");
    exit();
}
?>
<?php
$main_data = mainData(); // array one rawe
$parent_categories = parentCategory(); // array many row
$setting = setting();
$address_data = get_user_address(); //get user address if found 
?>

<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>

<div class="row branches py-5">

    <div class="container">
        <div class="row">

            <div class="col-12 col-sm-4 px-2 px-sm-2 mb-3 float-right">
                <div class="border border-border-Aluminium p-3 w-100" id="order-summary">
                    <h4 class="h5 text-uppercase"><span data-icon="&#xf0a2" class="px-2 d-inline-block"></span> <?php echo $order_summary; ?>  </h4>
                    <?php
                    $cart = get_item_cart()["data"];
                    $vat = $setting['vat']; //vat percentage
                    $discount = $setting['discount'] ? $setting['discount_percentage'] : 0; //disocunt percentage

                    $sum_total = 0;
                    $order_summary = "";
                    foreach ($cart as $key => $row) {
                        $id = $row['cart_id'];
                        $sub_catgeory_id = $row['sub_catgeory_id'];
                        $sub_category_name = $row['sub_category_name'];
                        $price = $row['price'];
                        $notes = $row['notes'];
                        $quantity = $row['quantity'];
                        $status = $row['status'];
                        $user_id = $row['client_id'];
                        $date_added = $row['date_added'];
                        $total_price = number_format($row['price'], 3, '.', '');
                        $sum_total += number_format($total_price, 3, '.', '');

                        $order_summary .= '<p class="py-3 m-0 font-weight-bold text-capitalize ">' . $quantity . 'x ' . $sub_category_name . '<span class="float-right">' . $total_price . ' ' . $currency_name . ' </span></p>';
                    }

                    $discount_value = number_format($discount / 100 * $sum_total, '3', '.', '');
                    $vat_value = number_format($vat / 100 * ($sum_total - $discount_value), '3', '.', '');

                    $sub_total = number_format(($sum_total), 3, '.', '');
                    $sum_total = number_format(($sum_total + $vat_value - $discount_value), 3, '.', '');
                    if ($discount > 0) {
                        $draw_div = '<p class="py-3 m-0 font-weight-bold text-secondary text-capitalize ">' . $discount_name . '(' . $discount . '%) <span class="float-right">' . $discount_value . ' ' . $currency_name . ' </span></p>';
                    }
                    $order_summary .= '  <p class="py-3 m-0 font-weight-bold text-secondary text-capitalize ">' . $sub_total_name . ' <span class="float-right">' . $sub_total . ' ' . $currency_name . ' </span></p>
                                       ' . $draw_div . '
                                        <p class="py-3 m-0 font-weight-bold text-secondary text-capitalize ">' . $vat_name . ' (' . $vat . '%) <span class="float-right">' . $vat_value . ' ' . $currency_name . ' </span></p>
                                        <p class="py-3 m-0 font-weight-bold text-secondary text-capitalize " >' . $delivery_fee_name . '<span class="float-right" id="delivery-fee" >0.000 ' . ' ' . $currency_name . '</span></p>
                                        <p class="py-3 m-0 font-weight-bold text-capitalize ">' . $total_name . ' <span id="cart-total-span" class="float-right">' . $sum_total . ' ' . $currency_name . ' </span></p>
                                     ';
                    echo $order_summary;
                    ?>


                </div>
            </div>

            <div class="col-12 col-sm-8 mb-3 px-2 px-sm-2 float-left">
                <div class="border border-border-Aluminium p-3 w-100">

                    <form class="mb-0" method="POST" action="checkout_confirm.php" >
                        <div class="row py-4">

                            <?php
                            $query = $con->query("SELECT * FROM `delivered_way` WHERE `display` = '1'");
                            while ($row = mysqli_fetch_assoc($query)) {
                                if ($row['id'] == 1) {
                                    ?>
                                    <div class="col-12 col-sm-6 mb-4 px-0 pl-0 pr-sm-2 custom-radio custom-control-inline pl-5">
                                        <input type="radio"  id="customRadioInline11"  value="Delivery" name="order_type" class="form-control custom-control-input">
                                        <label class="custom-control-label font-weight-bold" for="customRadioInline11">&nbsp;<?php
                                            if (isset($_COOKIE['site_lang']) && $_COOKIE['site_lang'] == 'ar') {
                                                echo $row['name'];
                                            } else {

                                                echo $row['name_en'];
                                            }
                                            ?></label>
                                    </div>
                                <?php } else if ($row['id'] == 2) { ?>
                                    <div class="col-12 col-sm-6 mb-4 px-0 pl-0 pr-sm-2 custom-radio custom-control-inline pl-5">
                                        <input type="radio"  id="customRadioInline22"  value="TakeAway" name="order_type" class="form-control custom-control-input">
                                        <label class="custom-control-label font-weight-bold" for="customRadioInline22">&nbsp;<?php
                                            if (isset($_COOKIE['site_lang']) && $_COOKIE['site_lang'] == 'ar') {
                                                echo $row['name'];
                                            } else {

                                                echo $row['name_en'];
                                            }
                                            ?></label>
                                    </div>
                                <?php } else if ($row['id'] == 3) { ?>
                                    <div class="col-12 col-sm-6 mb-4 px-0 pl-0 pr-sm-2 custom-radio custom-control-inline pl-5">
                                        <input type="radio"  id="customRadioInline33"  value="InRestaurant" name="order_type" class="form-control custom-control-input">
                                        <label class="custom-control-label font-weight-bold" for="customRadioInline33">&nbsp;<?php
                                            if (isset($_COOKIE['site_lang']) && $_COOKIE['site_lang'] == 'ar') {
                                                echo $row['name'];
                                            } else {

                                                echo $row['name_en'];
                                            }
                                            ?></label>
                                    </div>

                                <?php } ?>
                            <?php } ?>

                            <h4 class="h5 text-capitalize col-12 px-0"><span data-icon="&#xf2c7" class="px-2 d-inline-block"></span> <?php echo $contact_info_name ?> </h4>
                            <div class="col-12 col-sm-6 mb-4 px-0 pl-0 pr-sm-2">
                                <input type="text" value="<?php echo $address_data['name'] ?: ""; ?>" name="name" required class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom " placeholder="<?= $name_title; ?>">
                            </div>
                            <div class="col-12 col-sm-6 mb-4 px-0">
                                <input type="number" value="<?php echo $address_data['phone'] ?: ""; ?>" name="phone" required class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom " placeholder="<?= $phone_title; ?>">
                            </div>
                            <div class="col-12 col-sm-6 mb-4 px-0">
                                <input type="email" value="<?php echo $address_data['email']; ?>" name="email"  class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom " placeholder="<?= $email_title; ?>">
                            </div>

                            <div class="col-12 mb-4 px-0 deliver_order">
                                <select name="region_id" id="region_id" required class="custom-select p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom option_require">
                                    <option value="" ><?php echo $region_select; ?> </option>
                                    <?php
                                    $regions = getRegions();
                                    $select_option = "";
                                    foreach ($regions as $key => $row) {
                                        $region_id = $row['region_id'];
                                        $region_name = $row['region_name_en'];
                                        $select_option .= "<option value='{$region_id}'>{$region_name}</option>";
                                    }
                                    echo $select_option;
                                    ?>
                                </select>
                            </div>

                            <div class="col-12 col-sm-6 mb-4 px-0 deliver_order">
                                <input type="number" value="<?php echo$address_data['block']; ?>" required name="block" class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom option_require" placeholder="<?php echo $block; ?>">
                            </div>

                            <div class="col-12 col-sm-6 mb-4 px-0 pl-0 pr-sm-2 deliver_order">
                                <input type="number" value="<?php echo $address_data['road']; ?>" required name="road" class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom option_require" placeholder="<?php echo $road; ?>">
                            </div>

                            <div class="col-12 mb-4 px-0 deliver_order">
                                <input type="number" value="<?php echo $address_data['building']; ?>" required name="buidling" class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom option_require" placeholder="<?php echo $building; ?>">
                            </div>

                            <div class="col-12 mb-4 px-0 deliver_order">
                                <input type="text"  name="special_directions"class="form-control p-0 rounded-0 border-left-0 border-right-0 border-top-0 border-bottom option_require" placeholder="<?php echo $notes_form; ?>">
                            </div>

                        </div>
                        <input type="submit" value="<?php echo $continue_name; ?>" name="confirm_order" class="btn btn-danger btn-block text-uppercase"  />

                        <!-- <a href="checkout_confirm.php" class="btn btn-danger btn-block text-uppercase" type="submit">Continue</a> -->
                    </form>


                </div>
            </div>

        </div>
    </div>

</div>

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