<?php
  include "include/dbcon.php";
?>
<?php
  global $conn;
  print_r($_POST);

  $tstamp = time();
  $hid = $_POST["hid"];
  $id = trim($_POST["pid"]);
  $ename = $_POST["pename"];
  $cname = $_POST["pcname"];
  $jwt = $_POST["pjwt"];
  $sec = $_POST["psec"];
//  $admin = $_POST["padmin"];
//  $visb = $_POST["pvisb"];
  $pstatus = $_POST["pstatus"];
  if($pstatus=="1"){
    $pstatus="1";
  }else{
    $pstatus="0";
  }
  if($visb==""){
    $visb=0;
  }
  $pass = $_POST["ppass"];
  if($admin==""){
    $admin=0;
  }
  $_SESSION["err"]=0;
?>
<?php
  //-- validation ---//
  if($_POST["pid"]!="" && $_POST["pename"]!=""){
    $qchk = "SELECT * FROM ".$CLIENTID."_attendace_staff WHERE id='$id' AND id<>'$hid'";
	$rchk = mysqli_query($conn,$qchk);
	$nchk = mysqli_num_rows($rchk);
	if($nchk>0){
      $_SESSION["err"]=3;
      header("location: main.php?pg=stf&ac=edt&id=$hid");
	}
  }else{
  
    if($_POST["pid"]==""){
      $_SESSION["err"]=1;
      header("location: main.php?pg=stf&ac=edt&id=$hid");
    }else if($_POST["pename"]==""){
      $_SESSION["err"]=2;
      header("location: main.php?pg=stf&ac=edt&id=$hid");
    }

  }
?>
<?php
  if($_SESSION["err"]==0){
  
    $query = "UPDATE ".$CLIENTID."_attendace_staff 
	            SET id='$id', eName='$ename', cName='$cname', 
				    position='$jwt', section='$sec', administrator='$admin', password='$pass', userID='$pstatus', qualification='$tstamp'
              WHERE
                id='$hid'";
	  
	  echo $query;
    if( mysqli_query($conn,$query) ){
	
	  $query = "UPDATE ".$CLIENTID."_attendace_punchrecord SET staffId='$id', qualification='$tstamp' WHERE staffId='$hid'";
		echo $query;
	  if(mysqli_query($conn,$query)){
	   // echo $query . "<br>";
	  }
	  $query = "UPDATE ".$CLIENTID."_attendace_remarkrecord SET staffId='$id', qualification='$tstamp' WHERE staffId='$hid'";
	  if(mysqli_query($conn,$query)){
	   // echo $query . "<br>";
	  }
	  //$query = "UPDATE xoops_pc_request SET staffId='$id' WHERE staffId='$hid'";
	  //if(mysql_query($query)){
	   // echo $query . "<br>";
	  //}
	
      $_SESSION["err"]=0;
	  header("location: main.php?pg=stf");
    }else{
	  echo "error to record, pls try again";
	}
	
  }
?>