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

$myJSON['success'] = "0";
$myJSON['token']="0";
$myJSON['id']="0";
$myJSON['V_name']="0";
$myJSON['img']="0";
$myJSON['unit']="تومان";
$myJSON['change']="0";
$myJSON['holoo']="0";
$myJSON['canmanfi']="0";
$ses = 0;
$hash = "";
$user_id=0;
if(isset($_POST["uzermane"]) && isset($_POST["wasport"]) && isset($_POST["an1d"]))
{
    $uzername = $_POST["uzermane"];
    $passwor = $_POST["wasport"];

    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
		@mysqli_query($connect,"SET CHARACTER SET utf8;");
		
		$quer = "SELECT id  FROM ".$tbl_sessions." WHERE type = 'V'";
		$reslim=@mysqli_query($connect,$quer);
		if($reslim){
			$count = 0;
			while(@mysqli_fetch_assoc($reslim))
				$count++;
			$ses = $count;
		}
      $unitQ="SELECT p_Unit, cchprice,holoo,canmanfi FROM " . $tbl_company . " LIMIT 1";
      $resQ=@mysqli_query($connect,$unitQ);
      if ($resQ) {
          $u = @mysqli_fetch_assoc($resQ);
          $myJSON['change']= $u["cchprice"]."";
		  $myJSON['unit']= $u["p_Unit"];
		  $myJSON['holoo']=$u["holoo"];
	      $myJSON['canmanfi']=$u["canmanfi"];
      }


        $my_query="SELECT id,V_name,img FROM " . $tbl_visitors . " WHERE username = '" . $uzername ."' AND pass = '".$passwor."' LIMIT 1";
        $result=@mysqli_query($connect,$my_query);
        if (!$result) {
            $myJSON['success']= "400" ;
        }
        else
        {
            $countofresult = 0;
            while($row = @mysqli_fetch_assoc($result)){
                $user_id = $row["id"];
				$myJSON['img']=$row["img"];
				$myJSON['V_name']=$row["V_name"];
                $countofresult++;
            }
            if($countofresult == 0)
			{
                $myJSON['success'] = "404";
			}
            else
			{
				 $myJSON['token'] = md5($uzername.date("Y-m-d h:i:sa"));
				 $myJSON['id']=$user_id;
				 $my_query1="SELECT id FROM ".$tbl_sessions." WHERE uid = '".$user_id."' AND type = 'V' LIMIT 1";
				 $result1=@mysqli_query($connect,$my_query1);
				 if (!$result1) {
					 $myJSON['success']= "401" ;
				 }
				 else
				 {
					$ses_id=0;
					$countofresult =0;
					while($row = @mysqli_fetch_assoc($result1)){
						$ses_id = $row["id"];
						$countofresult++;
					}
					if($countofresult==0)
					{
						if($vislim > $ses){
							//insert
							$myJSON['success']="200";
							$qu = "UPDATE ".$tbl_visitors." SET last_login = '".jdate('Y/m/d')."' WHERE id = '".$user_id."'";
							@mysqli_query($connect,$qu);
							$my_query2="INSERT INTO ".$tbl_sessions." (uid,token,type)
							VALUES ('".$user_id."','".$myJSON['token']."','V')";
							$result2=@mysqli_query($connect,$my_query2);
							if (!$result2) {
								$myJSON['success']= "401" ;
							}
						}
						else{
							$myJSON['success']= "محدودیت در ورود کاربر...!" ;
						}
					}
					 //update
					else
					{
						$myJSON['success']="200";
						$qu = "UPDATE ".$tbl_visitors." SET last_login = '".jdate('Y/m/d')."' WHERE id = '".$user_id."'";
							@mysqli_query($connect,$qu);
						$my_query_edit= "UPDATE ".$tbl_sessions." SET token ='".$myJSON['token']."' WHERE id = '".$ses_id."'";
						$result3=@mysqli_query($connect,$my_query_edit);
						if (!$result3) {
							$myJSON['success']= "401" ;
					}
				}
			}
		}
	}
	}
	 else
		 $myJSON['success']="402";

}

echo json_encode($myJSON);
?>
