<?php
include "bank.php";
session_start();

	if(isset($_GET['token']) && $_GET['token'] !== ""){


$result = $conn->query("SELECT * FROM `s-users` WHERE token = '".$_GET['token']."' ");


		 if ($result->num_rows > 0) {
				// output data of each row
				while($row = $result->fetch_assoc()) {
					//echo print_r($row );
					$noom = explode(":",$row['numbers']);
					$udata = array
					(
						'id'=> $row['id'],
						'name'=> $row['name'],
						'username'=>$row['username'],
						'mood'=>$row['mood'],
						'numbers'=> $noom ,
						'st'=>$row['st'],
						'exp'=>$row['exp'],
						'level'=>$row['level'],
						'token'=>$row['token']
					);
					
					
					$uname = $row['username'];
					$pass =$row['password'];
					$from = $noom[5];
					
				}
				
				//echo time();
				include"home.php";
				
			} else {
				//echo "0 results";
				$data = array("userstats" => "0" ,"msg" => "token is invalide" );
				echo json_encode($data );
			}

	


	}elseif(isset($_GET['login']) &&  $_GET['login'] == "user"){
	$username = $_GET['username'];
	$password = $_GET['password'];
	
		
				$sql = "SELECT * FROM `users` WHERE  uname = '$username' AND pass = '$password'";
				echo $sql;
				$result = $conn->query($sql);

				if ($result->num_rows > 0) {
					// output data of each row
					while($row = $result->fetch_assoc()) {
					   $data = array('stats' => "you are logined!" , 'token' => $row['token']);
						echo json_encode($data );
					}
				} else {
					$data = array('stats' => "user not found!");
						echo json_encode($data );
				}
							


	}elseif(isset($_GET['signup']) && $_GET['signup'] == "newuser" ){
	
		
				$url = "37.130.202.188/services.jspd";
				$username = $_GET['username'];
				$password = $_GET['password'];
				$name = $_GET['name'];
				$company = $_GET['company'];
				$national_id = $_GET['national_id'];
				$certificate_id = $_GET['certificate_id'];
				$tell = $_GET['tell'];
				$mobile = $_GET['mobile'];
				$postalcode = $_GET['postalcode'];
				$email = $_GET['email'];
				$address = $_GET['address'];
		
				$param = array
							(
								'uname'=>'saeed200990',
								'pass'=>'123456',
								'user_uname'=> $username,
								'user_pass'=> $password,
								'name'=> $name,
								'company'=> $company,
								'national_id'=> $national_id,
								'certificate_id'=> $certificate_id,
								'access_id'=>'24143',
								'tell'=> $tell,
								'mobile'=> $mobile,
								'postalcode'=>$postalcode,
								'email'=>$email,
								'address'=> $address,
								'op'=>'newuser'
							);
					
		$handler = curl_init($url);             
		curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($handler, CURLOPT_GETFIELDS, $param);                       
		curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
		$response2 = curl_exec($handler);
		
		$response2 = json_decode($response2);
		$res_code = $response2[0];
		$res_data = $response2[1];
		
		
		//echo $res_data;
		
		if($res_data == "successfull"){
		$st = time();
		$exp = $st + 31536000;
		$token = md5($exp);
		$data = array('stats' => $res_data , 'token' => $token);
		echo json_encode($data );
		// mysql_query("INSERT INTO `s-users`(`id`, `name`, `username`, `password`, `mood`, `numbers`, `st`, `exp`, `level`, `token`) VALUES ('',$name,$username,$password,'0','',$st,$exp,,$token)")
		$username = $_GET['username'];
	$password = $_GET['password'];
	$name = $_GET['name'];
	$company = $_GET['company'];
	$national_id = $_GET['national_id'];
	$certificate_id = $_GET['certificate_id'];
	$tell = $_GET['tell'];
	$mobile = $_GET['mobile'];
	$postalcode = $_GET['postalcode'];
	$email = $_GET['email'];
	$address = $_GET['address'];
	
$sql = "INSERT INTO s-users (name, username, password, mood, numbers, st, exp, level, token)
VALUES ('".$name."','".$username."','".$password."','0','','".$st."','".$exp."','','".$token."')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

	
		}else{
		$data = array('stats' => $res_data );
		echo json_encode($data );
		}
		
		
	}else{
	    $data = array("userstats" => "0" ,"msg" => "please login now!" );
	   	echo json_encode($data );

	}
print_r($_GET);

?>

