<?php
$email=$formData->email;
$password=Password($formData->password);
$qlogin = mysqli_query($connect, "select * from users where user_email='$email'");
if(!$qlogin){
	$jdata = array('status'=>-1,'msg'=>mysqli_error($connect));	
}else{
	if (mysqli_num_rows($qlogin)>0){
		$b=mysqli_fetch_array($qlogin);
		if($b['password']==$password){
			if($b['user_status']=='1'){
				$us = $b[0];
				$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('hp'=>$b['user_hp'],'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_assoc($qt)){
					$toko[]=$bt;
				}
				$qt=mysqli_query($connect, "select a.*,b.stok,b.ID_toko from produk a, produk_stok_toko b where b.ID_produk=a.ID and b.ID_user=a.ID_user and a.ID_user='$b[ID]'") or die(mysqli_error($connect));
				$produk =array();
				while($bt=mysqli_fetch_assoc($qt)){
					$produk[]=$bt;
				}
				$qt=mysqli_query($connect, "select a.nama,a.email,a.no_hp,a.foto,a.status from karyawan a where a.ID_user='$b[ID]'") or die(mysqli_error($connect));
				$kasir = array();
				while($bt=mysqli_fetch_assoc($qt)){
					$qtk = mysqli_query($connect, "select b.nama_toko from karyawan_toko a, toko b where a.ID_toko=b.ID and a.ID_karyawan='$bt[ID]' and a.ID_user='$b[ID]'");
					$bt['access_toko']=array();
					while($btk=mysqli_fetch_assoc($qtk)){
						$bt['access_toko'][] = $btk;
					}
					$kasir[]=$bt;
				}
				$qt=mysqli_query($connect, "select * from karyawan_toko a where a.ID_user='$b[ID]'") or die(mysqli_error($connect));
				$kasir_toko =array();
				while($bt=mysqli_fetch_assoc($qt)){
					$kasir_toko[]=$bt;
				}
				$qt=mysqli_query($connect, "select a.*,b.nama as nama_kasir,c.nama_toko,c.alamat_toko from transaksi a, karyawan b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.email and a.ID_user=b.ID_user and a.ID_user='$b[ID]'") or die(mysqli_error($connect));
				$trx =array();
				while($bt=mysqli_fetch_assoc($qt)){
					$bt['by']=2;
					$bt['detail']=array();
					$qd=mysqli_query($connect, "select a.*,b.nama_produk from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='$b[ID]' and ID_transaksi='$bt[id_]'");
					while($bd=mysqli_fetch_assoc($qd)){
						$bt['detail'][]=$bd;
					}
					$trx[]=$bt;
				}
				$qt=mysqli_query($connect, "select a.*,b.user_nama as nama_kasir,c.nama_toko,c.alamat_toko from transaksi a, users b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.user_email and a.ID_user=b.ID and a.ID_user='$b[ID]'") or die(mysqli_error($connect));
				while($bt=mysqli_fetch_assoc($qt)){
					$bt['by']=1;
					$bt['detail']=array();
					$qd=mysqli_query($connect, "select a.*,b.nama_produk from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='$b[ID]' and ID_transaksi='$bt[id_]'");
					while($bd=mysqli_fetch_assoc($qd)){
						$bt['detail'][]=$bd;
					}
					$trx[]=$bt;
				}
				$udata = array('user'=>$user,'toko'=>$toko,'produk'=>$produk,'kasir'=>$kasir,'kasir_toko'=>$kasir_toko,'trx'=>$trx,'auth'=>$auth);
				$jdata=array('status'=>1,'msg'=>"Login Success, Welcome $b[user_nama]<br>Redirecting...",'data'=>$udata);
			}else{
				$jdata=array('status'=>0,'msg'=>'Sorry $b[user_nama], Temporarily your account is blocked!','wrong'=>'email');
			}
		}else{
			$jdata=array('status'=>0,'msg'=>'The password you entered is wrong!','wrong'=>'password');
		}
	}else{
		$qlogink = mysqli_query($connect, "select * from karyawan where email='$email'");
		if(!$qlogink){
			$jdata = array('status'=>-1,'msg'=>mysqli_error($connect));
		}else{
			if (mysqli_num_rows($qlogink)>0){
				$b=mysqli_fetch_array($qlogink);
				if($b['password']==$password){
					if($b['status']=='1'){
						$us = $b[0];
						$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('hp'=>$b['no_hp'],'email'=>$b['email'],'nama'=>$b['nama'],'foto'=>$b['foto'],'t'=>2);
						$qt=mysqli_query($connect, "select b.* from karyawan_toko a,toko b where a.ID_user='$b[ID_user]' and a.ID_karyawan='$b[ID]' and b.ID=a.ID_toko") or die(mysqli_error($connect));
						$toko =array();
						while($bt=mysqli_fetch_assoc($qt)){
							$toko[]=$bt;
						}
						$qt=mysqli_query($connect, "select a.*,b.stok,b.ID_toko from produk a, produk_stok_toko b where b.ID_produk=a.ID and b.ID_user=a.ID_user and a.ID_user='$b[ID_user]' and b.ID_toko in (select ID_toko from karyawan_toko where ID_user='$b[ID_user]' and ID_karyawan='$b[ID]' )") or die(mysqli_error($connect));
						$produk =array();
						while($bt=mysqli_fetch_assoc($qt)){
							$produk[]=$bt;
						}
						$qt=mysqli_query($connect, "select a.*,b.nama as nama_kasir,c.nama_toko,c.alamat_toko from transaksi a, karyawan b, toko c where c.ID=a.ID_toko and a.email_karyawan=b.email and a.ID_user=b.ID_user and a.ID_user='$b[ID_user]' and a.email_karyawan='$b[email]'") or die(mysqli_error($connect));
						$trx =array();
						while($bt=mysqli_fetch_assoc($qt)){
							$bt['by']=2;
							$bt['detail']=array();
							$qd=mysqli_query($connect, "select a.*,b.nama_produk from transaksi_detail a, produk b where a.ID_produk=b.ID and b.ID_user='$b[ID_user]' and ID_transaksi='$bt[id_]'");
							while($bd=mysqli_fetch_assoc($qd)){
								$bt['detail'][]=$bd;
							}
							$trx[]=$bt;
						}
						$udata = array('user'=>$user,'toko'=>$toko,'produk'=>$produk,'kasir_toko'=>$kasir_toko,'trx'=>$trx,'auth'=>$auth);
						$jdata=array('status'=>1,'msg'=>"Login Success, Welcome ".$user['nama']."<br>Redirecting...",'data'=>$udata);
					}else{
						$jdata=array('status'=>0,'msg'=>'Sorry $b[user_nama], Temporarily your account is blocked!','wrong'=>'email');
					}
				}else{
					$jdata=array('status'=>0,'msg'=>'The password you entered is wrong!','wrong'=>'password');
				}
			}else{
				$jdata=array('status'=>0,'msg'=>'Your email is unknown!','wrong'=>'email');		
			}
		}
	}
}