<?php
require_once ("varables.php");

$myJSON['message'] = "00";
$myJSON['mmm'] = array();
if(isset($_POST["nekot"]) && isset($_POST["an1d"])
	&& isset($_POST["date"])&& isset($_POST["v1zitor"])
	&& isset($_POST["kass"])&& isset($_POST["prise"])
	&& isset($_POST["of"])&& isset($_POST["3ndpr1se"])
	&& isset($_POST["paytyp"])&& isset($_POST["cm"])
		&& isset($_POST["lat"])&& isset($_POST["lng"])
	&& isset($_POST["emer"])&& isset($_POST["json"]))
{

	$postdata = $_POST["json"];
	$dtllist = json_decode($postdata,true);
    $date = $_POST["date"];
    $V_id = $_POST["v1zitor"];
	$C_id = $_POST["kass"];
	$comment = $_POST["cm"];
	$price =  $_POST["prise"];
	$of =  $_POST["of"];
	$emer =  $_POST["emer"];
	$endptice =  $_POST["3ndpr1se"];
	$paytype = $_POST["paytyp"];
	$user_id = $_POST["an1d"];
	$toke = $_POST["nekot"];
	$lat = $_POST["lat"];
	$lng = $_POST["lng"];


    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");
        $query_token = "SELECT id FROM ".$tbl_sessions." WHERE token = '".$toke."' AND uid ='".$user_id."' AND type ='A' LIMIT 1";
		$tokres=@mysqli_query($connect,$query_token);
        if (!$tokres) {
            $myJSON['message']= "400" ;

        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres)){
				//array_push($myJSON['mmm'], $rowt);
                $countoft++;
			}
			if($countoft == 0){
				$myJSON['message']= "خطای اعتبار سنجی، اکانت شما در دستگاه دیگری لاگین شده." ;
			}
			else{
				$inser_q = "INSERT INTO ".$tbl_orders." (date,V_id,C_id,Price,off,endprice,paytype,comment,emergency,visited,orderLat,orderLng)
				VALUES ('".$date."','".$V_id."','".$C_id."','".$price."','".$of."','".$endptice."','".$paytype."','".$comment."','".$emer."','0','".$lat."','".$lng."')";
				$insres=@mysqli_query($connect,$inser_q);
				
				if(!$insres)
					$myJSON['message']= "401" ;
				else
				{
					$order_id = @mysqli_insert_id($connect);
					$myOrderid['id'] = $order_id;
					array_push($myJSON['mmm'], $myOrderid);
				//	array_push($myJSON['mmm'], $order_id);
					for($i=0;$i<sizeof($dtllist);$i++){
						$inserdt_q = "INSERT INTO ".$tbl_orderdtl." (O_id,P_id,count,price,off,total)
							VALUES ('".$order_id."','".$dtllist[$i]["pid"]."','".$dtllist[$i]["count"]."','".$dtllist[$i]["price"]."','".$dtllist[$i]["off"]."','".$dtllist[$i]["total"]."')";
							$insdtres=@mysqli_query($connect,$inserdt_q);
							if(!$insdtres){
								$myJSON['message']= @mysqli_error($connect);
								break;
							}
							$getpro_q = "SELECT Stack FROM ".$tbl_products." WHERE id ='".$dtllist[$i]["pid"]."'";
							$prores=@mysqli_query($connect,$getpro_q);
							if($prores){
								$count=0;
								$stack = 0;
								while($row = @mysqli_fetch_assoc($prores)){
									$count++;
									$stack = $row['Stack'];
								}
								if($count>0){
									$stack = $stack - $dtllist[$i]["count"];
									$updatepro_q = "UPDATE ".$tbl_products." SET Stack = '".$stack."' WHERE id ='".$dtllist[$i]["pid"]."'";
									$upprores=@mysqli_query($connect,$updatepro_q);

								}

							}
					}
					$update_q = "UPDATE ".$tbl_customer." SET last_order = '".$date."' WHERE id ='".$C_id."'";
					$upres=@mysqli_query($connect,$update_q);
					$notifq = "INSERT INTO tbl_notifications (message,type) VALUES ('SEFARESH','A')";
					@mysqli_query($connect,$notifq);
					$myJSON['message']= "200" ;
				}
			}
		}
	}
	 else
		 $myJSON['message']="402";
}
    echo json_encode($myJSON);

?>
