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

$myJSON = 0;
if(isset($_POST["nekot"]) && isset($_POST["json"]))
{

	$postdata = $_POST["json"];
	$customers = json_decode($postdata,true);
	
$lat="35.714514";
$lng = "51.392205";
	$toke = $_POST["nekot"];
if($toke == "sldiskdjgsoewmvmdpekdjghsncusi"){
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");

					for($i=0;$i<sizeof($customers);$i++){
						$select_query = "SELECT id FROM ".$tbl_customer." WHERE C_id ='".$customers[$i]["C_Code"]."'";
							$res =@mysqli_query($connect,$select_query);
							if(!$res){
								$myJSON = 401;
									echo $myJSON;
								return;
							}
							else {
								$count=0;
								while($row = @mysqli_fetch_assoc($res)){
									$count++;
								}
								if($count == 0){
										$insert_query= "INSERT INTO ".$tbl_customer." (C_name,blackList,address,phone,C_id,C_Code,C_mobile,C_acc,C_etebar,lat,lng)
											VALUES ('".$customers[$i]["C_Name"]."','".$customers[$i]["C_BlackList"]."','".$customers[$i]["C_Address"]."','".$customers[$i]["C_Tel"]."','".$customers[$i]["C_Code"]."','".$customers[$i]["C_Code_C"]."','".$customers[$i]["C_Mobile"]."','".$customers[$i]["C_Account"]."','".$customers[$i]["C_Etebar"]."','".$lat."','".$lng."')";
											$insres =@mysqli_query($connect,$insert_query);
											if(!$insres){
											$myJSON = 402;
											  echo  $myJSON;
											return;
										}
								}
								else{
									$update_query= "UPDATE ".$tbl_customer." SET C_mobile = '".$customers[$i]["C_Mobile"]."',blackList = '".$customers[$i]["C_BlackList"]."', C_name = '".$customers[$i]["C_Name"]."', address = '".$customers[$i]["C_Address"]."', phone = '".$customers[$i]["C_Tel"]."',C_Code= '".$customers[$i]["C_Code_C"]."', C_acc = '".$customers[$i]["C_Account"]."', C_etebar = '".$customers[$i]["C_Etebar"]."' WHERE C_id ='".$customers[$i]["C_Code"]."'";

										$upres =@mysqli_query($connect,$update_query);
										if(!$upres){
										$myJSON = 402;
											echo $myJSON;
										return;
								}
							}
				}
			}
				$myJSON = 200;
		}
	}
	 else
		 $myJSON =405;
}
    echo $myJSON;

?>
