<?php
date_default_timezone_set('Asia/Kolkata'); 
//echo $date = date("Y-m-d", time());
include ('../config.php');
@session_start();
@$user_id1=$_SESSION['user_id1'];
@$idvfied1=$_SESSION['idverified1'];
@$namevfied1=$_SESSION['namevfied1'];
if(!isset($_SESSION['user_id1'])){
	header("Location: index.php");
}

$timeout = 60 * 60 * 24 * 365;
$s_name = session_name();
if(isset( $_COOKIE[ $s_name ] )) {
  setcookie( $s_name, $_COOKIE[ $s_name ], time() + $timeout, '/' );
}

$sel1=mysqli_query($conn,"select * from hr_dump where empid='".@$user_id1."'");
$fetch1=mysqli_fetch_array($sel1);

$queryFR=mysqli_query($conn,"select * from tracker where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR=mysqli_fetch_array($queryFR);

$queryFR=mysqli_query($conn,"select * from emp_leave where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR=mysqli_fetch_array($queryFR);
?>

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Pending Team Leave IC AMS</title>
	
	<?php	include ('header.php');	?>
	
	<div id="appCapsule">
        <div class="section wallet-card-section pt-1">
            <div class="wallet-card">
				
				<?php	include ('include/wallet-balance.php');	?>
				
				
				<div class="wallet-footer">
                    <div class="row text-center">
					<?php 
					if ($dataFR['Functional_Reporting'] == 'CN00119')
						{
						echo '<h6 class="text-center">Pending Leaves : '.date("Y-F", strtotime('-1 Months')).'</h6>';
						}
					else{
						echo'<div class="col-6"><a href="team-leave-pending.php"><div class="icon-wrapper"><i class="fas fa-calendar-alt"></i></div><strong class="pstrong">Current Month</strong></a></div>
						<div class="col-6"><a href="team-leave-pending-last.php"><div class="icon-wrapper bg-success text-white"><i class="fas fa-calendar-check"></i></div><strong class="pstrong">Last Month</strong></a></div>';
						echo '<h6 class="text-center">Pending Leaves : '.date("Y-F", strtotime('-1 Months')).'</h6>';
						}
					?>
					</div>
                </div>
				<div class="row mb-4">
					<form class="form-horizontal" name="ams" method='post' action='' enctype="multipart/form-data" autocomplete="off"><?php echo $alert; ?>
					<p>
						<?php 
						if ($fetch1['empid'] == 'CN00101')
							{
							echo "<input type='submit' value='Approve' class='btn btn-success' name='approve_update'>
							<input type='submit' value='Reject' class='btn btn-danger' name='reject_update'>";
							}
						?>
						<!-- <input type='submit' value='Approve' class='btn btn-success' name='approve_update'>
						<input type='submit' value='Reject' class='btn btn-danger' name='reject_update'> -->
					</p>
					<div class="table">
					<table class="table table-striped table-bordered bootstrap-datatable datatable responsive">
                    <tr style='background: whitesmoke;'>
                        <th><!--<input type='checkbox' id='checkAll' >--> Select</th>
                        <th>Employee Name</th>
                        <th>Type</th>
                        <th>Time</th>
                        <th>Employee Remarks</th>
                        <th>Date For</th>
                    </tr>
                    <?php
					$lm = date("Y-m", strtotime('-1 Months'));
                    $query = "SELECT * FROM emp_leave where Functional_Reporting='$fetch1[Employee_Number]' AND Status='Pending' AND DateFrom LIKE '%$lm%' order by id asc";
                    $result = mysqli_query($conn,$query);
                    while($row = mysqli_fetch_array($result) )
					{
					@$sn++;	
                    $id = $row['id'];
                    ?>
					<tr>
						<td><input type='checkbox' name='update[]' value='<?= $id ?>' ></td>
						<input type='hidden' name='id_<?= $id ?>' value='<?= $id ?>' readonly>
						<td><?php echo $row['Employee_Full_Name'] ?></td>
						<td><?php echo $row['LeaveType'] ?></td>
						<td><?php echo $row['TimeFor'] ?></td>
						<td><?php echo $row['Remarks'] ?></td>
                        <td><?php echo $row['DateFrom'] ?></td>
					</tr>
                    <?php
					}
					if(@$sn == 0) 
					{	echo "<div class='col-md-12 text-center btn btn-success'><strong>You have No Pending Request!</strong></div>";	}
                    ?>
                </table>
					</div>
				</form>
			<?php
				$alert= "";
				if(isset($_POST['approve_update'])){
					if(isset($_POST['update'])){
						foreach($_POST['update'] as $updateid){
								$updateUser = "UPDATE emp_leave SET Status='Approved' WHERE id=".$updateid;
								mysqli_query($conn,$updateUser);
						}
						echo "<script>window.location.href='team-leave-pending-last.php';</script>";	exit; 
						$alert = '<div class="alert alert-success" role="alert">Records successfully updated</div>';
					}
				}
				if(isset($_POST['reject_update'])){
					if(isset($_POST['update'])){
						foreach($_POST['update'] as $updateid){
								$updateUser = "UPDATE emp_leave SET Status='Rejected' WHERE id=".$updateid;
								mysqli_query($conn,$updateUser);
						}
						echo "<script>window.location.href='team-leave-pending.php';</script>";	exit; 
						$alert = '<div class="alert alert-success" role="alert">Records successfully updated</div>';
					}
				}
			?>
				</div>

            </div>
        </div>
		
<script type="text/javascript">
/* $(document).ready(function(){
   $("#customer_form").validate({
       rules: {
            f_name: {
                required: true,
                minlength: 3
            },
            l_name: {
                required: true,
                minlength: 3
            },   
        }
    });
}); */
</script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#checkAll').change(function(){
			if($(this).is(':checked')){
				$('input[name="update[]"]').prop('checked',true);
			}else{
				$('input[name="update[]"]').each(function(){	$(this).prop('checked',false);	}); 
			}
		});
		$('input[name="update[]"]').click(function(){
			var total_checkboxes = $('input[name="update[]"]').length;
			var total_checkboxes_checked = $('input[name="update[]"]:checked').length;
			if(total_checkboxes_checked == total_checkboxes){
				$('#checkAll').prop('checked',true);
			}else{	$('#checkAll').prop('checked',false);	}
		});
	});
</script>
        
		<!--</?php	include ('include/quik_links.php');	?>
		</?php	include ('include/extra_links.php');	?>-->
	
	<?php	include ('include/copyright.php');	?>
	</div>
	
	<?php	include ('footer.php');	?>