<?php

	if($_SERVER['REQUEST_URI'] == '/DTR-PORTAL/index.php'){
		header('location: ./');
	}
	
	//phpinfo();
	session_start();
	
	
	require 'includes/flight/Flight.php';
	include('models/functions.php');
	
	
	// Db Connection & Params
	
	//App Path & DB Conn
	
	//Flight::set('path', 'http://18.141.216.6:8080/principalportal-test/');
	//Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=keisanzamjo','root','RGC2020@dm1n'),
	
	//
	//Flight::set('path', 'http://localhost:8080/DTR-PORTAL/');
	
	// New Method
	$base = Flight::request()->base;
	Flight::set('path','https://'.$_SERVER['HTTP_HOST'].$base.'/');
	
	//
	Flight::register('db', 'PDO', array('mysql:host=localhost;dbname=keisan_showcase','root','showcase@1234'),
	
	  function($db){
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	  }
	);
	$dbconn = Flight::db();

	// Set Flight Variables
	Flight::set('db', $dbconn);
	Flight::set('title', "DTR Portal");
	
	
	Flight::route('/dtr', function(){
		
		//$db = Flight::get('db');
		
		//if (Flight::has('id')) {
		
		//if(empty($_SESSION['CLEARANCE_USER'])){
		//	Flight::redirect('/login');
		//}else{
			//$username = $_SESSION['CLEARANCE_USER'];
			//$profile = Flight::selectAll($username);
			//$_SESSION['access'] = $profile['access'];
			$title = Flight::get('title');
			$path  = Flight::get('path');
			//$footer  = Flight::get('footer');
			
			Flight::render('dtr/header', array('path' => $path),'header_content');
			Flight::render('dtr/navbar', array('path' => $path),'navbar_content');
			Flight::render('dtr/main',   array('path' => $path),'body_content');
			Flight::render('dtr/footer', array('path' => $path),'footer_content');
			Flight::render('dtr/layout', array('title' => $title));
			
		//}else{
		//	Flight::redirect('/login');
		//}
		
	});
	
	Flight::route('/dtrlogs', function(){
		//$db = Flight::get('db');
		
		//if (Flight::has('id')) {
		
		//if(empty($_SESSION['CLEARANCE_USER'])){
		//	Flight::redirect('/login');
		//}else{
			//$username = $_SESSION['CLEARANCE_USER'];
			//$profile = Flight::selectAll($username);
			//$_SESSION['access'] = $profile['access'];
			$title = Flight::get('title');
			$path  = Flight::get('path');
			//$footer  = Flight::get('footer');
			
			Flight::render('dtr/header', array('path' => $path),'header_content');
			Flight::render('dtr/navbar', array('path' => $path),'navbar_content');
			Flight::render('dtr/dtrlogs',   array('path' => $path),'body_content');
			Flight::render('dtr/footer', array('path' => $path),'footer_content');
			Flight::render('dtr/layout', array('title' => $title));
			
		//}else{
		//	Flight::redirect('/login');
		//}
		
	});
	
	Flight::route('/interface/check-qrcode', function(){
		
		unset($_SESSION['PIC_FILENAME']);
		
		$qrcode = $_POST['qrcode'];
		//$qrcode = trim($qrcode,"DEMO%3A+");
		//$qrcode = 'EMP-000002';
		
		//echo $qrcode;

		// set default timezone
		date_default_timezone_set('Asia/Manila'); // CDT
		
		$current_date = date('Y-m-d');
		$current_time = date('H:i:s');
		
		//header('content-type: application/json; charset=utf-8');
		include('./includes/mysql_crud.php');
		$db = new Database();
		
		$db->connect();
		$db->sql("SELECT concat(employeeNo,' - ',lastName,', ',firstName) as employee from e_basicinfo where employeeNo = '$qrcode'");
		$res = $db->getResult();

		foreach ($res as $row) {
			$employee =  $row['employee'];
		}
		$dtr_status = '';
		
		if($res){
			$db->connect();
			$db->sql("SELECT employeeNo from u_biometrics where employeeNo = '$qrcode' and biometricsDate = '$current_date' and biometricsTimeOut is null");
			$res = $db->getResult();
			
			if($res){
				$dtr_status = 'TIME-OUT';
				$image_name = $qrcode.'-'.date('YmdHis').'-'.$dtr_status.'.jpeg';
				$db->connect();
				$db->update('u_biometrics',array('biometricsTimeOut'=>$current_time,'imageOut'=>$image_name),"employeeNo = '$qrcode' and biometricsDate = '$current_date' and biometricsTimeOut is null"); // Table name, column names and values, WHERE conditions
				$res = $db->getResult();		
			}else{
				$dtr_status = 'TIME-IN';
				$image_name = $qrcode.'-'.date('YmdHis').'-'.$dtr_status.'.jpeg';
				$db->connect();
				$db->insert('u_biometrics',array('employeeNo'=>$qrcode,'biometricsDate'=>$current_date,'biometricsTimeIn'=>$current_time,'imageIn'=>$image_name));			
				$res = $db->getResult();				
			}			

			$_SESSION['PIC_FILENAME'] = $image_name;
			//echo json_encode($res);
			echo json_encode([ 'msg'=>$employee, 'dtr_status'=>$dtr_status, 'success'=>'1', 'current_date'=>$current_date.' - '.$current_time ]);
		}else{
			//echo json_encode($res);
			//echo 'No record';
			echo json_encode(['msg'=>'Record not Found', 'dtr_status'=>'', 'success'=>'0', 'current_date'=>'']);
		}
		
	});
	
	
	Flight::route('/interface', function(){
		header('Access-Control-Allow-Origin: *');
		header('Access-Control-Allow-Methods: GET, POST');
		header("Access-Control-Allow-Headers: X-Requested-With");

		header('content-type: application/json; charset=utf-8');
		include('./includes/mysql_crud.php');
		$db = new Database();
		$db->connect();
		$db->sql("SELECT a.employeeNo, 
					concat(b.lastName,', ',b.firstName) as employeeName,
					a.biometricsDate,
					a.biometricsTimeIn,
					a.biometricsTimeOut
			 	  FROM u_biometrics a
					left join e_basicinfo b
					on a.employeeNo = b.employeeNo
				  ORDER by dateUpdated desc
				  LIMIT 10
					");
		//$db->select('company','CompanyName','','CompanyCode = "CMPNY01"'); // Table name, Column Names, JOIN, WHERE conditions, ORDER BY conditions
		$res = $db->getResult();
		echo json_encode($res);
		
	});
	
	
	/// Default Routes 
	Flight::route('/', function(){
		Flight::redirect('/dtr');
	});
	Flight::route('/*', function(){
		Flight::redirect('/dtr');
	});
	

	Flight::start();

?>

