<?php
if(basename($_SERVER['PHP_SELF']) == 'attendance.php') 
  $ca=" class='current'";
include'header.php';
include'connection.php';
$dato=$datos=$Date;

if(isset($_POST['submit_show']))
		{
		$dato=$_POST['dato'];
		$datos=$_POST['datos'];
		}

if(isset($_POST['submit_add']))
		{
		$employee=$_POST['employee'];
		$dato=$_POST['dato'];
		$datos=$_POST['datos'];
	$do=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Eid`='$employee'");
		$ro=mysqli_fetch_assoc($do);
			$currentp=$ro['Currentp'];
		
			$date=$dato;
	while(strtotime("$date") <= strtotime("$datos")){

	$then=mysqli_query($conn, "INSERT INTO `attendance` (`Employee`, `Currentp`, `Owner`, `Date`) VALUES ('$employee', '$currentp', '$loge', '$date')");
			//echo"///// $date <br>";

			$originalDate = $date;
		$newDate = date("Y-m-d", strtotime($originalDate));
			$date = strtotime("+1 day", strtotime("$newDate"));
				$date = date("d-m-Y", $date);				
			
	}
		}
	
	if(isset($_POST['delete_id']))
		{
			$rowid=$_POST['rowid'];
		$dato=$_POST['dato'];
		$datos=$_POST['datos'];
			$then=mysqli_query($conn, "UPDATE `attendance` SET `Status`='1' WHERE `Number`='$rowid' LIMIT 1");
		}

	if($employee)
		$con="AND `Employee` = '$employee'";
	else
		$con='';

	$do=mysqli_query($conn, "SELECT *FROM `attendance` WHERE `Date` BETWEEN '$dato' AND '$datos' AND `Status`='0' $con ORDER BY `Date` ASC");
				$fo=mysqli_num_rows($do);
?>
<div class="container-fluid main-content">
<div class="page-title">
 
 <h1 style='margin-top:-20px; margin-bottom: 5px;'>Absence List</h1>

</div>
<div class="row"></div>
 <div class="row">

        <div class="col-md-12">
         <div class="row">
           <div class="col-md-6 "> 
           <form action="attendance.php" method="POST" class="form-horizontal ">
        <input name="searchkeyword" value="" type="hidden">
        <input name="column_name" value="" type="hidden">
    <table border='0'><tr><th>
            <div class="col-md-4 " style='width:150px;'> 
           <div class="input-group date datepicker" style='width:140px;'>
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>
</th><th>

		  <div class="col-md-4 " style='width:150px;'> 
           <div class="input-group date datepicker" style='width:140px;'>	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div> 
          
		  </th><th align='left'>
            
          
                    <div class="col-md-2" style='width:90px; top:2px;'> 
      
                 <button class="btn  btn-primary  btn-block" type="submit" name="submit_show"><i class="lnr lnr-chevron-right-circle"></i> Show</button>
          </div></th></tr></table> </div>





		    <div class="col-md-6 " style='text-align:right;'> 
        <input name="searchkeyword" value="" type="hidden">
        <input name="column_name" value="" type="hidden">
    
	    
          
                    <div class="col-md-2 " style='float:right'> 
                 <button class="btn  btn-primary  btn-block btn-success" type="submit" name="submit_add" onClick='return validate(form);'>Add</button>
          </div> 
            <div class="col-md-4 " style='float:right'> 
           <select name="employee" class="form-control" style='width:200px;'>
                              <option selected="selected" value=""> --Select Employee-- </option>
<?php
			$doi=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Status`='0' ORDER BY `Fname` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
$code=$roi['Eid'];
$fna=$roi['Fname'];
$lna=$roi['Lname'];
//$doh=mysqli_query($conn, "SELECT *FROM `attendance` WHERE `Employee`='$code' AND `Date`='$Date' AND `Status`='0'");
			//	if(!$foh=mysqli_num_rows($doh))
						echo"<option value='$code'> $fna $lna </option>";
			}
			?>                            
               </select>
          
         </div></form></div>
         
         <!--  Mark Attendance -->
<!-- mark attendance end  -->
         
         
         
         
          </div>
          <div class="row">
            <div class="col-md-12">
     
              <span>Total Records Found : <b><?php echo $fo ?></b></span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
                
                                 <table class="table table-striped">
                       
                                      <thead>
                    <tr role="row">
                      <th class="hidden-xs"> S.NO</th> 					  
                       <th class="hidden-xs"> Code</th>
                       <th> First&nbsp;Name </th>
                       <th> Last&nbsp;Name </th> 
                       <th class="hidden-xs"> Current Post </th> 
                       <th> Date </th>
                       <th style='padding-right:40px;'><div align='right'>Status</div></th>
                        <th style="width: 40px;text-align:center;">Actions</th>
                     </tr>
                    </thead>
                                        <tbody>
<?php
$n=1;
	while($ro=mysqli_fetch_assoc($do)){
$numb=$ro['Number'];
$eid=$ro['Employee'];
$curp=$ro['Currentp'];
$dati=$ro['Date'];
$leave=$ro['Leave'];
	if($leave){
		$lbt="<span class='label label-success' style='width:80px;'>LEAVE</span>";
		$act="<td><center> -- </td>";
	}
	else{
		$lbt="<span class='label label-danger' style='width:80px;'>ABSENT</span>";
		$act="<form method=post action='attendance.php'><td align='right' style='width:40px; padding:0px;'>
                              <center><input type='hidden' name='dato' value='$dato'>
<input type='hidden' name='datos' value='$datos'><input type='hidden' name='rowid' value='$numb'>
                          <button style='background-color:transparent;border:0px solid black; width:50px; margin:0px;' type='submit' name='delete_id'>
						  <span style=color:red;>Delete</span></button>
                                                    <input name=id value=29 type=hidden>
                          </td></form>";
	}

$dox=mysqli_query($conn, "SELECT *FROM `employees` WHERE `Eid`='$eid'");
		$rox=mysqli_fetch_assoc($dox);
			$fn=$rox['Fname'];
			$ln=$rox['Lname'];

$theni=mysqli_query($conn, "SELECT `Postname` FROM `position` WHERE `Postid` = '$curp'");
	$reni=mysqli_fetch_assoc($theni);
		$pos=$reni['Postname'];
           
		print("<tr>
                        <td class=hidden-xs>$n</td>
                        <td class=hidden-xs>$eid</td>
                        <td> $fn </td><td> $ln </td>
                        <td class=hidden-xs> $pos </td>
                        <td> $dati </td>
                        <td align='right'> $lbt </td>
                          $act              	
                        <tr>");
	$n++;
	}
	?>
              
                    </tbody>   
                  </table>
                                      <div class="row">
                  <div class="col-md-12">
                  <div class="pull-right">
                  <ul class="pagination">
                      <li>
                                 <a class="icon" href="#">
           <i class="lnr lnr-chevron-left"></i></a>
                                            </li>
                      <li class="active">
                        <a href="#">
                        1                       </a>
                      </li>

                                            <li>
                                              <a class="icon" href="#">
                        <i class="lnr lnr-chevron-right"></i></a>
                                           </li>
                    </ul>
              </div></div></div>
                                
              </div>
            </div>

<?php
include'footer.php';
?>