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

$myJSON['success'] = "0";
$myJSON['token']="0";
$myJSON['id']="0";
$myJSON['name']="0";
$myJSON['isAdmin']="0";
$myJSON['username']="0";
$myJSON['company']="وبکام";
$myJSON['unit']="تومان";
$myJSON['change']="0";
$myJSON['vis']="0";
$myJSON['con']="1";
$myJSON['minBuy']="0";
$myJSON['extname']="";
$myJSON['extprice']="0";
$myJSON['holoo']="0";
$myJSON['canmanfi']="0";
$myJSON['tell']="";
$myJSON['cus']=0;
$myJSON['showprice']=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;");

        $unitQ="SELECT name,p_Unit,cchprice,con,vis,minBuy,extname,extprice,holoo,canmanfi,tell,cus,showprice FROM " . $tbl_company . " LIMIT 1";
        $resQ=@mysqli_query($connect,$unitQ);
        if ($resQ) {
          $u = @mysqli_fetch_assoc($resQ);
            $myJSON['unit']= $u["p_Unit"];
              $myJSON['company']= $u["name"];
              $myJSON['change']= $u["cchprice"].""; 
			  $myJSON['con']= $u["con"];
			  $myJSON['minBuy']= $u["minBuy"];
			  $myJSON['vis']= $u["vis"]."";
			   $myJSON['extname']= $u["extname"];
			   $myJSON['extprice']= $u["extprice"];
			   $myJSON['holoo']= $u["holoo"];
			   $myJSON['canmanfi']=$u["canmanfi"];
			   $myJSON['tell']=$u["tell"];
			   $myJSON['cus']=$u["cus"];
			   $myJSON['showprice']=$u["showprice"];
        }

        $my_query="SELECT id,name,isAdmin FROM " . $tbl_admin . " 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['name']=$row["name"];
				$myJSON['isAdmin']=$row["isAdmin"];
                $countofresult++;
            }
            if($countofresult == 0)
			{
                $myJSON['success'] = "404";
			}
            else
			{
				 $myJSON['success']="200";
				 $qu = "UPDATE ".$tbl_admin." SET last_login = '".jdate('Y/m/d')."' WHERE id = '".$user_id."'";
				 @mysqli_query($connect,$qu);
				 $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 = 'A' 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)
					{
						//insert
						$my_query2="INSERT INTO ".$tbl_sessions." (uid,token,type)
						VALUES ('".$user_id."','".$myJSON['token']."','A')";
						$result2=@mysqli_query($connect,$my_query2);
						if (!$result2) {
							$myJSON['success']= "401" ;
						}
					}
					 //update
					else
					{
						$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);
?>
