<?php
    session_start();
    					$server = "localhost";
                        $username="qrcode";
                        $password="P@ssw0rd%!";
                        $dbname="hris";

    $conn = new mysqli($server,$username,$password,$dbname);

    if($conn->connect_error){
        die("Connection failed" .$conn->connect_error);
    }

    if(isset($_POST['empidn'])){
        
        date_default_timezone_set('Asia/Taipei');
        $empi =$_POST['empidn'];
		$nd = $_POST['stnm'];
		$lgdate = date('Y-m-d H:i:s');
		$date = date('Y-m-d');
		$time = date('H:i:s');
      
      	$ob = json_decode($empi);
		$mp = $ob->employee_no;


		$sql ="SELECT * FROM employees WHERE employee_no='$mp';";
		$query=$conn->query($sql);
		IF($query->num_rows==0){
			$_SESSION['warn'] = 'Please contact IT Team for QR validation';
		}ELSE{
			// get the employee information
			while($row = $query->fetch_assoc()) {
				$empinfo = $ob->name . ' (' . $mp  . ')';
			}
			// time validation
			IF(time() <= strtotime("15:00:00")) {

				IF($nd == "night"){
					$sqls = "INSERT INTO qrattendance(EMPID,LOGDATE,STATUS,FLAG) VALUES('$mp','$lgdate','O','1')";
					$query=$conn->query($sqls);
					$_SESSION['timeout'] =  '<b>' . $empinfo . '<br></b>Log: ' . $lgdate;
				}else{
					$sqls = "INSERT INTO qrattendance(EMPID,LOGDATE,STATUS,FLAG) VALUES('$mp','$lgdate','I','1')";
					$query=$conn->query($sqls);
					$_SESSION['timein'] =  '<b>' . $empinfo . '<br></b>Log: ' . $lgdate;
				}

			}ELSE{
				
				IF($nd == "night"){
					$sqls = "INSERT INTO qrattendance(EMPID,LOGDATE,STATUS,FLAG) VALUES('$mp','$lgdate','I','1')";
					$query=$conn->query($sqls);
					$_SESSION['timein'] =  '<b>' . $empinfo . '<br></b>Log: ' . $lgdate;
				}else{
					$sqls = "INSERT INTO qrattendance(EMPID,LOGDATE,STATUS,FLAG) VALUES('$mp','$lgdate','O','1')";
					$query=$conn->query($sqls);
					$_SESSION['timeout'] =  '<b>' . $empinfo . '<br></b>Log: ' . $lgdate;
				}

			}

			
		}

	}
    header("location: index.php");
	   
    $conn->close();
?>