<?php

    


$response = array();

define('DB_USER', "abomesbah_system"); // db user
define('DB_PASSWORD', "1adAbvX1Bp]j"); // db password (mention your db password here)
define('DB_DATABASE', "abomesbah_system"); // database name
define('DB_SERVER', "localhost"); // db server

date_default_timezone_set('Asia/Bahrain');


$mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);
mysqli_query($mysqli,"SET CHARACTER SET 'utf8'");
mysqli_query($mysqli,"SET SESSION collation_connection ='utf8_unicode_ci'");


if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}


    


// get all products from products table
if (isset($_GET['order_id'],$_GET['order_type'])) {


    $find_item_discount_value = 0 ;

    $order_type=$_GET['order_type'];
    $order_id = $_GET['order_id'];
    $lang = $_GET['lang'];
    
          
    $cart_id_all_yala;


            $sql = "SELECT * FROM `orders` WHERE `order_id`='$order_id' and `mobile_type`='$order_type' ORDER BY `order_id` DESC";
                if ($result = $mysqli -> query($sql)) {
                  while ($row = $result -> fetch_row()) {
                   $cart_id_all_yala = $row[1];
                  }
                  $result -> free_result();
                }


    $response["product"] = array();
    
        $lastchar = substr($cart_id_all_yala, -1);
        if ($lastchar==','){

            $cart_id_all_yala = substr_replace($cart_id_all_yala ,"",-1);
        }



 //echo $cart_id_all_yala;

    $cart_id_all = explode(',', $cart_id_all_yala);
    


    
    foreach ($cart_id_all as $one) {
       
        $comments_response = array();
        
        $query_sql="SELECT *,
        (select  parent_category_id from sub_categories  where sub_categories.sub_category_id=cart.sub_category_id )  group_id
        ,(select sub_category_name from sub_categories where sub_categories.sub_category_id=cart.sub_category_id) as sub_category_name 
        ,(select sub_category_desc from sub_categories where sub_categories.sub_category_id=cart.sub_category_id) as sub_category_desc 
        ,(select sub_category_size_name from sub_categories_size_prices where sub_categories_size_prices.sub_category_size_price_id=cart.size_id) as sub_category_size_name 
        FROM `cart` WHERE `cart_id`=$one  ORDER BY `cart_id` LIMIT 1";

            $row_select;
            if ($result22 = $mysqli -> query($query_sql)) {
              while ($row_2 = $result22 -> fetch_array(MYSQLI_ASSOC)) {
              $row_select=$row_2;
              }
              $result22 -> free_result();
            }
            
            $mysqli -> close();


        $addition_arr_values = array();
        $remove_arr_values = array();

	if ($find_item_discount_value == 1) {
	    $results["item_discount_value"] = $row_select['item_discount_value'] ;  
	    
	}else { $results["item_discount_value"] =0; }


        $results["cart_id"] = $row_select['cart_id'];
        //echo $results["cart_id"];
        
        $results["remove_id"] = $row_select['remove_id'];
        $remove_id = $row_select['remove_id'];
        $results["note"] = $row_select['note'];
        $results["group_id"] = $row_select['group_id'];

        $potato_id = 0;
        $drink_id = 0;
        $type = 0;
        $sub_category_id = $row_select['sub_category_id'];
        
        $results["sub_category_id"] = $row_select['sub_category_id'];
        
        $results["sub_category_image"] = $row_select['sub_category_image'];
        $results["sub_category_desc"] = $row_select['sub_category_desc'];
        $results["sub_category_name"] =$row_select['sub_category_name'];
        $size_id = $row_select['size_id'];
        $results["size_name"] = $row_select['sub_category_size_name'];

        $results["size_price"] = number_format((float) ($row_select['price']), 3, '.', '');
        
        //echo 'CART ID :'.$results["cart_id"] . '  Price: ' .$results["size_price"];

        // $addition_id = $row_select['addition_id'];
        // $lastchar = substr($addition_id, -1);
        // if ($lastchar=','){
        //     $addition_id = substr_replace($addition_id ,"",-1);
        // }
        // if ($addition_id != '') {
        //     $addition_id_all = explode(',', $addition_id);
        //     foreach ($addition_id_all as $one) {
        //     $query_sql="select * from sub_categories_addition_prices where sub_categories_addition_prices.sub_category_addition_price_id=$addition_id_all";
        //     if ($result22 = $mysqli -> query($query_sql)) {
        //       while ($row_2 = $result22 -> fetch_array(MYSQLI_ASSOC)) {
        //       $row_select=$row_2;
        //       $addition["addition_name"]=$row_2['sub_category_addition_name'];
        //      $addition["addition_price"] = number_format((float) ($row_2['sub_category_addition_price']), 3, '.', '');
        //         array_push($addition_arr_values, $addition);
        //       }
        //       $result22 -> free_result();
        //     }
        //     $mysqli -> close();
        //     }
        // }
        //$results["addition"] = $addition_arr_values;
        
        
        // if ($remove_id != '') {
        //     $remove_id_all = explode(',', $remove_id);
        //     foreach ($remove_id_all as $one) {
        //         if ($lang == "ar") {
        //             $remove["remove_name"] = get_remove_name_ar_from_id($one);
        //         } else {
        //             $remove["remove_name"] = get_remove_name_from_id($one);
        //         }
        //         array_push($remove_arr_values, $remove);
        //     }
        // }
        // $results["remove"] = $remove_arr_values;

        $results["quantity"] = $row_select['quantity'];

        $price = $row_select['price'];
        $results["price"] = number_format((float) ($price), 3, '.', '');

        // $result_2 = mysql_query("SELECT * FROM `sub_category_comments` WHERE `sub_category_id`='$sub_category_id' order by comment_id desc ") or die(mysql_error());
        // while ($row_2 = mysql_fetch_array($result_2)) {
        //     $comments = array();
        //     $comments["comment_id"] = $row_2["comment_id"];
        //     $comments["comment"] = $row_2["comment"];
        //     $comments["rate"] = $row_2["rate"];

        //     array_push($comments_response, $comments);
        // }
        // $results["comments"] = $comments_response;
         array_push($response["product"], $results);
        
    }


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