<?php

/*
 * Following code will list all the products
 */
// array for JSON response
$response = array();

// include db connect class
include("db_connect.php");

// connecting to db
$con = new DB_CONNECT();
$connection = $con->connect();

$connection->query("SET NAMES 'utf8'");

$connection->query("SET CHARACTER SET utf8");

$connection->query("SET SESSION collation_connection = 'utf8_unicode_ci'");

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

header('Content-Type: application/json');


if (isset($_GET['client_id']) && isset($_GET['cart_id'])) {

    $lang = $_GET['lang'];
    $client_id = $_GET['client_id'];
    $client_address_id = $_GET['client_address_id'];
    $cart_id = $_GET['cart_id'];
    $cart_id_all = explode(',', $cart_id);
    $get_order_price = orderPrice($cart_id);
    $response["product"] = array();

    $get_region_id = get_region_id($client_id, $client_address_id);
    $min_order = get_min_order($get_region_id);

    $branch_id = get_branch_from_region_id($get_region_id);
    if ($branch_id == '') {
        $branch_id = get_branch();
    }
    $check_branch_avaliable_delivery = check_branch_avaliable_delivery($branch_id);

    if (!empty($client_address_id)) {

        if ($check_branch_avaliable_delivery == 1) {
            if ($get_order_price >= $min_order) {
                $response["success"] = 1;
                echo json_encode($response);
            } else {
                $response["success"] = 0;
                if ($lang == "ar") {
                    $response["message"] = "عفوا لا يمكن تأكيد الطلب الحد الأدني هو  " . $min_order . " دينار بحريني .";
                } else {
                    $response["message"] = "Sorry, Your order can't be confirmed the minimum order is 
" . $min_order . " BD";
                }
                echo json_encode($response);
            }
        } else {
            $response["success"] = 0;
            if ($lang == "ar") {
                $response["message"] = "عفوا , التوصيل الي المنزل غير متاح الأن";
            } else {
                $response["message"] = "Sorry, delivery to home is not available now"
                ;
            }
            echo json_encode($response);
        }
    } else {

        $response["success"] = 1;
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    if ($lang == "ar") {
        $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";
    } else {
        $response["message"] = "Missing data Please review your data";
    }
    // echoing JSON response
    echo json_encode($response);
}
?>