<?
  include "../../mainfile.php";
  date_default_timezone_set('Asia/Kuala_Lumpur');
  include "include/cfunction.php";
  include "include/CTIMETABLE.php";
  include "../admsys/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;
    }
  ?>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
	
<center>
	
<?
  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

	    //$tcont2 = mb_convert_encoding($tcont, 'UTF-16LE', 'UTF-8' ); //mb_convert_encoding($tcont,"utf-8","HTML-ENTITIES");//HTML_ENTITIES use for non_utf8 encoding

		  
		  //echo $tcont . "<br>";
	    $arr = explode("\t",$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];
	
		$tid = ltrim($arr[2],"0");
		$tarrdate = explode(" ",$arr[8]);
		$tdate = convDate($tarrdate[0]);
		$ttime = $tarrdate[1];
		  
		$strDate = $tdate["date"];
		  
		$arrFileuid[$tid] = $tid;
		  
//	echo $tid . " : " . $strDate . " " . $ttime . "<br>";
//	echo "1, $strDate = " . mb_detect_encoding( $strDate, "auto" ) . "<br>";
		  
		//$strDate2 = htmlentities($strDate, ENT_QUOTES, 'UTF-8');
//	$strDate2 = mb_convert_encoding($strDate, 'UTF-8', 'UTF-16LE' );
//	echo "2, $strDate2 = " . mb_detect_encoding( $strDate2, "auto" ) . "<br>";
		  
		$tempdate = $tdate["date"];
		$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;
	      }
//	print_r($arrMydata);
		/*
	    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"]!=""){
//  print_r($arrMydata);
?>
<?
  $query = "SELECT * FROM xoops_pc_punchrecord WHERE punchDate='$pdate'";
  $result = mysql_query($query);
  $num = mysql_num_rows($result);
  $i = 0;
  while($i<$num){
  
    $tid = mysql_result($result,$i,"punch_Id");
    $tuid = mysql_result($result,$i,"staffId");
    $tdate = mysql_result($result,$i,"punchDate");
	$punchIn = mysql_result($result,$i,"punchIn");
	$punchOut = mysql_result($result,$i,"punchOut");
	
	$arrMydata2[$tdate][$tuid]["in"] = $punchIn;
    $arrMydata2[$tdate][$tuid]["out"] = $punchOut;
	$arrMydata2[$tdate][$tuid]["id"] = $tid;
  
    $i++;
  }
	
//	print_r($arrMydata2);
?>

<style>
	.mytable{
		font-family: Arial;
		width: 850px;
		font-size: 12px;
	}
	
	.mytd{
		padding-left: 5px;
	}
	
	.mytdM{
		text-align: center;
		background-color: #FFC4FC;
	}
	
	.mytdS{
		text-align: center;
		background-color: #C4C5FF;
	}
	
</style>
	
<table border="1" class="mytable">
  
  <tr>
    <th colspan="7" style="background-color: #CACACA">
	
	  <table border="0">
	  <tr>
		  
		  <form action="vb_punchimport3.php" method="get">
		  <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>
		  </td>
		  <td>
			<input type="submit" value="SELECT DATA" />
		  </td>
		  </form>
	  
	  	  <td style="width: 25px"></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>
		  </td>
		  <td>
	  		 <input type="submit" value="SELECT DATE" />
          </td>
		  </form>
		  
	  </tr>
	  </table>
	  
	</th>
  </tr>
  
  <tr style="background-color: #636363; color: #FFFFFF; text-align: center">
    <th>&nbsp;</th>
    <th>ID</th>
    <th colspan="2">IN</th>
    <th colspan="2">OUT</th>
    <th><input type="checkbox" id="mychk" onClick="checkAll(this)" />UPDATE</th>
  </tr>

	
  <form action='vb_punchimport3_up.php' method="post">
  <input type="hidden" name="pfle" value="<?= $_GET["fle"] ?>" >
  <input type="hidden" name="pdate" value="<?= $_GET["pdate"] ?>">	  
<?
  $i=1;
	  
  foreach($arrTempUid as $tuid){
    foreach($arrTempDate as $tdate){
	  if($tdate==$pdate){
	  
	  if( strcmp(trim($arrMydata[$pdate][$tuid]["out"]),trim($arrMydata2[$pdate][$tuid]["out"]))==0 ){
	  }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 class='mytd'>";
		  
		  echo "[ UID:" . $tuid . " ] ";
		  
		  if( $arrMydata2[$pdate][$tuid]["in"]>0 ){
			  $tcolorI = " style='color: #000000'";
		  }else{
			  $tcolorI = " style='color: #FF0000'";
		  }
		  
		  if( $arrMydata2[$pdate][$tuid]["out"]>0 ){
			  $tcolorO = " style='color: #000000'";
		  }else{
			  $tcolorO = " style='color: #FF0000'";
		  }
		  
	  echo $arrStaff[$tuid]["ename"]."</td>
		<td class='mytdM'>M: ".$arrMydata[$pdate][$tuid]["in"]."</td>
		<td class='mytdS' $tcolorI>S: ".$arrMydata2[$pdate][$tuid]["in"]."</td>
		<td class='mytdM'>M: ".$arrMydata[$pdate][$tuid]["out"]."</td>
		<td class='mytdS' $tcolorO>S: ".$arrMydata2[$pdate][$tuid]["out"]."</td>
		<td align='center'><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 style="background-color: #636363; color: #FFFFFF; text-align: center" 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>


</center>
	
</body>
</html>
