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

  $tstamp = time();
  $id = trim($_POST["pid"]);
  $ename = $_POST["pename"];
  $cname = $_POST["pcname"];

  //$cname = mb_convert_encoding($_POST["pcname"], 'UTF-8', 'ISO-8859-1');
  $cname = mb_convert_encoding($cname, 'UTF-8', 'ISO-8859-1');

  $jwt = $_POST["pjwt"];
  $sec = $_POST["psec"];
  $admin = $_POST["padmin"];
  if($admin==""){
    $admin=0;
  }
  $visb = $_POST["pvisb"];
  if($visb==""){
    $visb=0;
  }
  $pstatus = $_POST["pstatus"];
  $_SESSION["err"]=0;
?>
<?php
  //-- validation ---//
  if($_POST["pid"]!="" && $_POST["pename"]!=""){
    $qchk = "SELECT * FROM punchsys_staff WHERE id='$id'";
	$rchk = mysqli_query($conn,$qchk);
	$nchk = mysqli_num_rows($rchk);
	if($nchk>0){
      $_SESSION["err"]=3;
      header("Location: main.php?pg=stf");
	}
  }else{
  
    if($_POST["pid"]==""){
      $_SESSION["err"]=1;
      header("Location: main.php?pg=stf");
    }else if($_POST["pename"]==""){
      $_SESSION["err"]=2;
      header("Location: main.php?pg=stf");
    }

  }
?>
<?php
  if($_SESSION["err"]==0){
  
    $query = "INSERT INTO punchsys_staff 
                (id, eName, cName, position, section, administrator,`type`,qualification,userID)
              VALUES ('$id','$ename','$cname','$jwt','$sec','$admin','$visb','$tstamp','$pstatus')";
    if( mysqli_query($conn,$query) ){
      $_SESSION["err"]=0;
	  header("Location: main.php?pg=stf");
    }else{
	  echo "error to record, pls try again";
	}
	
  }
?>
<?php
/*
$utf8 = '´÷»ÛÁá'; // file must be UTF-8 encoded
echo $utf8;
$iso88591_1 = utf8_decode($utf8);
echo $iso88591_1;
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $utf8);

$iso88591_2 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');
echo $iso88591_2;

echo "<hr>";
$iso88591 = '´÷»ÛÁáÐ£³¤'; // file must be ISO-8859-1 encoded
//$iso88591 = $_POST["pcname"];
//echo $iso88591;

echo utf8_decode($cname) . " - " . utf8_encode($iso88591) . "<br>";

if( strcmp($cname,$iso88591)==0){
	echo "same<br>";
}else{
	echo "not same<br>";
}

$utf8_1 = utf8_encode($iso88591);
echo $utf8_1 . "<br>";
$utf8_2 = iconv('ISO-8859-1', 'UTF-8', $iso88591);
echo $utf8_2 . "<br>";
$utf8_2 = mb_convert_encoding($iso88591, 'UTF-8', 'ISO-8859-1');
echo $utf8_2;
*/
?>