<?php
  include "include/dbcon.php";
//  date_default_timezone_set("Asia/Kuala_Lumpur");
//  include "include/CPCSETTING.php";
  include "include/cfunction.php";
//  include "include/CFUNCTION2.php";
?>
<?php
  global $conn;
//  $pfr = $_POST["fr"];
  $pid = trim($_POST["txtId"]);

  $ptstamp = time();
//echo $ptstamp;
  $pweekday = date("w");
//echo "week $pweekday <br>";
  $pdate = date("Y-m-d",$ptstamp);
//echo "date $pdate <br>";
  $ptime = date("H:i:s");
//echo "time $ptime <br>";

  $valid = true;
?>
<?php
  $qsec = "SELECT * FROM punchsys_timetable";
  $rsec = mysqli_query($conn,$qsec);
  while($myrow = mysqli_fetch_array($rsec)){
	  $tsecid = $myrow["secid"];
	  $twkDay = $myrow["wkDay"];
	  $arrSec[$tsecid][$twkDay]["in"] = $myrow["pcInTime"];
	  $arrSec[$tsecid][$twkDay]["out"] = $myrow["pcOutTime"];
  }
?>
<?php
  $query = "SELECT * FROM punchsys_staff WHERE id='$pid' AND id NOT IN ('admin','pejabat')";
  $result = mysqli_query($conn,$query);
  $num = mysqli_num_rows($result);
  if( $num>0 ){

	  while($myrow = mysqli_fetch_array($result)){
		  $tsection = $myrow["section"];
		  $tename = $myrow["eName"];
	  }
	  
	  $schpunchtimein = $arrSec[$tsection][$pweekday]["in"];
	  $schpunchtimeout = $arrSec[$tsection][$pweekday]["out"];
	  
	  $qchk = "SELECT * FROM punchsys_punchrecord WHERE staffId='$pid' AND punchDate='$pdate'";
	  $rchk = mysqli_query($conn,$qchk);
	  $nchk = mysqli_num_rows($rchk);

	  if($nchk>0){
		  

          $difSecOut = getDifSec2($ptime,$pdate,$schpunchtimeout,$pdate);
	      if( $difSecOut<0 ){
	        $levEarly = "00:00:00";
	      }else{
            $levEarly = secConvTime($difSecOut);
	      }		  
		  
		  $qupdate = "UPDATE punchsys_punchrecord SET punchOut='$ptime', levEarly='$levEarly', message='$ptstamp' WHERE punchDate='$pdate' AND staffId='$pid'";
		  if( mysqli_query($conn,$qupdate) ){
			$_SESSION["errmsg"] = "<span style='color:#8F00FF'>PUNCH OUT : </span><br><span style='color:#2365FF'>".$tename."</span>";
			$valid = true;  
		  }

	  }else{
		  $difSecIn = getDifSec2($ptime,$pdate,$schpunchtimein,$pdate);
		  
		  if( $difSecIn<0 ){
			$comelate = secConvTime($difSecIn);	
		  }else{
			$comelate = "00:00:00";
		  }
		  
		  $qinsert = "INSERT INTO punchsys_punchrecord VALUES (null,'$pid','$pdate','$ptime','$pdate','00:00:00','$ptstamp','$comelate','00:00:00')";
		  if( mysqli_query($conn,$qinsert) ){
			$_SESSION["errmsg"] = "<span style='color:#0000FF'>PUNCH IN : </span><br><span style='color:#2365FF'>".$tename."</span>";
			$valid = true;  
		  }
		  
	  }
	  
	  
	  
  }else{
	  $valid = false;
	  $_SESSION["errmsg"] = "<font color='#FF0003'>[".$pid."] INVALID ID.</font>";
  }
?>
<?php
  if($valid){
	  header("location: punchpage.php");
  }else{
	  header("location: punchpage.php");
  }
?>
<?php
/*
  $query = "SELECT * FROM xoops_adm_teacher WHERE id='$pid' AND id NOT IN ('admin','pejabat')";
  $result = $xoopsDB->query($query);
  
  if( $xoopsDB->getRowsNum($result)>0 ){
	  header("location: passverify.php?fr=$pfr");
  }else{
	  $_SESSION["msg"] = "<font color='red'>[".$pid."] Invalid ID.</font>";
	  header("location: punchpage.php");
  }

  //--- check wherether need check password
    $mysttg = new CPCSETTING();
	$_SESSION["userid"]=$pid;
	$_SESSION["count"]=3;

    //---if password verify enable----------
    if( $mysttg->passverify==1){
//	  echo "1";
//	  header("Location: index.php?pass=1");
    //---if password verify disable----------
    }else{
//	echo "2";
//      header("Location: passverify.php?fr=$pfr");
    }
  }else{
    //--- invalid id
    if($pfr=="v"){
//	  echo "3";
	  $msg = "[$pid] Invalid ID";
//      header("Location: punchpage.php?msg=$msg&v2=0");
	}else{    
//	echo "4";
	  $_SESSION["msg"] = "<font color='red'>[".$pid."] Invalid ID.</font>";
//      header("Location: punchpage.php");
	}

  }
  */
?>