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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
if(isset($_POST["cnam"]) && isset($_POST["an1d"])
	&& isset($_POST["shname"])&& isset($_POST["adres"])
	&& isset($_POST["nekot"])&& isset($_POST["phone"])
	&& isset($_POST["mob1le"])	&& isset($_POST["routid"]))
{

		$cname =$_POST["cnam"];
		$shname =$_POST["shname"];
		$adres =$_POST["adres"];
		$toke =$_POST["nekot"];
		$phone = $_POST["phone"];
		$mobile = $_POST["mob1le"];
		$rout =$_POST["routid"];
		$user_id =$_POST["an1d"];

		$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 ='V' LIMIT 1";
			$tokres=@mysqli_query($connect,$query_token);
			if (!$tokres) {
				$myJSON['message']= "خطای پایگاه داده...!" ;

			}
			else{
				$countoft = 0;
				while($rowt = @mysqli_fetch_assoc($tokres)){
					array_push($myJSON['mmm'], $rowt);
					$countoft++;
				}
				if($countoft == 0){
					$myJSON['message']= "خطای اعتبار سنجی" ;


				}
				else{
					$serchcus_q = "SELECT id FROM ".$tbl_customer." WHERE C_mobile ='".$mobile."'";
					$srchcusres=@mysqli_query($connect,$serchcus_q);
					if(!$srchcusres){
						$myJSON['message']= @mysqli_error($connect);
					}
					else{
						$iidd = 0;
						while($rowt = @mysqli_fetch_assoc($srchcusres))
							$iidd++;
							if($iidd==0){
								$inser_q = "INSERT INTO ".$tbl_customer." (C_name,shop_name,address,phone,C_mobile,C_acc,C_etebar,C_code,lat,lng)
								VALUES ('".$cname."','".$shname."','".$adres."','".$phone."','".$mobile."','0','0','','35.714514','51.392205')";
								$insres=@mysqli_query($connect,$inser_q);
								if(!$insres){
									$myJSON['message']= "خطای ثبت مشتری" ;
								}
								else{
								$cid = @mysqli_insert_id($connect);
								$serch_q = "SELECT rdtid FROM ".$tbl_routdtl." WHERE C_id ='".$cid."' AND R_id = '".$rout."'";
								$qcusrouted = "UPDATE ".$tbl_customer." SET routed = 1 WHERE id ='".$cid."'";
								$serchres=@mysqli_query($connect,$serch_q);
								if($serchres){
									$countoft = 0;
									while($rowt = @mysqli_fetch_assoc($serchres))
										$countoft++;
									if($countoft == 0){
										$insert_Q = "INSERT INTO ".$tbl_routdtl." (C_id,R_id,sort)
										VALUES ('".$cid."','".$rout."','1')";
											$insres=@mysqli_query($connect,$insert_Q);
											if(!$insres){
												$myJSON['message']= "خطای ثبت در مسیر" ;
											}
											else{
												$myJSON['message']= "200" ;
												$ii = @mysqli_query($connect,$qcusrouted);
											}
									}
									else{
										$myJSON['message']= "" ;
									}
								}
								else
									$myJSON['message']= "خطای پایگاه داده" ;

								}
							}
							else{
									$myJSON['message']= "یک مشتری دیگر با این شمار موبایل ثبت نام شده است." ;
							}
					}


				}
			}
		}
		 else
			 $myJSON['message']="خطا در اتصال به پایگاه داده";
	//}
}
    echo  json_encode($myJSON);
?>
