	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	* Location profil -> login
	*/
	
	require_once("../connection.php");
	
			$username=mysqli_real_escape_string($connect, $_POST['user']);
			$password=mysqli_real_escape_string($connect, md5($_POST['password']));
			$latitude=mysqli_real_escape_string($connect, $_POST['latitude']);
			$longitude=mysqli_real_escape_string($connect, $_POST['longitude']);
			
	$setting = mysqli_query($connect,"SELECT * FROM setting");
	$s=mysqli_fetch_array($setting);


	
	class usr{}
	
	$tanggal	= gmdate("Y-m-d", time()+60*60*7);
	$jam	= gmdate("H:i", time()+60*60*7);

	
	
	
	if ((empty($username)) || (empty($password))) {
		$response = new usr();
		$response->status 	= 'true';
		$response->errCode 	= '00';
		$response->msg 		= "Columns cannot be empty"; 
		die(json_encode($response));
	}
	
	$query = "SELECT * FROM users WHERE phone_number='$username' AND password='$password' AND blokir='N'";
	$hasil = mysqli_query($connect,$query);
	
	if(mysqli_num_rows($hasil) > 0 ){
	$date		= gmdate("dHi");	
	$sesi="$username$date";
	
	$ids=md5($sesi);
	
	//mysqli_query($connect,"UPDATE users SET id_session = '$ids'
      //                       WHERE phone_number = '$username'");	
		
	$response = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	
	$response["data"] = array();
	while($x = mysqli_fetch_array($hasil)){
		
	
	$dataabsen = mysqli_query($connect,"SELECT * FROM attendance WHERE staff='$x[phone_number]' AND date='$tanggal'");
	$ambil=mysqli_num_rows($dataabsen);

	if ($ambil > 0){
		
		
	}else{
		mysqli_query($connect,"INSERT INTO attendance(staff,
								  date,
								  hour,
								  latitude,
								  longitude,
								  device,
								  name_attendance,
								  user) 
                    VALUES('$x[phone_number]',
						   '$tanggal',
						   '$jam',
						   '$latitude',
						   '$longitude',
						   '$ids',
						   'login',
						   '$x[master]')");
		
	}
	
	$mastertoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$x[id_store]'");
	$mt=mysqli_fetch_array($mastertoko);
	
	if($mt['currency']==''){
			$currencyes = "";
	}else{
			$currencyes = $mt['currency'];
			
	
$locale='en-US'; //browser or user locale
$currency=$currencyes;
$fmt = new NumberFormatter( $locale."@currency=$currency", NumberFormatter::CURRENCY );
$symbol = $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
header("Content-Type: text/html; charset=UTF-8;");
	}
	

	
  //  $h['key'] 		= $ids;
    $h['key'] 		= $x["id_session"];
    $h['currency'] 	= "$symbol ";
    $h['user'] 		= $x["phone_number"];
    $h['level'] 	= $x["level"];
    $h['master'] 	= $x["master"];
    $h['packages'] 	= $x["packages"];
	$h['typestore'] = $mt["typestore"];
	$h['decimal'] 	= $mt["decimals"];
    $h['id_store'] 	= $x["id_store"];
	$h['system_point'] 	= $s['point_system'];
    array_push($response["data"], $h);
	}
		
    die(json_encode($response));
		
	}else {
	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'Wrong login or inactive account'
	);
	echo json_encode($response);
	}
	
	mysqli_close();


?>