<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$leave_id = utility::app_crypt($_REQUEST['leave_id'],'d');
if($_REQUEST['id'] != $leave_id){
	die("Invaild URL");
}

###########################################  STRAT FILTER CONDITIONS    #####################################
$search_employee_input 	= isset($_REQUEST['search_employee_name']) ? $_REQUEST['search_employee_name'] : "";
$search_employee_name = mysqli_real_escape_string($conn, $search_employee_input);
if($search_employee_input) {
    $where	.= " AND employee_name = '$search_employee_name' ";
    $next_page_condition .= "&customer_name=" . urlencode($search_employee_input);
}
###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "leave_holiday_management";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Leave Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_records = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));


#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	
	$_SESSION['info_message'] = "Record deleted successfully";
	header("location: $file_name?page=view".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content ">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
					<!----------------------- Message ------------------------------->
					<?php if(isset($_SESSION['info_message'])){ ?>
					  <div class="alert alert-info alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						<?php echo $_SESSION['info_message']; unset($_SESSION['info_message']); ?>
					  </div>
					<?php } ?>
					<!----------------------- End Message ------------------------------->

					<div class="">
						<div class="card-body">
							<!-- search form -->
							<?php // include_once('leave_management_search_options.inc.php'); ?>
							<!-- /.search form -->
							Total No. of Leaves: <?=$total_no_of_records; ?>

							<form action="index?page=add" method="post" name="form1" id="form1">
								<div class="card-footer">
									<button type="submit" name="add" class="btn btn-primary" onclick="setFormAction('add')">Add Leave</button>
									<button type="submit" name="add_holiday" class="btn btn-success" onclick="setFormAction('add_holiday')">Add Holiday</button>
								</div>

								<?php if($result > 0) { ?>
									<div class="table-responsive">
										<table id="example1" class="table table-bordered table-striped">
											<thead>
												<tr>
													<th>Employee Name</th>
													<th>Start Date</th>
													<th>End Date</th>
													<th>Type</th>
													<th>Status</th>
													<th>Edit</th>
													<th>Delete</th>
												</tr>
											</thead>
											<tbody>
												<?php while($row = mysqli_fetch_array($sql)) { 
													$row = utility::validate_crypt_data($row);
												?>
												<tr style="background-color: <?php echo $row['is_authorised'] == '1' ? '#d4edda' : ($row['is_authorised'] == '2' ? '#f8d7da' : ''); ?>;">
													<td><?php echo $gsi->getEmployeeById($row["employee_name_id"]); ?></td>
													<td><?php echo utility::getFormatedDate($row['leave_holiday_start_date']); ?></td>
													<td><?php echo utility::getFormatedDate($row['leave_holiday_end_date']); ?></td>
													<td><?php echo ($row["leave_holiday_type"] == 1) ? "Holiday" : "Leave"; ?></td>
													<td>
														<?php if ($row['is_authorised'] == 1): ?>
															<i class="fa fa-check-circle text-success"></i> Approved
														<?php elseif ($row['is_authorised'] == 2): ?>
															<i class="fa fa-times-circle text-danger"></i> Rejected
														<?php elseif ($row['is_authorised'] == 0): ?>
															<i class="fa fa-clock text-info"></i> Pending
														<?php endif; ?>
													</td>
													<td>
														<a href="index?page=<?php echo ($row['leave_holiday_type'] == 1) ? 'add_holiday' : 'add'; ?>&action=edit&leave_id=<?=urlencode(utility::app_crypt($row['id'],'e')); ?>&id=<?=$row['id'].$next_page_condition.$paging; ?>"> 
															<i class="fa fa-fw fa-edit text-primary"></i> 
														</a>
													</td>
													<td><a href="index?page=view&action=delete&leave_id=<?=urlencode(utility::app_crypt($row['id'],'e')); ?>&id=<?=$row['id'].$next_page_condition.$paging; ?>" onclick="return confirm('Are you sure you want to delete?');"><i class="fa fa-fw fa-trash text-danger"></i> </a></td>
												</tr>
												<?php } ?>
											</tbody>
										</table>
									</div>
								<?php } else { ?>
									No Leave found...
								<?php } ?>

								<div class="box-footer clearfix">
									<!-- Paging starts here  -->
									<?php 
									$next_page_condition .= "&page=view";
									include("paging/paging_row.inc.php"); 
									?>
									<!-- Paging ends here  -->
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<?include('view_leave_holiday_calendar.inc.php');?>
