<?
  include "include/dbcon.php";
  date_default_timezone_set('Asia/Kuala_Lumpur');
  include "include/cfunction.php";
  include "include/CTIMETABLE.php";
  include "include/CTEACHER.php";
?>
<?
  if($_GET["pdate"]!=""){
    $pdate = $_GET["pdate"];
  }else{
    $pdate = date("Y-m-d");
  }
?>
<?
  //$pdate = "2013-11-03";
  
  $arrTimetable = getTimeTableList();
  $arrStaff = getTeaList();
//  echo "<pre>";
//  print_r($arrStaff);
//  echo "</pre>";
?>
  <?
    function convDate($pdate){
      $arrDate = explode("/",$pdate);
	
	  $output["yr"] = $arrDate[0];
	  if(strlen($arrDate[1])<2){
	    $output["mth"] = "0".$arrDate[1];
	  }else{
	    $output["mth"] = $arrDate[1];
	  }
	  if(strlen($arrDate[2])<2){
	    $output["day"] = "0".$arrDate[2];
	  }else{
	    $output["day"] = $arrDate[2];
	  }
	
	  $output["date"] = $output["yr"] . "-" . $output["mth"] . "-" . $output["day"];
	
      return $output;
    }
  ?>
  <?
    function convTime($ptime){
      $arr = explode(":",$ptime);
	
	  $output["hr"] = $arrTime[0];
	  $output["min"] = $arrTime[1];
	  $output["sec"] = $arrTime[2];
	
      return $output;
    }
  ?>
<?
  if($_GET["fle"]!=""){
  
    $handle = fopen($_GET["fle"], "rb");
    if($handle==true){

      $contents = '';
      while (!feof($handle)) {
        $contents .= fread($handle, 8192);
      }
      fclose($handle);

	  $arrCont = explode("\n",$contents);

      foreach($arrCont as $tcont){

	    $tcont2 = mb_convert_encoding($tcont,"HTML-ENTITIES");//HTML_ENTITIES use for non_utf8 encoding
//echo $tcont . "<br>";
	    $arr = explode("_",$tcont2);//[0]=stafid,[1]=date,[2]=time
		//$iii=0;
		//foreach($arr as $tarr){
		//  echo "arr[" . $iii . "] " . $tarr . "<br>";
		//  $iii++;
		//}
		//echo "<hr>";
	    //$tdate = convDate($arr[1]);
	    //$ttime = $arr[2];

//print_r($arr);echo "<br>";
	
		$tid = $arr[0];
//		$tarrdate = explode(" ",$arr[5]);
//		$tdate = convDate($tarrdate[1]);
		$tdate = $arr[1];
//		$ttime = $tarrdate[2];
		$ttime = $arr[2];
		//echo $tid . " : " . $tdate["date"] . " " . $ttime . "<br>";
	
		//$tempdate = $tdate["date"];
		$tempdate = $tdate;
		$arrTempDate[$tempdate] = $tempdate;
		$arrTempUid[$tid] = $tid;
		
		$arrTemp[ $tempdate ][ $tid ]++;
		
	      if( $arrTemp[ $tempdate ][ $tid ] > 1 ){
	        $arrMydata[$tempdate][$tid]["out"] = $ttime;
	      }else{
	        $arrMydata[$tempdate][$tid]["in"] = $ttime;
	      }
	
		/*
	    if($arr[1]!=""){
		
	      $tempdate = $tdate["date"];
	  
	      $arrTempDate[$tempdate] = $tempdate;
	      $arrTempUid[$arr[0]] = $arr[0];
	
	      $arrTemp[ $tempdate ][ $arr[0] ]++;
	
	      if( $arrTemp[ $tempdate ][ $arr[0] ] > 1 ){
	        $arrMydata[$tempdate][$arr[0]]["out"] = $ttime;
	      }else{
	        $arrMydata[$tempdate][$arr[0]]["in"] = $ttime;
	      }
	    
	    }//--if($arr[1]!=""){
		*/
		
      }//foreach($arrCont as $tcont){

    }//end of if($handle==true){
  
  }//if($_GET["fle"]!=""){
//  echo "<pre>";
//  print_r($arrMydata);
//  echo "</pre>";
?>
<?
  $query = "SELECT * FROM punchsys_punchrecord WHERE punchDate='$pdate'";
  $result = mysqli_query($conn,$query);
  $num = mysqli_num_rows($result);
  $i = 0;
  if($num>0){
  while($myrow = mysqli_fetch_array($result)){
  
    $tid = $myrow["punch_Id"];
    $tuid = $myrow["staffId"];
    $tdate = $myrow["punchDate"];
	$punchIn = $myrow["punchIn"];
	$punchOut = $myrow["punchOut"];
	
	$arrMydata2[$tdate][$tuid]["in"] = $punchIn;
    $arrMydata2[$tdate][$tuid]["out"] = $punchOut;
	$arrMydata2[$tdate][$tuid]["id"] = $tid;
  
    $i++;
  }
  }
?>

<table border="1" style="font-family:Arial; width:850px; font-size:12px ">
  <form action="vb_punchimport2.php" method="get">
  <tr>
    <th colspan="7">
	
	  <table>
	  <tr><td>
	  <select size="2" name="fle">
	  <?
	  $path_parts = pathinfo(__FILE__);
	  if ($handle = opendir($path_parts['dirname'])) {
        while (false !== ($tempfile = readdir($handle))) {
		  if( strpos($tempfile,"mydata2") !== false ){
	        $arrfiles2[$tempfile] = $tempfile;
		  }else{
		  }
	    }
        closedir($handle);
      }
	  sort($arrfiles2);

	  foreach($arrfiles2 as $key=>$file){
        echo "<option value='$file'>$file</option>";
	  }
	  ?>
	  </select>
	  <input type="submit" value="SELECT DATA" />
	  </td>
	  </form>
	  
	  <td>&nbsp;&nbsp;&nbsp;</td>
  
      <form action="" method="get">
	  <input type="hidden" name="fle" value="<?= $_GET["fle"] ?>" />
      <td>
      <select name="pdate">
	  <?
	  foreach($arrMydata as $tdate=>$tdump){
	    if($tdate==$_GET["pdate"]){
		  $selected = "SELECTED";
		}else{
		  $selected = "";
		}
	    echo "<option value='$tdate' $selected>$tdate</option>";
	  }
	  ?>
      </select>
	  <input type="submit" value="SELECT DATE" />
      </td>
    </tr>
    </form>
	
     </td></tr>
	 </table>
	  
	  
	</th>
  </tr>
  </form>
  <tr align="center">
    <td>&nbsp;</td>
    <td>ID</td>
    <td colspan="2">IN</td>
    <td colspan="2">OUT</td>
    <td><input type="checkbox" id="mychk" onclick="checkAll(this)" />UPDATE</td>
  </tr>
  <form action='vb_punchimport2_up.php' method="post">
<?
  $i=1;

  foreach($arrTempUid as $tuid){
    $tdate = "";
    foreach($arrTempDate as $tdate){

	  if($tdate==$pdate){
	  
//echo "(".$arrMydata[$pdate][$tuid]["in"].",".$arrMydata2[$pdate][$tuid]["in"].")<br>";
//	  if( strcmp(trim($arrMydata[$pdate][$tuid]["out"]),trim($arrMydata2[$pdate][$tuid]["out"]))==0  ){
	  if( $arrMydata[$pdate][$tuid]["in"]=="" ){

	  }else{
	  $tnCheck = "arrPid[$i]";
	  echo "<input type='hidden' name='pdate' value='$pdate' />";
	  echo "<input type='hidden' name='arrUid[$i]' value='$tuid' />";
	  echo "<input type='hidden' name='arrIn[$i]' value='".$arrMydata[$pdate][$tuid]['in']."' />";
	  echo "<input type='hidden' name='arrOut[$i]' value='".$arrMydata[$pdate][$tuid]['out']."' />";
      echo "<tr>
	    <td>$i</td>
		<td>[".$tuid . "] " . $arrStaff[$tuid]["ename"]."</td>
		<td>M: ".$arrMydata[$pdate][$tuid]["in"]."</td>
		<td>S: ".$arrMydata2[$pdate][$tuid]["in"]."</td>
		<td>M: ".$arrMydata[$pdate][$tuid]["out"]."</td>
		<td>S: ".$arrMydata2[$pdate][$tuid]["out"]."</td>
		<td><input type='checkbox' id='chk".$i."' name='$tnCheck' value='".$arrMydata2[$pdate][$tuid]["id"]."' /></td>
		</tr>";
      $i++;
      }//if( strcmp(trim($arrMydata[$pdate][$tuid]["out"]),trim($


	  
	  }//if($tdate==$pdate){
	  else{
//	  	  echo $tdate . " " . $pdate ."<br>";
	  }
    }//foreach($arrTempDate as $tdate){
  }//foreach($arrTempUid as $tuid){
?>
  <tr>
    <th colspan="7">
	<input type="submit" value="UPDATE" />
	</th>
  </tr>
</form>
</table>
<script>
function checkAll(){

  bolChk = document.getElementById("mychk").checked;
  if(bolChk==true){
<?
  $count = $i;
  $i=1;
  while($i<$count){
    ?>
	document.getElementById("chk<?= $i ?>").checked = true;
	<?
    $i++;
  }
?>
  }
  if(bolChk==false){
<?
  $count = $i;
  $i=1;
  while($i<$count){
    ?>
	document.getElementById("chk<?= $i ?>").checked = false;
	<?
    $i++;
  }
?>
  }
}
</script>
<pre>
<?
//  print_r($arrMydata);
?>
</pre>