<script language='javascript'>
	function btnCancel(){
		window.location = 'main.php?pg=sys&ac=hol';
	}
	function cfmdel(pid,pname){
		var response = confirm("Are you sure to delete ["+pname+"]?");
		
		if(response==true){
			//window.location = 'sysholdel.php?id=' + pid;
            document.getElementById("delid").value = pid;
            document.getElementById("frmdel").submit();
		}else{
		    return false;
		}
	}
	function publicHoliday(){
		x = document.getElementById("selHoliday");
		y = x.options[x.selectedIndex].text;
		document.getElementById("txtName").value = y;
	}
</script>
<form id="frmdel" action="sysholdel.php" method="post">
    <input type="hidden" id="delid" name="id">
</form>

<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;
  }
    
    .mybtnsubmit{
        padding: 3px 10px 3px 10px;
	    font-size:14px;
	    font-weight: bold;
	    font-family: Arial;
        color: #0000FF;
    }
    
  .mytext{
    text-align: center;
    padding: 3px;
  }
</style>


<?php
  $t = time();
  $today_date = date("Y-m-d", $t);
  $thisyear = date("Y");
?>

<center>
<?php
  if($_SESSION["errmsg"]!==""){
    echo $_SESSION["errmsg"];
    unset($_SESSION["errmsg"]);
  }
?>
<?php
  //----- add new record --------------------
  if($_GET["ac2"]!="edt"){
?>

<form action="sysholsav.php" method="post">
<table border="0" cellpadding="0" cellspacing="1" class="mytable">
  <tr>
    <th class="mytableth" colspan="3">ADD HOLIDAY</th>
  </tr>
  <tr>
    <th class="mytableth">HOLIDAY</th>
    <th class="mytableth" width="130px">FROM</th>
    <th class="mytableth" width="130px">UNTIL</th>
  </tr>
  <tr>
    <td class="mytabletd"><input type="text" name="hname" class="mytext" style="width: 230px" placeholder="holiday..."></td>
    <td class="mytabletd"><input type="date" name="hfrom" value="<?= $today_date ?>" class="mytext"></td>
    <td class="mytabletd"><input type="date" name="htill" value="<?= $today_date ?>" class="mytext"></td>
  </tr>
  <tr>
    <th class="mytableth" colspan="3">
        <input type="submit" value="ADD HOLIDAY" class="mybtnsubmit">
    </th>
  </tr>
</table>
</form>
    
<hr>
    
<?php
  }//----- end add new record --------------------
?>




<?php
	//----- edit record --------------------
	if($_GET["ac2"]=="edt"){
        $pid = $_GET["pid"];
        
		$query = "SELECT * FROM punchsys_holiday WHERE id=" . $pid;
		$result = mysqli_query($conn,$query);
		while($myrow = mysqli_fetch_array($result)){
            $tid = $myrow["id"];
            $tholDateFm = $myrow["holDateFm"];
            $tholDateTo = $myrow["holDateTo"];
            $tholDesc = $myrow["holDesc"];
        }

?>
		
        <form action="sysholup.php" method="post">
        <input type="hidden" name="pid" value="<?= $tid ?>">
        <table border="0" cellpadding="0" cellspacing="1" class="mytable">
          <tr>
            <th class="mytableth" colspan="3">EDIT HOLIDAY</th>
          </tr>
          <tr>
            <th class="mytableth">HOLIDAY</th>
            <th class="mytableth" width="130px">FROM</th>
            <th class="mytableth" width="130px">UNTIL</th>
          </tr>
          <tr>
            <td class="mytabletd"><input type="text" name="hname" class="mytext" style="width: 230px; background-color: #FFFF00" value="<?= $tholDesc ?>" placeholder="holiday..."></td>
            <td class="mytabletd"><input type="date" name="hfrom" class="mytext" style="background-color: #FFFF00" value="<?= $tholDateFm ?>"></td>
            <td class="mytabletd"><input type="date" name="htill" class="mytext" style="background-color: #FFFF00" value="<?= $tholDateTo ?>"></td>
          </tr>
          <tr>
            <th class="mytableth" colspan="3">
                <input type="submit" value="UPDATE HOLIDAY" class="mybtnsubmit">
				<input type="button" value="DELETE" onclick="cfmdel('<?= $tid ?>','<?= $tholDesc ?>')" class="mybtnsubmit" style="color:#FF0000" />
            </th>
          </tr>
        </table>
        </form>

        <hr>
<?php
  }
  //----- end edit record --------------------
?>
    
    
<?php
  //----- holiday page --------------------
  $qhol = "SELECT * FROM punchsys_holiday WHERE YEAR(holDateFm) IN ('".$thisyear."','".($thisyear+1)."') ORDER BY holDateFm";
  $rhol = mysqli_query($conn,$qhol);
  $nhol = mysqli_num_rows($rhol);
?>

    <table border="0" cellpadding="0" cellspacing="1" class="mytable">
      <tr>
        <th class="mytableth" style="width: 12px"></th>
        <th class="mytableth">HOLIDAY</th>
        <th class="mytableth">FROM</th>
        <th class="mytableth">UNTIL</th>
        <th class="mytableth">ACTION</th>
      </tr>
      <?php
      if($nhol>0){
      $i = 1;
      while($myrow = mysqli_fetch_array($rhol)){
        if( ($i%2)==0 ){
          $tcolor = "#EEEEEE";
        }else{
          $tcolor = "#DDDDDD";
        }
        ?>
      <tr>
        <td class="mytabletd" style="background-color: <?= $tcolor ?>"><?= $i ?></td>
        <td class="mytabletd" style="background-color: <?= $tcolor ?>"><?= $myrow["holDesc"] ?></td>
        <td class="mytabletd" style="background-color: <?= $tcolor ?>"><?= $myrow["holDateFm"] ?></td>
        <td class="mytabletd" style="background-color: <?= $tcolor ?>"><?= $myrow["holDateTo"] ?></td>
        <td class="mytabletd" style="background-color: <?= $tcolor ?>"><input type="button" value="EDIT" class="mybtnsubmit" onClick="frmEdit('<?= $myrow["id"] ?>')"></td>
      </tr>
        <?php
          $i++;
      }
      }
      ?>
    </table>
    
    <script>
        function frmEdit(pid){
           document.getElementById("pid").value = pid;
           document.getElementById("frmedit").submit();
        }
    </script>
    
    <form id="frmedit" action="main.php" method="get">
        <input type="hidden" name="pg" value="sys">
        <input type="hidden" name="ac" value="hol">
        <input type="hidden" name="ac2" value="edt">
        <input type="hidden" name="pid" id="pid" value="">
    </form>
    
    <br><br>
		
	</center>