<?
  include "include/dbcon.php";
//  include "../admsys/include/CSETTING.php";
  include "include/CTEACHER.php";
//  include "include/CTIMETABLE.php";
  include "include/cfunction.php";
//  date_default_timezone_set('Asia/Kuala_Lumpur');
?>
<?
//print_r($_POST);
?>
<?
  $staffId = $_POST["teaid"];
  $myuser = new CTEACHER();
  $myuser->setTeacher("id",$staffId);
  
  $hid = $_POST["hid"];
  $punchDate = $_POST["pdate"];
  $arrDate = explode("-",$punchDate);
  $pinhr = $_POST["inhr"];
  $pinmin = $_POST["inmin"];
  $pouthr = $_POST["outhr"];
  $poutmin = $_POST["outmin"];
?>
<?
  if($_POST["submit"]=="SAVE"){
?>
<?
  $intime = "$pinhr:$pinmin:00";
  $outtime = "$pouthr:$poutmin:00";
  $startT = $myuser->getStartT( date("w",mktime(0,0,0,$arrDate[1],$arrDate[2],$arrDate[0])) );
  $endT  = $myuser->getEndT( date("w",mktime(0,0,0,$arrDate[1],$arrDate[2],$arrDate[0])) );

  $punchIn = $intime;
  $punchOut = $outtime;
  $difSec1 = getDifSec2($startT,$punchDate,$punchIn,$punchDate);
//echo "$difSec1<br><br>";
  //--- come late -----------------
  if( $difSec1>0 ){
    $comLate = secConvTime($difSec1);
  //--- come early -----------------
  }else{
    $comLate = "00:00:00";
  }
  $difSec2 = getDifSec2($punchOut,$punchDate,$endT,$punchDate);
//echo "$difSec2<br><br>";
  //--- come late -----------------
  if( $difSec2>0 ){
    $levEarly = secConvTime($difSec2);
  //--- come early -----------------
  }else{
    $levEarly = "00:00:00";
  }

  $query = "UPDATE punchsys_punchrecord SET punchIn='$punchIn',punchOut='$punchOut',comLate='$comLate',levEarly='$levEarly'
				WHERE  punch_Id='$hid'";
      
//      echo $query;
    mysqli_query($conn,$query);
?>
<?
  }else if($_POST["submit"]=="DEL"){
  
    $query = "DELETE FROM punchsys_punchrecord WHERE punch_Id='$hid'";
	mysqli_query($conn,$query);
	
	$query2 = "DELETE FROM punchsys_remarkrecord WHERE staffid='$staffId' AND remarkDate='$punchDate'";
	mysqli_query($conn,$query2);
	
  }
?>
<?
  header("Location: main.php?pg=pch&pdte=$punchDate");
?>