<?php
include("../include/config.php");
$date0='';
$date00='';
$date1='';
$date2='';

$emp_id=$_REQUEST['emp'];
$name_qry="select name,emp_number from tbl_employee  where emp_number='".$emp_id."'";
$name_sql=mysql_query($name_qry);
$name_arr=mysql_fetch_array($name_sql);
$yr=$_REQUEST['yr'];
$y1=$_REQUEST['yr1'];
$mon=$_REQUEST['mon'];
 $m1=$_REQUEST['mon1'];
 $seldt=$_REQUEST['seldt'];
 $selmt=$_REQUEST['selmt'];
 $selyt=$_REQUEST['selyt'];
 $flag=$_REQUEST['flag'];
  $stsval=$_REQUEST['stsval'];
  if($stsval!='')
  $flag=$stsval;
$seldt0=$selyt.$selmt.$seldt;
 if($selmt<10)
 $selmt="0".$selmt;
$sdt1=$selyt.'-'.$selmt.'-'.$seldt;
$dst1=$seldt.'-'.$selmt.'-'.$selyt;

$dd=$_REQUEST['dd'];
$d1=$_REQUEST['dd1'];

$date0=$yr.$mon.$dd;
$date00=$y1.$m1.$d1;
 if($m1<10)
 $m1="0".$m1;
 if($mon<10)
 $mon="0".$mon;


$date1=$yr.'-'.$mon.'-'.$dd;
$dat=$y1.'-'.$m1.'-'.$d1;

$dt1=$dd.'-'.$mon.'-'.$yr;
$dt2=$d1.'-'.$m1.'-'.$y1;

?>
<div>
 <table width="98%" border="0" cellspacing="0" cellpadding="0">
     <tr><td colspan="2" height="6"></td></tr>
  <tr><td class="err_msg" colspan="5">
   Leave Sheet of <? echo $name_arr['name'];?>/<? echo $name_arr['emp_number'];?> on <? if($seldt0!=''){ echo $dst1;}else{if($date0!=''){ echo $dt1;}if($date0!='' && $date00!=''){echo ' to ';} if($date00!=''){ echo $dt2;}}?>
   </td></tr>
    <tr>
      <th width="4%" align="left" valign="middle" class="tr_td">No</th>
	  <th width="4%" align="left" valign="middle" class="tr_td">Id</th>
	  <!-- <th width="30%" align="left" valign="middle" class="tr_td">Name</th>
      <th width="18%" align="left" valign="middle" class="tr_td">Emp Number</th> -->
      <th width="11%" align="left" valign="middle" class="tr_td">Leave On</th>
      <th width="11%" align="left" valign="middle" class="tr_td">Reason</th>
	   <th width="12%" align="left" valign="middle" class="tr_td">Status</th>
      <th width="10%" align="left" valign="middle" class="tr_td">option</th>
      </tr>
<?php
//$emp_qry="select * from tbl_employee where 1   ";

//$emps=mysql_query($emp_qry);
	$adjacents = 3;
	$page=0;
	$emp_qry='';
	$emp_id='';
	$sql1='';
	$yr1='';$mon='';$mon1='';$dd='';$dd1='';$yr='';
	if(isset($_REQUEST['emp']))
	$emp_id=$_REQUEST['emp'];
	if(isset($_REQUEST['yr']))
		$yr=$_REQUEST['yr'];
		if(isset($_REQUEST['yr1']))
		$yr1=$_REQUEST['yr1'];
		if(isset($_REQUEST['mon']))
		$mon=$_REQUEST['mon'];
		if(isset($_REQUEST['mon1']))
		$mon1=$_REQUEST['mon1'];
		if(isset($_REQUEST['dd']))
		$dd=$_REQUEST['dd'];
		if(isset($_REQUEST['dd1']))
		$dd1=$_REQUEST['dd1'];
		$date0=$yr.$mon.$dd;
		$date00=$yr1.$mon1.$dd1;
		if($mon<10)
		$mnth="0".$mon;
		$date1=$yr.'-'.$mnth.'-'.$dd;
		if($mon1<10)
		$mnth1="0".$mon1;
		$date2=$yr1.'-'.$mnth1.'-'.$dd1;
/*****************************/
		//if($month == $mon && $year == $yr )
		$mon=date('m');
		$yr=date('Y');
		
		/***********************/
		//$query = "SELECT COUNT(*) as cn where tbl_emp_leave.emp_no = tbl_employee.emp_number and YEAR(`fromdate`)='".$yr."' and YEAR(`todate`)='".$yr."' and MONTH(`fromdate`)='".$mon."' and MONTH(`todate`)='".$mon."'";
		//$sql = "SELECT tbl_emp_leave.*,tbl_employee.name FROM tbl_emp_leave,tbl_employee where tbl_emp_leave.emp_no = tbl_employee.emp_number and YEAR(`fromdate`)='".$yr."' and YEAR(`todate`)='".$yr."' and MONTH(`fromdate`)='".$mon."' and MONTH(`todate`)='".$mon."'";
		$query = "SELECT COUNT(*) as cn FROM tbl_emp_leave,tbl_employee where tbl_emp_leave.emp_no = tbl_employee.emp_number ";
		$sql = "SELECT tbl_emp_leave.* FROM tbl_emp_leave,tbl_employee where tbl_emp_leave.emp_no = tbl_employee.emp_number ";
	if($emp_id!=''){
		$query.=" and tbl_emp_leave.emp_no='".$emp_id."'";
		$sql.=" and tbl_emp_leave.emp_no='".$emp_id."'";
		}
		//-------------status checking
		if(isset($flag) && ($flag=='app')){
		$query.=" and tbl_emp_leave.status='app'";
		$sql.=" and tbl_emp_leave.status='app'";
		$cond=" and tbl_emp_leave.status='app'";
		}
		if(isset($flag) && ($flag=='pend')){
		$query.=" and tbl_emp_leave.status='pend'";
		$sql.=" and tbl_emp_leave.status='pend'";
		$cond=" and tbl_emp_leave.status='pend'";
		}
		if(isset($flag) && ($flag=='notapp')){
		$query.=" and tbl_emp_leave.status='notapp'";
		$sql.=" and tbl_emp_leave.status='notapp'";
		$cond=" and tbl_emp_leave.status='notapp'";
		}
		//----------------------------------------------------------//
		if($seldt0!=''){
		$query.=" and tbl_emp_leave.date='".$sdt1."'";
		$sql.=" and tbl_emp_leave.date='".$sdt1."'";
		}else{
			if($date0!=''){
			$query.=" and tbl_emp_leave.date>='".$date1."'";
			$sql.=" and tbl_emp_leave.date>='".$date1."'";
			}
			if($date0!='' && $date00!=''){
			$query.= " and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."'";
			$sql.= " and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."'";
			}
			if($date00!=''){
			$query.="  and  tbl_emp_leave.date<='".$date2."'";
			$sql.="  and  tbl_emp_leave.date<='".$date2."'";
			}
			if($date0!='' && $date00!=''){
			$query.= "  and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."'";
			$sql.= "  and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."'";
			}
		}
		$query.=" order by tbl_emp_leave.id desc ";	
	if(mysql_query($query)){
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages['cn'];
	}
	$targetpage = "emp-leave-req-view.php"; 	
	$limit = 30; 	
	
	if(isset($_GET['page']))
	{							
	$page = $_GET['page'];
	}
	if($page) 
	$start = ($page - 1) * $limit; 			
	else
	$start = 0;		
	
   $sql.= " order by tbl_emp_leave.date desc LIMIT $start, $limit";
	$res = mysql_query($sql);
	

	if ($page == 0) $page = 1;					
	$prev = $page - 1;							
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$lpm1 = $lastpage - 1;						
	
	
	$pagination = "";
	if($lastpage > 1)
	{	
	$pagination .= "<div class=\"pagination\">";
	if ($page > 1) 
	$pagination.= "<a href=\"javascript:emp_leave($prev)\">« previous</a>";
	else
	$pagination.= "<span class=\"disabled\">« previous</span>";	
	if ($lastpage < 7 + ($adjacents * 2))	
	{	
	for ($counter = 1; $counter <= $lastpage; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"javascript:emp_leave($counter)\">$counter</a>";					
	}
	}
	elseif($lastpage > 5 + ($adjacents * 2))	
	{
	if($page < 1 + ($adjacents * 2))		
	{
	for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
	}
	$pagination.= "...";
	$pagination.= "<a href=\"javascript:emp_leave($lpm1)\">$lpm1</a>";
	$pagination.= "<a href=\"javascript:emp_leave($lastpage)\">$lastpage</a>";		
	}
	elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
	{
	$pagination.= "<a href=\"javascript:emp_leave(1)\">1</a>";
	$pagination.= "<a href=\"javascript:emp_leave(2)\">2</a>";
	$pagination.= "...";
	for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"javascript:emp_leave($counter)\">$counter</a>";					
	}
	$pagination.= "...";
	$pagination.= "<a href=\"javascript:emp_leave($lpm1)\">$lpm1</a>";
	$pagination.= "<a href=\"javascript:emp_leave($lastpage)\">$lastpage</a>";		
	}
	else
	{
	$pagination.= "<a href=\"javascript:emp_leave(1)\">1</a>";
	$pagination.= "<a href=\"javascript:emp_leave(2)\">2</a>";
	$pagination.= "...";
	for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"javascript:emp_leave($counter)\">$counter</a>";					
	}
	}
	}
		
	if ($page < $counter - 1) 
	$pagination.= "<a href=\"javascript:emp_leave($next)\">next »</a>";
	else
	$pagination.= "<span class=\"disabled\">next »</span>";
	$pagination.= "</div>\n";		
	}
	

if($res && mysql_num_rows($res)>0){
$lve_cnt=$total_pages;
$c=1;
while($result=mysql_fetch_array($res))
{
	if($result['status'] == 'pend')
	{
		$status="pending";
	}
	else if($result['status'] == 'app')
	{
		$status="approved";
	}
	else if($result['status'] == 'not app')
	{
		$status="Not approved";
	}
		if($date0==''){
		$res_qry1=mysql_query("select date  from tbl_emp_leave  where emp_no='".$result['emp_no']."' ".$cond." and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."' order by id asc limit 0,1");
		$res_obj1=mysql_fetch_array($res_qry1);
		}else{
		$res_qry1=mysql_query("select date  from tbl_emp_leave  where emp_no='".$result['emp_no']."' ".$cond." and tbl_emp_leave.date>='".$date1."' order by id asc limit 0,1");
		$res_obj1=mysql_fetch_array($res_qry1);
		}
		$from_d=$res_obj1['date'];
		if($date00==''){
		$res_qry2=mysql_query("select date  from tbl_emp_leave  where emp_no='".$result['emp_no']."' ".$cond." and YEAR(tbl_emp_leave.date)='".$yr."'  and MONTH(tbl_emp_leave.date)='".$mon."' order by id desc limit 0,1");
		$res_obj2=mysql_fetch_array($res_qry2);
		}else{
		$res_qry2=mysql_query("select date  from tbl_emp_leave  where emp_no='".$result['emp_no']."' ".$cond." and  tbl_emp_leave.date<='".$date2."' order by id desc limit 0,1");
		$res_obj2=mysql_fetch_array($res_qry2);
		}
		$todat_d=$res_obj2['date'];

		
		$from=explode("-",$from_d);
		$month=$from[1];
		$year=$from[0];
		$days=$from[2];
		
		
		$todat=explode("-",$todat_d);
		$month1=$todat[1];
		$year1=$todat[0];
		$days1=$todat[2];
		$start = strtotime($from_d);
		$end = strtotime($todat_d);
		$days_between = ceil(abs($end - $start) / 86400);
		$daycont=$days_between;
		//$month=substr($from, 5, 2);
		//$year=substr($from, 0, 4);
		$lvDate1=$result['date'];
		$lvDate=explode("-",$lvDate1);
		$lvDatem=$lvDate[1];
		$lvDatey=$lvDate[0];
		$lvDated=$lvDate[2];
		
?>
  <tr>
      <td class="tr_td"><?=$c?></td>
     <td class="tr_td"><?php echo $result['id'];?></td>
      <!--  <td class="tr_td"><?php// echo $result['name'];?></td>
      <td class="tr_td"><?php// echo $result['emp_no'];?></td> -->
      <!--<td class="tr_td"><?php// echo $days.'-'.$month.'-'.$year;?></td>
      <td class="tr_td"><?php// echo $days1.'-'.$month1.'-'.$year1;?></td> -->
	  <td class="tr_td"><?php echo $lvDated.'-'.$lvDatem.'-'.$lvDatey;?></td>
      <td class="tr_td"><?php echo $result['reason'];?></td>
	  <td class="tr_td">
	   <? if(($todat_d!='' && $todat_d>=date("Y-m-d")) || ($todat_d=='' && $from_d!='' && $from_d>=date("Y-m-d"))){?>
 	  <select name="stval<?php echo $c;?>" id="stval<?php echo $c;?>" onChange="status_change('<?php echo $c;?>','<?php echo $result['id'];?>')" >
	 
  	<!--  <select name="stval<?php// echo $c;?>" id="stval<?php// echo $c;?>" onChange="javascript:location.href='emp-leave-req-view.php?leave_id=<?php// echo $result['id'];?>'" >
 --><option value="app" <? if($result['status']=="app"){echo 'selected';}?> >approved</option> 
	  <option value="pend"  <? if($result['status']=="pend"){echo 'selected';}?>>pending</option> 
	    <option value="notapp"  <? if($result['status']=="notapp"){echo 'selected';}?>>Not Approved</option> 
	  </select>
	  <? }else{if($status=='pend' || $status=='notapp'){echo 'Not Approved';}
	  if($status=='app'){echo 'Approved';}
	   }?>
	  </td>
      <td class="tr_td_no">
	  <a href="emp-leave-req.php?edit_id=<?=$result['id'];?>">
	  <img src="images/icon2.jpg" width="18" height="18" />
	  </a>
	  <a href="emp-leave-req-view.php?emp=<?php echo $_REQUEST['emp'];?>&flag=<?php echo $_REQUEST['flag'];?>&del_id=<?php echo $result['id']; ?>&emp_no=<?php echo $result['emp_number'];?>" onclick="return confirm('Do you really want to delete this field?');">
	  <img src="images/icon1.jpg" width="21" height="21" /></a></td>
      </tr>
<?php
	//}
	$c++;
}?>
<tr> <td class="tr_td_no" colspan="2" align="right" style="border-right:0px; "><b>Total Leaves:</b></td>
      <td class="tr_td_no" colspan="9" align="left"><span  class="err_msg"><?php echo $lve_cnt;?> days</span></td></tr>
<?
}else{?>
 <tr>
      <td class="tr_td_no" colspan="10">No Records Found!!</td></tr>

<? }
?></table></div>
 <div align="right" style="padding-top:10px;"><?=$pagination?></div>
