<?php
  $pg = $_GET["pg"];
  $ac = $_GET["ac"];
  $psid = $_GET["sid"];
?>
<?php
  unset($arrWeek);
  $arrWeek[1] = "MONDAY";
  $arrWeek[2] = "TUESDAY";
  $arrWeek[3] = "WEDNESDAY";
  $arrWeek[4] = "THURSDAY";
  $arrWeek[5] = "FRIDAY";
  $arrWeek[6] = "SATURDAY";
  $arrWeek[0] = "SUNDAY";
?>

<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: #0000FF;
    }
  
  .mybtn{
    width:100px;
	font-size:14px;
	font-weight: bold;
	font-family: Arial;
  }
</style>

<script language='javascript'>
	function btnCancel(){
		window.location = 'main.php?pg=sys&ac=sect&sid=<?= $psid ?>';
	}
</script>

<center>
    
<?php
  $qsection = "SELECT * FROM punchsys_section";
  $rsection = mysqli_query($conn,$qsection);
  $nsection = mysqli_num_rows($rsection);
  while($myrow = mysqli_fetch_array($rsection)){
  
    $tid = $myrow["id"];
	$tname = $myrow["name"];
	$arrSection[$tid] = $tname;
  }
?>
<?php
  $qsect = "SELECT * FROM punchsys_timetable WHERE secid='$psid'";
  $rsect = mysqli_query($conn,$qsect);
  $nsect = mysqli_num_rows($rsect);
  if($nsect>0){
  while($myrow = mysqli_fetch_array($rsect)){
  
    $id = $myrow["id"];
	$secid = $myrow["secid"];
    $wkDay = $myrow["wkDay"];
	$pcInTime = $myrow["pcInTime"];
    $pcOutTime = $myrow["pcOutTime"];
  
    $arrSecTId[$wkDay] = $id;
    $arrSecTIn[$wkDay] = $pcInTime;
    $arrSecTOut[$wkDay] = $pcOutTime;

  }  
  }else{
     //not timetable exist 
  }
?>
    
<?php
  if($nsect==7){
  ?>
    
    <table cellpadding="0" cellspacing="1" border="0" class="mytable">
      <form action="main.php" method="get">
      <input type="hidden" name="pg" value="sys">
      <input type="hidden" name="ac" value="sec">
      <tr>
          <th class="mytableth" align="left" colspan="7" style="padding-left: 30px"><input type="submit" value="BACK" style="margin-right: 30px; color: #000000; font-weight: bold; padding: 3px 10px 3px 10px">SECTION : <?= $arrSection[$psid] ?></th>
      </tr>
      </form>

      <tr>
        <?php
        foreach($arrWeek as $tid=>$tweek){
          ?>
          <th class="mytableth"><?= substr($tweek,0,3) ?></th>
          <?php
        }
        ?>
      </tr>

      <tr>
        <?php
        foreach($arrWeek as $tid=>$tweek){
          ?>
          <td class="mytabletd"><a href="main.php?pg=sys&ac=sect&sid=<?= $psid ?>&wk=<?= $tid ?>"><span style="color: #0000FF"><?= $arrSecTIn[$tid] ?></span></a>
              <br>
          <a href="main.php?pg=sys&ac=sect&sid=<?= $psid ?>&wk=<?= $tid ?>"><span style="color: #0000FF"><?= $arrSecTOut[$tid] ?></span></a></td>
          <?php
        }
        ?>
      </tr>

      <tr>
          <td class="mytableth" colspan="7">&nbsp;</td>
      </tr>
    </table>
    
  <?php
  }else{//no timetable set
  ?>
    
    <table cellpadding="0" cellspacing="1" border="0" class="mytable">
      <form action="main.php" method="get">
      <input type="hidden" name="pg" value="sys">
      <input type="hidden" name="ac" value="sec">
      <tr>
          <th class="mytableth" align="left" colspan="7" style="padding-left: 30px"><input type="submit" value="BACK" style="margin-right: 30px; color: #000000; font-weight: bold; padding: 3px 10px 3px 10px">SECTION : <?= $arrSection[$psid] ?></th>
      </tr>
      </form>
        
      <form action="syssectimadd.php" method="post">
        <input type="hidden" name="sid" value="<?= $psid ?>">
      <tr>
          <th class="mytabletd" style="height: 80px">
              <span style="color: #FF0000">NOTE: No Timetables or Timetables corrupted,<br>please click </span>
              <input type="submit" value="RESET TIME TABLES" style="margin-right: 30px; color: #0000FF; font-weight: bold; padding: 3px 10px 3px 10px"></th>
      </tr>
      </form>
        
      <tr>
          <td class="mytableth">&nbsp;</td>
      </tr>
        
    </table>
    <?php
}
?>
    
<?php
//if($_GET["wk"]!==""){
if( isset($_GET["wk"]) ){
    $pwk = $_GET["wk"];
    ?>
    <hr>

  <form action="syssectimup.php" method="post">
    <input type="hidden" name="sid" value="<?= $psid ?>">
    <input type="hidden" name="wk" value="<?= $pwk ?>">
      
  <table cellpadding="0" cellspacing="1" border="0" class="mytable" style="width:100%">
    <tr>
	  <th class="mytableth"></th>
      <th width="35%" class="mytableth">Punch In Time</th>
      <th width="35%" class="mytableth">Punch Out Time</th>
	</tr>
    <tr>
	  <th class="mytableth" align="center"><?= $arrSection[$psid] ?><br><?= $arrWeek[$pwk] ?></th>
      <th height="50" bgColor="#EEEEEE">
        <input type="time" step="1" name="tmeStart" id="tmeStart" value="<?= $arrSecTIn[$pwk] ?>" style="width:120px; text-align:center"/>
	  </th>
	  
      <th width="200" bgcolor="#DDDDDD">
	    <input type="time" step="1" name="tmeEnd" id="tmeEnd" value="<?= $arrSecTOut[$pwk] ?>" style="width:120px; text-align:center"/>
	  </th>
	</tr>
	
    <tr>
	  <td colspan="3" align="center" class="mytableth">
		<input type="submit" value="UPDATE EXISTING RECORD" class="mybtn" style="width:250; color:#0000FF ">
		<input type="button" value="CANCEL" onClick="btnCancel()"  class="mybtn">&nbsp;&nbsp;&nbsp;
	  </td>
	</tr>
  </table>
  </form>
    
    <?php
}    
?>
    
</center>