<?php
$email=$formData->email;
$password=Password($formData->password);
$qlogin = mysqli_query($connect, "select * from users where user_email='$email'");
if (mysqli_num_rows($qlogin)>0){
	$jdata=array('status'=>0,'msg'=>'Your email is already registered! Please Login','wrong'=>'email');
}else{
	$qlogink = mysqli_query($connect, "select * from karyawan where email='$email'");
	if (mysqli_num_rows($qlogink)>0){
		$jdata=array('status'=>0,'msg'=>'Your email is already registered! Please Login','wrong'=>'email');
	}else{
		$q=mysqli_query($connect, "INSERT INTO `users` (`password`, `user_nama`, `user_email`,`user_hp`, `user_tgl`, `user_status`)
									 VALUES ('$password', '".$formData->nama."', '$email','".$formData->no_hp."','$now', '1');");
		if(!$q){
			$jdata = array('status'=>-1,'msg'=>mysqli_error($connect));	
		}else{
			$b=mysqli_fetch_array(mysqli_query($connect, "select * from users where user_email='$email' and password='$password'"));
			$timeout = time()+1800;
			$auth_data = array('email'=>$email,'ua'=>$_SERVER['HTTP_USER_AGENT'],'timeout'=>$timeout);
			$auth = encryptString(json_encode($auth_data),ENCRYPTION_KEY);
			setAuth($connect,$b['ID'],$auth);
			$user = array('email'=>$b['user_email'],'nama'=>$b['user_nama'],'foto'=>$b['foto'],'t'=>1);
			$qt=mysqli_query($connect, "select * from toko where ID_user='$b[ID]'") or die(mysqli_error($connect));
			$toko =array();
			while($bt=mysqli_fetch_array($qt)){
				$toko[]=$bt;
			}
			$udata = array('user'=>$user,'toko'=>$toko,'auth'=>$auth);
			$jdata=array('status'=>1,'msg'=>"Sign up success, Welcome $b[user_nama]",'data'=>$udata);
		}
	}
}