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

$myJSON['message'] = "0";
$myJSON['mmm'] = array();
if(isset($_POST["nekot"]) && isset($_POST["an1d"] )
		&& isset($_POST["vrid"])&& isset($_POST["me3od"])
		&& isset($_POST["V_id"])&& isset($_POST["R_di"]))
{
	$toke = $_POST["nekot"];
	$id = $_POST["vrid"];
	$user_id = $_POST["an1d"];
	$Vid = $_POST["V_id"];
	$Rid = $_POST["R_di"];
	$method =  $_POST["me3od"];
    $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{
				if($method == 0){
					$delete_q = "DELETE FROM ".$tbl_visitor_routs." WHERE vrid='".$id."'";
					$updr = "UPDATE ".$tbl_routs." SET visitored = '0' WHERE id = '".$Rid."'";
					$delres=@mysqli_query($connect,$delete_q);
					if(!$delres){
						$myJSON['message']= "404";
					}
					else{
						$myJSON['message']= "200";
						@mysqli_query($connect,$updr);
					}
				}
				else{
					$serch_q = "SELECT vrid FROM ".$tbl_visitor_routs." WHERE V_id ='".$Vid."' AND R_id = '".$Rid."'";
					$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_visitor_routs." (V_id,R_id)
							VALUES ('".$Vid."','".$Rid."')";
							$updr = "UPDATE ".$tbl_routs." SET visitored = '1' WHERE id = '".$Rid."'";
								$insres=@mysqli_query($connect,$insert_Q);
								if(!$insres){
									$myJSON['message']= "404" ;
								}
								else{
									$myJSON['message']= "200" ;
									@mysqli_query($connect,$updr);
								}
						}
						else{
							$myJSON['message']= "444" ;
						}
					}
					else
						$myJSON['message']= "000" ;
				}
			}
		}
	}
	 else
		 $myJSON['message']="402";
}
    echo  json_encode($myJSON);

?>
