<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$fin_year=$_SESSION['fin_year'];

//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
$sql="create table holiday (id INT NOT NULL AUTO_INCREMENT ,date DATE NOT NULL ,narr VARCHAR(50) NOT NULL ,brcode VARCHAR(50) NOT NULL ,PRIMARY KEY (id))";
mysqli_query($db,$sql);
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Kerala Khadi & Village Industries Board</title>
<link type="text/css" href="css/form.css" rel="stylesheet" />	
<link type="text/css" href="jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery/jquery.jqEasyCharCounter.min.js"></script>
<script type="text/javascript">
  $(function() {
    $( "#holi_date" ).datepicker({ dateFormat: 'yy-mm-dd' });

  });
  </script>
  <style>
   
</style>
  </head>
  <body>

  <form method='post'>
  <center>
  <table>
  <h1 style="color: red;">Select Holidays for AMPL</h1>
  <tr>
  <td style="text-align:right;color: black;" class="invoiceForm_fromNameTD" id="invoice_dateTD">
  Add Holiday Here
   </td>
  <td style="text-align:right;" class="invoiceForm_fromNameTD" id="invoice_dateTD">
<input id="holi_date" name="holi_date" style="width:141px;outline:none;border:1px solid grey; cursor:pointer; padding-left:7px; font-weight:normal; font-family:verdana;font-size:13px;color:#3c3c3c;" type=text  value="<? echo date('Y-m-d');?>" onKeydown="return handleEnter(this, event)">
</td>
<td>
Narration
</td>
<td>
<input type='text' name='narr' style="width:141px;outline:none;border:1px solid grey; cursor:pointer; padding-left:7px; font-weight:normal; font-family:verdana;font-size:13px;color:#3c3c3c;">
</td>
<td>
<input type="submit" name='add' value='Add Holiday' style="background-color:transparent; border-color:transparent;cursor:pointer;" > 
</input>
  </td>
  <td>
<input type="submit" name='sun' value='Add All Sundays'> 
</input>
  </td>
  </tr>
  </table>
  </form>

  <?
if(isset($_POST['sun']))	
{
$holi_date=$_REQUEST['holi_date'];

$strt = round(((strtotime(substr($fin_year,0,4).'-04-01'))/86400),0);
$endt = round(((strtotime('20'.substr($fin_year,-2).'-03-31'))/86400),0);
	for($i=$strt;$i<=$endt;$i++)
	{
	if(date('D', ($i*86400))=='Sun')
		{
		$sund= date('Y-m-d',($i*86400));
	$res1=mysqli_query($db,"select * from holiday where date='$sund'");	
  if(mysqli_num_rows($res1)==0)
  {
		
		$sql="insert into holiday (date,narr,brcode) values('$sund','Sunday','$brcode')";
		mysqli_query($db,$sql) or die(mysqli_error($db));
  }
	    }
	}	
print "<script>alert('Updated Successfully');</script>";
}
if(isset($_POST['add']))	
{
$holi_date=$_REQUEST['holi_date'];
$narr=$_REQUEST['narr'];
  $res1=mysqli_query($db,"select * from holiday where date='$holi_date'");
  if(mysqli_num_rows($res1)>0)
  {
    print "<script> alert('Duplicate Entry');</script>";
	return;
  }

$sql="insert into holiday (date,narr,brcode) values('$holi_date','$narr','$brcode')";
mysqli_query($db,$sql) or die(mysqli_error($db));
print "<script>alert('Added Successfully');</script>";
}
  $sql2=mysqli_query($db,"select * from holiday where brcode='$brcode' order by date");
print "<br><br><form method='post'><table class='report' color='red' border='1px' width='300px'>";
print "<h2>AMPL Holidays</h2>";
print "<tr><td>SNo:</td><td>Date</td><td>Narration</td><td>Delete</td></tr>";
$i=1;
while($row=mysqli_fetch_array($sql2))
  {
  print "<tr><td>".$i."</td>";
  print "<td>".$row[date]."</td><td>".$row[narr]."</td>";
  print "<td><button type='button' onclick='delfun(".$row[id].")' name='del' style='background-color:transparent; border-color:transparent;'> 
		<img src='images/delete.png' height='20' width='20'/>
		</button></td></tr>";
  $i++;
  }
  print "</table></form>";

  ?>
</body>
<script>
function delfun(x)
{	
	 if (confirm('do you want to delete Holiday ?'))
	 {
	document.location="delholiday.php?delid="+x,true; 
	 }
	
}
</script>
</html>