<script>
	function btnCancel(){
		window.location = 'main.php?pg=stf';
	}
    
    function cfmdel(pid,pname){
        var response = confirm("Are you sure to delete the staff ["+pname+"]? \nAll the ["+pname+"]'s records will delete together.");
        if(response==true){
            window.location = 'syssecdel.php?id=' + pid;
        }
    }
</script>

<style>
  .mytable{
    font-size:16px;
	width:700px;
	margin-top:10px;
	color:#FFFFFF;
  }
  
  .mytableth{
    background-color:#000000;
	color:#FFCC00;
	padding:5px 5px 5px 5px;
  }
  
    .mytabletd{
        background-color: #EEEEEE;
        text-align: center;
        color: #000000;
    }
    
  .mybtn{
    width:100px;
	font-size:14px;
	font-weight: bold;
	font-family: Arial;
    color: #0000FF;
  }
    
  .mytext{
    text-align: center;
    padding: 3px;
  }
</style>

<?php
  $qsec = "SELECT * FROM ".$CLIENTID."_attendace_section";
  $rsec = mysqli_query($conn,$qsec);
  while($myrow = mysqli_fetch_array($rsec)){
  
    $tid = $myrow["id"];
    $tname = $myrow["name"];
	$arrSec[$tid] = $tname;
  
  }
?>

<center>
<?php
  if($_SESSION["errmsg"]!==""){
    echo $_SESSION["errmsg"];
    unset($_SESSION["errmsg"]);
  }
?>
<?php
  //----- add new record --------------------
  if($_GET["ac2"]!="edt"){
?>
    
<?php
  $qmaxid = "SELECT MAX(CAST(id AS UNSIGNED)) FROM ".$CLIENTID."_attendace_staff WHERE id NOT IN ('admin','pejabat')";
  $rmaxid = mysqli_query($conn,$qmaxid);
  while($myrow = mysqli_fetch_array($rmaxid)){
	$maxid = $myrow["MAX(CAST(id AS UNSIGNED))"];
  }
?>


<form action="staff_sav.php" method="post">
<table border="0" cellpadding="0" cellspacing="1" class="mytable">
    <tr>
      <th width="80px" class="mytableth">ID</th>
      <th class="mytableth">NAME</th>
      <th width="160px" class="mytableth">POSITION</th>
      <th width="40px" class="mytableth">SEC</th>
    </tr>
<tr>
    <td class="mytabletd">
      <input type="text" name="pid" class="mytext" maxlength="10" style="width:70px" value="<?= substr(( pow(10,strlen($maxid)) + $maxid +1 ),1) ?>">
    </td>
    <td class="mytabletd">
        <input type="text" name="pename" class="mytext" maxlength="30" style="width:190px" placeholder="name...">
        <input type="text" name="pcname" class="mytext" style="width:90px" placeholder="姓名。。。">
    </td>
    <td class="mytabletd">
        <input type="hidden" name="pvisb" value="1" />
        <input type="hidden" name="padmin" value="8" />
		<input type="hidden" name="pstatus" value="0" />
        <input type="text" name="pjwt" class="mytext" maxlength="15" style="width:130px" placeholder="position...">
    </td>
    <td bgcolor="#DDDDDD">
        <select name="psec">
          <?php
            foreach($arrSec as $tid=>$tsec){
              echo "<option value='$tid'>[$tid] $tsec</option>";
            }
          ?>
        </select>
    </td>
</tr>
<tr>
    <th colspan="4" class="mytableth"><input type="submit" value="ADD NEW STAFF" class="mybtn" style="width: 150px"></th>
</tr>
</form>
</table>

<hr>
<?php
  }//----- end add new record --------------------
?>
 
    
    
    

<?php
  if($_GET["ord"]!=""){
    switch($_GET["ord"]){
	  case "i":
	    $exquery1 = ", CAST(id as SIGNED)";
	    break;
	  case "n":
	    $exquery1 = ", eName";
	    break;
	  case "s":
	    $exquery1 = ", section, CAST(id as SIGNED), eName";
	    break;
	}
  }else{
    $exquery1 = ", repPrint, CAST(id as SIGNED)";
  }
?>
<?php
  $qstf = "SELECT * FROM ".$CLIENTID."_attendace_staff WHERE id NOT IN ('admin','pejabat') ORDER BY userID $exquery1";
  $rstf = mysqli_query($conn,$qstf);

  while($myrow = mysqli_fetch_array($rstf)){
	  $tid = $myrow["id"];

	  $arrStf[$tid]["ename"] = $myrow["eName"];
	  $arrStf[$tid]["cname"] = $myrow["cName"];
	  $arrStf[$tid]["position"] = $myrow["position"];
	  $arrStf[$tid]["section"] = $myrow["section"];
	  $arrStf[$tid]["admin"] = $myrow["administrator"];
	  $arrStf[$tid]["userID"] = $myrow["userID"];
  }
?>

<center>
<table border="0" cellpadding="0" cellspacing="1" class="mytable">
<tr>
  <th class="mytableth" width="60px">
    ID  
	<a href="main.php?pg=stf&ord=i">
        <abbr title="ORDER BY ID">
            <img src="img/ico_sort.png" height="18px"></abbr>
	</a>
  </th>
    
  <th class="mytableth" width="280px">
    NAME
      <a href="main.php?pg=stf&ord=n"><abbr title="ORDER BY NAME"><img src="img/ico_sort.png" height="18px"></abbr></a>
      
    <a href="main.php?pg=stf&ac=ord"><abbr title="sorting staff">
        <img src="img/ico_sort2.png" height="18px" style="margin-left: 20px"></a>

  </th>
    
  <th width="130px" class="mytableth">POSITION</th>
    
  <th class="mytableth" width="100px">
      SEC
      <a href="main.php?pg=stf&ord=s"><abbr title="SORT BY SECTION"><img src="img/ico_sort.png" height="18px"></abbr></a>
    </th>
  <th class="mytableth" width="50px"></th>
</tr>
</table>
    
<div style="width: 700px; height: 300px; overflow: auto;">
<table border="0" cellpadding="0" cellspacing="1" class="mytable" style="margin-top: 0px; font-size: 13px; width: 680px">
<?php
  $i=0;
  foreach($arrStf as $tid=>$tstf){
  
    $tename = $tstf["ename"];
    $tcname = $tstf["cname"];
    $tjwt = $tstf["position"];
	$tsec = $tstf["section"];

	$tstatus = $tstf["userID"];
	if($tstatus>0){
	  $tstatus = "style='color: #666666'";
	}else{
	  $tstatus = "style='color: #000000'";
	}
?>
<form action="main.php" method="post">
<input type="hidden" name="pg" value="<?= $_GET["pg"] ?>" />
<tr class="myh2" align="center" <?= $tstatus ?>>
  <td width="65px" bgcolor="#CCCCCC"><?= $tid ?></td>
    
  <td  bgcolor="#999999"><?= $tename  ?></td>
    
  <td width="100px" bgcolor="#999999"><?= $tcname ?></td>
    
  <td width="140px" bgcolor="#CCCCCC"><?= $tjwt ?></td>
    
  <td width="120px" bgcolor="#CCCCCC"><?= $arrSec[$tsec] ?></td>
    
  <td width="35px" bgcolor="#999999" style="padding: 3px">
      <img width="20px" src="img/ico_edit.png" style="cursor:pointer" onclick="gotoEdtTea('<?= $tid ?>')" />
  </td>
</tr>
</form>
<?php
    $i++;
  }
?>
</table>
</div>
    
    
</center>
    
<script>
  function gotoEdtTea(id){
    window.location = "main.php?pg=stf&ac=edt&id="+id;
  }
</script>

<!-- show error msg if found error -->
<?php
  if($_SESSION["err"]!=""){
    $perr = $_SESSION["err"];
    switch($perr){
      case "0":
	    $msg = "record saved";
        break;
      case "1":
	    $msg = "sorry, id can not be empty.";
	    break;
      case "2":
	    $msg = "sorry, name can not be empty.";
	    break;
      case "3":
	    $msg = "sorry, id already in used.";
	    break;
    }
	echo "<script>alert('$msg')</script>";
  }
  unset($_SESSION["err"]);
?>