<?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["cdi"])	&& isset($_POST["llast"]))
{
	$target_dir = "cusimages/";
	$target_file_name = $target_dir . basename($_FILES["image"]["name"]);
	$imageFileType = pathinfo($target_file_name,PATHINFO_EXTENSION);
	if(basename($_FILES["image"]["name"]) != "")
	if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
		&& $imageFileType != "gif" ){
		$myJSON['message'] = "410";
		 echo  json_encode($myJSON);
		 return;
	}


		$cname =str_replace('"',"",$_POST["cnam"]);
		$shname =str_replace('"',"",$_POST["shname"]);
		$adres =str_replace('"',"",$_POST["adres"]);
		$toke =str_replace('"',"",$_POST["nekot"]);
		$phone = str_replace('"',"",$_POST["phone"]);
		$cid = str_replace('"',"",$_POST["cdi"]);
		$img = "";
		if(basename($_FILES["image"]["name"]) !="")
			$img = $target_file_name;
		$last =str_replace('"',"",$_POST["llast"]);
		$user_id =str_replace('"',"",$_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 ='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']= "405" ;


				}
				else{
					$serchcus_q = "SELECT id FROM ".$tbl_customer." WHERE C_mobile ='".$shname."'";
					$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,C_mobile,address,phone,C_id,last_visit,img)
					VALUES ('".$cname."','".$shname."','".$adres."','".$phone."','".$cid."','".$last."','".$img."')";
					$insres=@mysqli_query($connect,$inser_q);
					if(!$insres){
						$myJSON['message']= @mysqli_error($connect);
					}
				else{
					$myJSON['message']= "200" ;
					if($img!=""){
						$lastid = @mysqli_insert_id($connect);
						$iamgename =  md5($lastid);
						$target_file_name = $target_dir . $iamgename.".". basename($_FILES["image"]["type"]);

						if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file_name)){
								$img = $target_file_name;
								$update_q = "UPDATE ".$tbl_customer." SET img = '".$img."' WHERE id = '".$lastid."'";
								$updateres=@mysqli_query($connect,$update_q);
								if(!$updateres)
				            	$myJSON['message']= "2000";
						}

					}

				}
			}
			else
				$myJSON['message']= "مشتری با این شماره موبایل قبلا ثبت شده است.";
		}
				
	}
}
		}
else
	$myJSON['message']="402";
	//}
}
    echo  json_encode($myJSON);

?>
