<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// include the class
include 'requires/class_resize.php' ;

// keep parameter in value
$page = escapeString($_GET['page']) ;
$page_mode = escapeString($_GET['page_mode']) ;
$type = escapeString($_GET['type']) ;
$search = escapeString($_GET['search']) ;
$search_from_to = escapeString($_GET['search_from_to']) ;
$search_name = escapeString($_GET['search_name']) ;
$search_idno = escapeString($_GET['search_idno']) ;
$search_date_from = escapeString($_GET['search_date_from']) ;
$search_date_to = escapeString($_GET['search_date_to']) ;

// check permission
if ( !permissionCheck($row_user, 'leave-view') ){
	header('Location: index.php') ;
	exit ;
}

// mode type | all list | new | edit
switch($page_mode){
	case 'trash':
		$q = "UPDATE staff_leave SET 
		deleted_at = '".TODAYDATE."'
		WHERE leave_id = '".$page."' ";
		$mysqli->query($q);
		if($mysqli->error == ''){
	        $error_message = '<div class="result_success">'.$lang['Thank you your leave has been updated'].'</div>' ;
			header("Location:hr-leave.php?page_mode=list&ist_type=confirmed") ;
			$_SESSION['system_result'] = $error_message ;
			exit ;

		}
	break;
	// edit leave
	case 'new' :
	case 'edit' :

		// check query exsits
		$submit_type = 'new' ;
		$mysqli_page = $mysqli->query("SELECT * FROM staff_leave
									   WHERE leave_id = '".$page."' AND deleted_at IS NULL LIMIT 1");
		if ($mysqli_page->num_rows > 0){
			// keep query value in array
			$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
			$submit_type = 'edit' ;
		}

		// update database
		if ( isset($type) && ( $type == 'new' || $type == 'edit' ) && $_POST['hide'] == 1 ){

			$error_message  = '<div class="result_error">'.$lang['Please enter all required fill'].'</div>' ;

			$staff_id  		= escapeString($_POST['staff_id']) ;
			$leave_type     = escapeString($_POST['leave_type']) ;
			$leave_fullhalf = escapeString($_POST['leave_fullhalf']) ;
			$leave_from     = escapeString($_POST['leave_from']) ;
			$leave_to       = escapeString($_POST['leave_to']) ;
			$leave_reason   = escapeString($_POST['leave_reason']) ;
			$leave_status   = escapeString($_POST['leave_status']) ;

            if ( $leave_fullhalf == 'half' ){
            	$leave_to   = $leave_from ;
            }

			if ( ( ( $submit_type == 'new' && $staff_id != '' ) || $submit_type == 'edit' ) && $leave_type != '' && $leave_from != '' && $leave_to != '' && $leave_reason != '' ){

				$error_message = '<div class="result_error">'.$lang['Sorry you cannot apply different year of leave'].'</div>' ;

	            if ( date('Y', strtotime($leave_from)) == date('Y', strtotime($leave_to)) ){

	            	// get again staff 
	                $department      = '' ;
	                $get_department  = $mysqli->query("SELECT department_id FROM staff_department
	                                                   WHERE deleted_at IS NULL  AND staff_id = '".$staff_id."'") ;
	                if ( $get_department->num_rows > 0 ){
	                    while ( $row_department = $get_department->fetch_assoc() ){
	                        $department .= ',('.$row_department['department_id'].')' ;
	                    }
	                    $department = substr($department, 1) ;
	                }

	                // get days
	                $datetime1  = new DateTime($leave_from) ;
	                $datetime2  = new DateTime($leave_to) ;
	                $interval   = $datetime1->diff($datetime2) ;
	                $days       = $interval->format('%a')+1 ;

	                if ( $leave_fullhalf == 'half' ){
		            	$days = '0.5' ;
		            } ;

	                $leave_file = '' ;
	                $image 	    = $_FILES["image"]["name"] ;
	                if ( $_POST['remove_photo'] != '1' ){
		                if ( $image != '' ){
		                	$file_name  = $staff_id.'-'.time().'.png' ;
		                	copy($_FILES["image"]["tmp_name"], 'uploads/Leave/'.$file_name) ;
		                	$leave_file = $file_name ;
		                }else{
		                	$leave_file = $row_page['leave_file'] ;
		                }
		            }

	                // insert into leave
	                if ( $submit_type == 'new' ){
	                    if ( $mysqli->query("INSERT INTO staff_leave
	                                    	(staff_id, leave_department, leave_type, leave_from, leave_to, leave_day, leave_reason, leave_file, leave_incharge_status, leave_status, created_at, updated_at) VALUES
	                                    	('".$staff_id."', '".$department."', '".$leave_type."', '".$leave_from."', '".$leave_to."', '".$days."', '".$leave_reason."', '".$leave_file."', 'confirmed', 'pending', '".TODAYDATE."', '".TODAYDATE."')") ){
	                    	$error_message = '<div class="result_success">'.$lang['Thank you your leave has been add'].'</div>' ;
	                    }
	                }else{
	                	if ( $mysqli->query("UPDATE staff_leave SET
	                						 leave_department 	= '".$department."',
	                						 leave_type 	  	= '".$leave_type."',
	                						 leave_from 		= '".$leave_from."',
	                						 leave_to 			= '".$leave_to."',
	                						 leave_day 			= '".$days."',
	                						 leave_reason 		= '".$leave_reason."',
	                						 leave_file 		= '".$leave_file."',
	                						 updated_at 		= '".TODAYDATE."'
	                						 WHERE leave_id 	= '".$page."'") ){
	                		$error_message = '<div class="result_success">'.$lang['Thank you your leave has been updated'].'</div>' ;

	                		// for admin update date 
	                		// refer to update status part
	                		/*
	                		if ( $_POST['hide_admin_edit'] == 'yes' && $submit_type == 'edit' && $row_page['leave_status'] == 'confirmed' ){
	                			$mysqli_leave     = $mysqli->query("SELECT * FROM staff_leave
									   			 				    WHERE leave_id = '".$page."' AND deleted_at IS NULL LIMIT 1");
								if ( $mysqli_leave->num_rows > 0 ){
									$row_leave    = $mysqli_leave->fetch_array(MYSQLI_ASSOC) ;
									$page_action  = $row_leave['leave_status'] ;
									$key 		  = $page ;

									if ( $row_leave['leave_type'] == 'unpaid' || $row_leave['leave_type'] == 'annual' || $row_leave['leave_type'] == 'sick' ){
									
										// deduct leave
										$new_date = date('Y-m-d', strtotime($row_leave['leave_from'])) ;
		        						$new_year = date('Y', strtotime($row_leave['leave_from'])) ;

		        						$get_previous_leave = $mysqli->query("SELECT * FROM staff_leave_date
		        															  WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND leave_id = '".$key."' AND  leave_type = '".$row_leave['leave_type']."'") ;
		        						if ( $get_previous_leave->num_rows > 0 ){

		        							$count = 0 ;
		        							while ( $previous_leave = $get_previous_leave->fetch_assoc() ){
		        								if ( $previous_leave['leave_type_mode'] == 'working' ){
		        									$count += $previous_leave['leave_work_day'] ;
		        								}
		        								$mysqli->query("UPDATE staff_leave_date SET
		        												deleted_at = '".TODAYDATE."'
		        												WHERE leave_date_id = '".$previous_leave['leave_date_id']."'") ;
		        							}

		        							if ( $count > 0 ){
												$mysqli->query("UPDATE staff_leave_year SET
																leave_days = leave_days + '".$count."'
																WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND leave_type = '".$row_leave['leave_type']."' AND leave_year_from <= '".$row_leave['leave_from']."' AND leave_year_to >= '".$row_leave['leave_from']."'") ;
		        							}
		        						}

		        						// confirmed leave
	            						$boolean_leave_update = false ;
	            						if ( $page_action == 'confirmed' ){

	            							// check if staff exists or not
	            							$get_staff = $mysqli->query("SELECT * FROM staff
	            														 WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' LIMIT 1") ;
	            							if ( $get_staff->num_rows > 0 ){

	            								$row_staff = $get_staff->fetch_assoc() ;

	            								// open leave year for selected staff
	            								setStaffLeaveYear($row_leave['staff_id']) ;

	            								// check staff leave 
												// get current staff total leave
												$get_leave_year = $mysqli->query("SELECT leave_year_id, SUM(leave_days) as leave_total_days FROM staff_leave_year
																			 	  WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND  leave_type = '".$row_leave['leave_type']."' AND leave_year_from <= '".$row_leave['leave_from']."' AND leave_year_to >= '".$row_leave['leave_from']."'") ;

												if ( $get_leave_year->num_rows > 0 ){

													$leave_days         = 0 ;
							                        $boolean_ded        = false ;
							                        if ( $get_leave_year->num_rows > 0 ){
							                        	$row_leave_year = $get_leave_year->fetch_assoc() ;
							                            $leave_days  	= $row_leave_year['leave_total_days'] ;
							                            $boolean_ded 	= true ;
							                        }

							                        if ( $leave_days > 0 ){

								                        // check if full day or half day
								                        $day_fullhalf = 'full' ;
								                        $leave_day    = $row_leave['leave_day'] ; // should change to $days
								                        $cut_day  	  = '1' ;
								                        if ( $leave_day == '0.5' ){
								                        	$day_fullhalf = 'half' ;
								                        	$leave_day    = '1' ;
									                        $cut_day  	  = '0.5' ;
								                        }

								                        // save to leave more
								                        $leave_date_list = [] ;
							                        	$leave_work_day  = 0 ;
							                        	for ( $a = 1 ; $a <= $leave_day ; $a++ ){

							                        		$leave_type_mode   = 'working' ;
							                        		$leave_work_direct = 'no' ;
								                            $leave_work_day    = '0' ;
								                            $boolean_holiday   = false ;
								                            $boolean_off       = false ;

								                            // check if today is holiday
								                            $get_holiday = $mysqli->query("SELECT * FROM setting_holiday
								                            							   WHERE deleted_at IS NULL AND  holiday_date = '".$new_date."' LIMIT 1") ;
								                            if ( $get_holiday->num_rows > 0 ){
								                            	$leave_type_mode = 'holiday' ;
								                                $boolean_holiday = true ;
								                                $leave_work_day  = '1' ;
								                            }

								                            // check working days if today off
							                            	if ( !$boolean_holiday ){
							                            		$new_week_day = date('N', strtotime($new_date)) ;
							                            		$get_working  = $mysqli->query("SELECT * FROM setting_working
							                            										WHERE deleted_at IS NULL AND group_id = '".$row_staff['group_id']."' AND working_day = '".$new_week_day."' LIMIT 1") ;
							                            		if ( $get_working->num_rows > 0 ){
							                            			$row_working = $get_working->fetch_assoc() ;
							                            			if ( $row_working['working_on'] == 'no' ){
								                                        $leave_type_mode  = 'off' ;
								                                        $boolean_off      = true ;
								                                        $leave_work_day   = '1' ;
								                                    }
								                                    $leave_work_direct = $row_working['working_direct_day'] ;
							                            		}

							                            		if ( !$boolean_off ){
								                                    if ( $leave_days > 0 ){
								                                        $leave_work_day = $cut_day ;
								                                    }
								                                }

							                            	}

							                            	$leave_date_list[] = [
							                            		'leave_type_mode' => $leave_type_mode,
							                            		'leave_date'      => $new_date,
							                            		'leave_work_day'  => $leave_work_day,
							                            	] ;

								                            $new_date = date('Y-m-d', strtotime($new_date . '+1 days')) ;

								                            if ( !$boolean_holiday && !$boolean_off ){
								                                $leave_days = ( $leave_days - $leave_work_day ) ;   
								                            }

							                        	}

							                        	if ( $leave_days >= 0 ){
							                        		foreach ( $leave_date_list as $k_date => $v_date ){
								                            	// check total leave
									                        	$mysqli->query("INSERT INTO staff_leave_date
									                        					(staff_id, leave_id, leave_type, leave_type_mode, leave_date, leave_work_day, created_at, updated_at) VALUES
									                        					('".$row_staff['staff_id']."', '".$key."', '".$row_leave['leave_type']."', '".$v_date['leave_type_mode']."', '".$v_date['leave_date']."', '".$v_date['leave_work_day']."', '".TODAYDATE."', '".TODAYDATE."')") ;
							                        		}

							                        		if ( $boolean_ded ){
								                        		$mysqli->query("UPDATE staff_leave_year SET
								                        						leave_days = '".$leave_days."'
								                        						WHERE leave_year_id = '".$row_leave_year['leave_year_id']."'") ;
									                        }

									                        $boolean_leave_update = true ;
														
							                        	}else{
							                        		$error_update++; 
							                        	}
							                        }else{
						                        		$error_update++; 
						                        	}

												}

	            							}

	            						}else{
	            							$boolean_leave_update = true ;
	            						}

	            						if ( $boolean_leave_update ){

	            							$mysqli->query( "UPDATE staff_leave SET
														     leave_status 		  = '".$page_action."',
														     leave_updated_author = '".$_SESSION['system_id']."'
														     WHERE leave_id       = '".$key."'") ;

											$staff_list[]  = $value ;

											if ( $row_leave['staff_incharge_id'] != '0' ){
												$supervisor_list[] = [
													'super_id'   => $row_leave['staff_incharge_id'],
													'staff_name' => $row_leave['staff_name'],
													'staff_idno' => $row_leave['staff_idno']
												] ;
											}
	            						}


		        					}
								}

	                		}
	                		*/
	                		// end refer to update status part


	                	}
	                }

	            }

			}
		
			// refresh page
			header("Location:hr-leave.php?page_mode=".$page_mode."&page=".$page) ;
			$_SESSION['system_result'] = $error_message ;
			exit ;
		}

		// active menu bar
		$active_main_menu = 'hr' ;
		$active_sub_menu  = 'hr-leave' ;

		if ( ( $page_mode == 'new' && !permissionCheck($row_user, 'leave-new') ) ||
			 ( $page_mode == 'edit' && !permissionCheck($row_user, 'leave-update') ) ){
			header('Location: hr-leave.php') ;
			exit ;
		}

		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';

		// get all requires 
		// get all staff
		$staff_list   = [] ;
		$mysqli_staff = $mysqli->query("SELECT staff_id, staff_name, staff_idno FROM staff
										WHERE deleted_at IS NULL ".$user_branch_permission_sql) ;
		if ( $mysqli_staff->num_rows > 0 ){
			while ( $row_staff = $mysqli_staff->fetch_assoc() ){
				$staff_list[$row_staff['staff_id']] = dataFilter($row_staff['staff_name']) . ' ( ' . dataFilter($row_staff['staff_idno']) . ' )' ;
			}
		}

		$input_block = '' ;
		$admin_edit  = 'no' ;
		// if ( $submit_type == 'edit' && $row_page['leave_status'] != 'pending' ){
		// 	$input_block = 'disabled' ;
		// }
		if ( $row_user['user_permission'] != 'admin' ){
			if ( $submit_type == 'edit' && $row_page['leave_status'] != 'pending' ){
				$input_block = 'disabled' ;
			}
		}else{
			if ( $row_page['leave_status'] == 'confirmed' ){
				$admin_edit  = 'yes' ;
			}
		}

		?>

		<div class="warper container-fluid">  
		<div class='container' style="background-color: white; border-radius: 10px;">
            <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">	
				<h1><?= $lang['Leave']?> <small><?= $lang[$page_mode] ?></small></h1>
			</div>
			<?php
            if ($_SESSION['system_result'] != ''){
            	echo $_SESSION['system_result'] ;
                unset($_SESSION['system_result']) ;
            }
            ?>
            <div class="panel panel-default">
                <div class="panel-heading"></div>
                <div class="panel-body">
                    <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="hr-leave.php?page_mode=edit&page=<?= $page ?>&type=edit" novalidate="novalidate">

                    	<?php if ( $submit_type == 'new' ){ ?>
							<div class="form-group">
	                            <div class="col-sm-2 control-label"><?= $lang['Staff']?></div>
	                            <div class="col-sm-9">
	                            	<select name="staff_id" id="selectuser" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" required <?= $input_block ?> >
	                            		<option value=""><?= $lang['select']?></option>
										<?php
										if ( count($staff_list) > 0 ){
											foreach ( $staff_list as $k => $v ){
												echo '<option value="'.$k.'" '.( $k == $row_page['staff_id'] ? 'selected' : '').'>'.$v.'</option>' ;
											}
										}
										?>
									</select>
	                            </div>
	                        </div>
	                    <?php }else{ ?>
	                    	<input type="hidden" name="staff_id" value="<?= $row_page['staff_id'] ?>" />
	                    <?php } ?>

                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['type']?></div>
                            <div class="col-sm-9">
                                <select name="leave_type" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" required <?= $input_block ?> >
                            		<option value="unpaid" <?= ( $row_page['leave_type'] == 'unpaid' ? 'selected' : '' ) ?> ><?= $lang['Unpaid']?></option>
                            		<option value="annual" <?= ( $row_page['leave_type'] == 'annual' ? 'selected' : '' ) ?> ><?= $lang['Annual']?></option>
                            		<option value="sick" <?= ( $row_page['leave_type'] == 'sick' ? 'selected' : '' ) ?> ><?= $lang['Sick leave']?></option>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Full Half']?></div>
                            <div class="col-sm-9">
                                <select name="leave_fullhalf" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" id="leave_fullhalf" required <?= $input_block ?> >
                            		<option value="full" <?= ( $row_page['leave_day'] != '0.5' ? 'selected' : '' ) ?> ><?= $lang['Full']?></option>
                            		<option value="half" <?= ( $row_page['leave_day'] == '0.5' ? 'selected' : '' ) ?> ><?= $lang['Half']?></option>
								</select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['from']?></div>
                            <div class="col-sm-9">
                            	<input type="date" name="leave_from" value="<?= $row_page['leave_from'] ?>" class="form-control tab_index" required <?= $input_block ?> />
                            </div>
                        </div>
                        <div class="form-group" id="div_leave_to" <?= ( $row_page['leave_day'] == '0.5' ? 'style="display:none;"' : '' ) ?> >
                            <div class="col-sm-2 control-label"><?= $lang['to']?></div>
                            <div class="col-sm-9">
                            	<input type="date" name="leave_to" value="<?= $row_page['leave_to'] ?>" class="form-control tab_index" required <?= $input_block ?> />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['Reason']?></div>
                            <div class="col-sm-9">
                            	<input type="text" name="leave_reason" value="<?= $row_page['leave_reason'] ?>" class="form-control tab_index" required <?= $input_block ?> />
                            </div>
                        </div>
                        <?php if ( $submit_type == 'edit' ){ ?>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"><?= $lang['status']?></div>
	                            <div class="col-sm-9">
	                            	<select name="leave_status" class="chosen-select form-control tab_index ui-dropdown-select ui-search-input select2-basic-single" id="leave_fullhalf" required disabled >
	                            		<option value="pending" <?= ( $row_page['leave_status'] == 'pending' ? 'selected' : '' ) ?> ><?= $lang['Pending']?></option>
	                            		<option value="confirmed" <?= ( $row_page['leave_status'] == 'confirmed' ? 'selected' : '' ) ?> ><?= $lang['Confirmed']?></option>
	                            		<option value="rejected" <?= ( $row_page['leave_status'] == 'rejected' ? 'selected' : '' ) ?> ><?= $lang['Rejected']?></option>
									</select>
	                            </div>
	                        </div>
                        <?php } ?>
                        <div class="form-group">
                            <div class="col-sm-2 control-label"><?= $lang['photo']?></div>
                            <div class="col-sm-9">
                                <div class="file_upload">
                                    <div class="file_form">
                                        <input type="hidden" name="hide_image" value="<?= dataFilter($row_page['leave_file']) ?>" />
                                        <input type="hidden" name="remove_photo" value="">
                                        <input type="file" name="image" class="file_button control-label" <?= $input_block ?> />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if ($row_page['leave_file'] != ''){ ?>
	                        <div class="form-group form-group-pdf">
	                            <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
	                            <div class="col-sm-9">
	                            	<label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;<?= $lang['Remove File']?></label>
	                            	<a href="<?= PATH.'uploads/Leave/'.dataFilter($row_page['leave_file']) ?>" target="_blank"><?= $lang['Download']?></a>
		                        </div>
	                        </div>
                        <?php }else{ ?>
                            <input type="hidden" name="remove_photo" value="">
                        <?php } ?>

                        <?php if ( $row_page['leave_status'] == '' || $row_page['leave_status'] == 'pending' ){ ?>
	                        <div class="form-group">
	                            <div class="col-sm-2 control-label"></div>
	                            <div class="col-sm-9">
	                                <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit']?></button>
	                                <input type="hidden" name="hide" value="1" />
	                                <input type="hidden" name="hide_admin_edit" value="<?= $admin_edit ?>" />
	                                <input type="hidden" name="page_status" value="<?= $submit_type ?>">
	                            </div>
	                        </div>
	                    <?php } ?>

                    </form>
                </div>
            </div>
        </div>
		</div>

		<script>
		$(document).ready(function(){
			$("#leave_fullhalf").change(function(){
				var thisKey = $(this),
					thisVal = thisKey.val() ;
				if ( thisVal == 'half' ){
					$("#div_leave_to").hide() ;
				}else{
					$("#div_leave_to").show() ;
				}
			}) ;
		}) ;
		</script>

		<?php

	break ;
	
	// all type list
	case 'all' :
	default :
		
		// query type
		$search_query = '' ;
		$ty_type      = escapeString($_GET['ty_type']) ;
		$ist_type     = escapeString($_GET['ist_type']) ;
		$st_type      = escapeString($_GET['st_type']) ;
		$sort_by      = escapeString($_GET['sort_by']) ;
		$sort_by 	  = ( $sort_by != '' ? $sort_by : 'created_at' ) ;
	    $sort_by_type = escapeString($_GET['sort_by_type']) ;
		$sort_by_type = ( $sort_by_type != '' ? $sort_by_type : 'desc' ) ;
		$export       = escapeString($_GET['export']) ;
		
		// search query
		if ($search != ''){
			$search_query .= " AND ( a.leave_reason LIKE '%".$search."%' OR b.staff_name LIKE '%".$search."%' OR b.staff_idno LIKE '%".$search."%' )" ;
		}
		if ($ty_type != ''){
			$search_query .= " AND a.leave_type = '".$ty_type."'" ;
		}
		if ($ist_type != ''){
			$search_query .= " AND a.leave_incharge_status = '".$ist_type."'" ;
		}
		if ($st_type != ''){
			$search_query .= " AND a.leave_status = '".$st_type."'" ;
		}

		if ($search_from_to != ''){
			$search_query .= " AND ((a.leave_from BETWEEN '".$search_from_to."' AND '".$search_from_to."') || (a.leave_to BETWEEN '".$search_from_to."' AND '".$search_from_to."')) " ;
		}

		if ($search_name !='') {
			$search_query .= " AND b.staff_name like '%".$search_name."%' " ;
		}

		if ($search_idno !='') {
			$search_query .= " AND b.staff_idno like '%".$search_idno."%' " ;
		}

		// if ($search_date_from != '' && $search_date_to == ''){
		// 	$search_query .= " AND DATE(a.leave_from) like '%".$search_date_from."%' " ;
		// }

		// if ($search_date_from == '' && $search_date_to != ''){
		// 	$search_query .= " AND DATE(a.leave_to) like '%".$search_date_to."%' " ;
		// }

		if ($search_date_from != '' && $search_date_to == ''){
			$search_query .= " AND (DATE(a.leave_from) like '%".$search_date_from."%' OR (DATE(a.leave_from) <= '".$search_date_from."' AND DATE(a.leave_to) >= '".$search_date_from."' ) ) " ;
		}

		if ($search_date_from == '' && $search_date_to != ''){
			$search_query .= " AND (DATE(a.leave_to) like '%".$search_date_to."%' OR (DATE(a.leave_from) <= '".$search_date_to."' AND DATE(a.leave_to) >= '".$search_date_to."' ) ) " ;
		}
		
		if ($search_date_from != '' && $search_date_to != ''){
			$search_query .= " AND ((DATE(a.leave_from) >= '".$search_date_from."' AND DATE(a.leave_to) <= '".$search_date_to."') OR (DATE(a.leave_from) <= '".$search_date_from."' AND DATE(a.leave_to) >= '".$search_date_to."') OR (DATE(a.leave_from) >= '".$search_date_from."' AND DATE(a.leave_from) <= '".$search_date_to."') OR (DATE(a.leave_to) >= '".$search_date_from."' AND DATE(a.leave_to) <= '".$search_date_to."') ) " ;
		}

		// active menu bar
		$active_main_menu = 'hr' ;
		$active_sub_menu  = 'hr-leave' ;
		
		// form submit
		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){

			$result = 'failed-action' ;
			$page_action = $_POST['page_action'] ;

			// trash item
			switch ( $page_action ){
				case 'confirmed' :
				case 'rejected' :

					$result 		 = 'failed-check' ;

					$multiple        = $_POST['multiple_trash'] ;
					$supervisor_list = [] ;
					$staff_list      = [] ;
					$update_list     = [] ;
					$error_update    = 0 ;

					if ( arrayCheck($multiple) ){

						foreach ( $multiple as $key => $value ){

							// check if leave exsits
							$get_leave = $mysqli->query("SELECT * FROM staff_leave a
													 	 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
														 WHERE a.deleted_at IS NULL AND a.leave_id = '".$key."' AND a.leave_status != '".$page_action."' LIMIT 1") ;
							if ( $get_leave->num_rows > 0 ){

								$row_leave = $get_leave->fetch_assoc() ;

								if ( $row_leave['leave_type'] == 'unpaid' || $row_leave['leave_type'] == 'annual' || $row_leave['leave_type'] == 'sick' ){
									
									// deduct leave
									$new_date = date('Y-m-d', strtotime($row_leave['leave_from'])) ;
            						$new_year = date('Y', strtotime($row_leave['leave_from'])) ;

            						$get_previous_leave = $mysqli->query("SELECT * FROM staff_leave_date
            															  WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND leave_id = '".$key."' AND  leave_type = '".$row_leave['leave_type']."'") ;
            						if ( $get_previous_leave->num_rows > 0 ){

            							$count = 0 ;
            							while ( $previous_leave = $get_previous_leave->fetch_assoc() ){
            								if ( $previous_leave['leave_type_mode'] == 'working' ){
            									$count += $previous_leave['leave_work_day'] ;
            								}
            								$mysqli->query("UPDATE staff_leave_date SET
            												deleted_at = '".TODAYDATE."'
            												WHERE leave_date_id = '".$previous_leave['leave_date_id']."'") ;
            							}

            							if ( $count > 0 ){
											$mysqli->query("UPDATE staff_leave_year SET
															leave_days = leave_days + '".$count."'
															WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND leave_type = '".$row_leave['leave_type']."' AND leave_year_from <= '".$row_leave['leave_from']."' AND leave_year_to >= '".$row_leave['leave_from']."'") ;
            							}
            						}

            						// confirmed leave
            						$boolean_leave_update = false ;
            						if ( $page_action == 'confirmed' ){

            							// check if staff exists or not
            							$get_staff = $mysqli->query("SELECT * FROM staff
            														 WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' LIMIT 1") ;
            							if ( $get_staff->num_rows > 0 ){

            								$row_staff = $get_staff->fetch_assoc() ;

            								// open leave year for selected staff
            								setStaffLeaveYear($row_leave['staff_id']) ;

            								// check staff leave 
											// get current staff total leave
											$get_leave_year = $mysqli->query("SELECT leave_year_id, SUM(leave_days) as leave_total_days FROM staff_leave_year
																		 	  WHERE deleted_at IS NULL AND staff_id = '".$row_leave['staff_id']."' AND  leave_type = '".$row_leave['leave_type']."' AND leave_year_from <= '".$row_leave['leave_from']."' AND leave_year_to >= '".$row_leave['leave_from']."'") ;

											if ( $get_leave_year->num_rows > 0 ){

												$leave_days         = 0 ;
						                        $boolean_ded        = false ;
						                        if ( $get_leave_year->num_rows > 0 ){
						                        	$row_leave_year = $get_leave_year->fetch_assoc() ;
						                            $leave_days  	= $row_leave_year['leave_total_days'] ;
						                            $boolean_ded 	= true ;
						                        }

						                        if ( $leave_days > 0 ){

							                        // check if full day or half day
							                        $day_fullhalf = 'full' ;
							                        $leave_day    = $row_leave['leave_day'] ; // should change to $days
							                        $cut_day  	  = '1' ;
							                        if ( $leave_day == '0.5' ){
							                        	$day_fullhalf = 'half' ;
							                        	$leave_day    = '1' ;
								                        $cut_day  	  = '0.5' ;
							                        }

							                        // save to leave more
							                        $leave_date_list = [] ;
						                        	$leave_work_day  = 0 ;
						                        	for ( $a = 1 ; $a <= $leave_day ; $a++ ){

						                        		$leave_type_mode   = 'working' ;
						                        		$leave_work_direct = 'no' ;
							                            $leave_work_day    = '0' ;
							                            $boolean_holiday   = false ;
							                            $boolean_off       = false ;

							                            // check if today is holiday
							                            $get_holiday = $mysqli->query("SELECT * FROM setting_holiday
							                            							   WHERE deleted_at IS NULL AND  holiday_date = '".$new_date."' LIMIT 1") ;
							                            if ( $get_holiday->num_rows > 0 ){
							                            	$leave_type_mode = 'holiday' ;
							                                $boolean_holiday = true ;
							                                $leave_work_day  = '1' ;
							                            }

							                            // check working days if today off
						                            	if ( !$boolean_holiday ){
						                            		$new_week_day = date('N', strtotime($new_date)) ;
						                            		$get_working  = $mysqli->query("SELECT * FROM setting_working
						                            										WHERE deleted_at IS NULL AND group_id = '".$row_staff['group_id']."' AND working_day = '".$new_week_day."' LIMIT 1") ;
						                            		if ( $get_working->num_rows > 0 ){
						                            			$row_working = $get_working->fetch_assoc() ;
						                            			if ( $row_working['working_on'] == 'no' ){
							                                        $leave_type_mode  = 'off' ;
							                                        $boolean_off      = true ;
							                                        $leave_work_day   = '1' ;
							                                    }
							                                    $leave_work_direct = $row_working['working_direct_day'] ;
						                            		}

						                            		if ( !$boolean_off ){
							                                    $leave_work_day = $cut_day ;
							                                }
						                            	}

						                            	$leave_date_list[] = [
						                            		'leave_type_mode' => $leave_type_mode,
						                            		'leave_date'      => $new_date,
						                            		'leave_work_day'  => $leave_work_day,
						                            	] ;

							                            $new_date = date('Y-m-d', strtotime($new_date . '+1 days')) ;

							                            if ( !$boolean_holiday && !$boolean_off ){
							                                $leave_days = ( $leave_days - $leave_work_day ) ;   
							                            }

						                        	}


						                        	if ( $leave_days >= 0 ){
						                        		foreach ( $leave_date_list as $k_date => $v_date ){
							                            	// check total leave
								                        	$mysqli->query("INSERT INTO staff_leave_date
								                        					(staff_id, leave_id, leave_type, leave_type_mode, leave_date, leave_work_day, created_at, updated_at) VALUES
								                        					('".$row_staff['staff_id']."', '".$key."', '".$row_leave['leave_type']."', '".$v_date['leave_type_mode']."', '".$v_date['leave_date']."', '".$v_date['leave_work_day']."', '".TODAYDATE."', '".TODAYDATE."')") ;
						                        		}

						                        		if ( $boolean_ded ){
							                        		$mysqli->query("UPDATE staff_leave_year SET
							                        						leave_days = '".$leave_days."'
							                        						WHERE leave_year_id = '".$row_leave_year['leave_year_id']."'") ;
								                        }

								                        $boolean_leave_update = true ;
													
						                        	}else{
						                        		$error_update++; 
						                        	}
						                        }else{
					                        		$error_update++; 
					                        	}

											}

            							}

            						}else{
            							$boolean_leave_update = true ;
            						}

            						if ( $boolean_leave_update ){

            							$mysqli->query( "UPDATE staff_leave SET
													     leave_status 		  = '".$page_action."',
													     leave_updated_author = '".$_SESSION['system_id']."'
													     WHERE leave_id       = '".$key."'") ;

            							pushToUserCron( 'staff_leave', $key, $value, 'Leave '.ucwords($page_action), 'Your leave has been '.$page_action.'.' ) ;

										$staff_list[]  = $value ;

										if ( $row_leave['staff_incharge_id'] != '0' ){
											pushToUserCron( 'staff_leave', $key, $row_leave['staff_incharge_id'], 'Staff Leave '.ucwords($page_action), 'Staff '.$row_leave['staff_name'].' ('.$row_leave['staff_idno'].') leave has been '.$page_action.'.' ) ;
										}
            						}

								}

							}
						}

						// push notification for those staff
						if ( count( $staff_list ) > 0 ){
							if ( $error_update == 0 ){
								$result = 'success-update' ;
							}else{
								$result = 'success-some-update' ;
							}
						}else{
							$result = 'success-some-update' ;
						}

					}

				break ;
			}

			$_SESSION['system_result'] = $result ;
		}
		
		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from = ($product_page - 1) * LIMIT ; //end next and prev page
		
		// set search url
		$search_url = 'search='.$search.'&search_name='.$search_name.'&search_idno='.$search_idno.'&search_date_from='.$search_date_from.'&search_date_to='.$search_date_to.'&ty_type='.$ty_type.'&ist_type='.$ist_type.'&st_type='.$st_type.'&sort_by='.$sort_by.'&sort_by_type='.$sort_by_type ;
		
		// page query
		$mysqli_query = "SELECT a.leave_id as leave_id, a.leave_type, a.leave_from, a.leave_to, a.leave_day, a.leave_file, a.leave_reason, a.leave_incharge_status, a.leave_status, a.leave_updated_author, a.created_at, a.updated_at, b.staff_id, b.staff_name, b.staff_idno FROM staff_leave a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 WHERE a.deleted_at IS NULL " . $search_query.$user_branch_permission_sql_b ;
		// export excel
		if ( $export == 'yes' ){

			include 'PhpExcel/PHPExcel.php' ;

			$page_filename = 'Leave-'.date( 'Ymd', time() ) ;
		    $objPHPExcel   = new PHPExcel() ;
		    $objPHPExcel->getProperties()
		                ->setCreator(COMPANY)
		                ->setTitle(COMPANY)
		                ->setSubject(COMPANY)
		                ->setDescription(COMPANY)
		                ->setKeywords(COMPANY)
		                ->setCategory(COMPANY) ;

		    $objPHPExcel->getActiveSheet()->setTitle( $page_filename ) ;
		    $objPHPExcel->setActiveSheetIndex(0);
		    $objWriter   = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel5' ) ;

		    // default parameter
		    $count       = 1 ;
		    $char        = 'A' ;
		    $count_row   = 1 ;

		    $array_title = array( 'No.', 'Request By', 'Type', 'From ~ To', 'Reason', 'Status', 'Created Date', 'Updated Date' ) ;

		    $newChar     = $char ;
		    foreach( $array_title as $k => $v ){
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( $newChar.$count, $v ) ;
                $newChar++ ;
		    }
		    $count++ ;

			$row_q = $mysqli->query($mysqli_query." ORDER BY a.created_at DESC") ;
			if ( $row_q->num_rows > 0 ){
				while ( $row = $row_q->fetch_assoc() ){
					$newChar = $char ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, $count_row ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, dataFilter($row['staff_name']).' ( '.dataFilter($row['staff_idno']).' )' ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, ucwords($row['leave_type']) ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, dataFilter($row['leave_from']).' ~ '.dataFilter($row['leave_to']).' ('.dataFilter($row['leave_day']).')' ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, dataFilter($row['leave_reason']) ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, dataFilter($row['leave_status']) ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, resetDateFormat($row['created_at']) ) ;
	                $objPHPExcel->setActiveSheetIndex(0)->setCellValue( ($newChar++).$count, resetDateFormat($row['updated_at']) ) ;

	                $count++ ;
	                $count_row++ ;
				}
			}

			// Submission from
		    header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ; 
		    header( 'Content-Disposition: attachment;filename="'.$page_filename.'.xls"' ) ;
		    header( 'Cache-Control: max-age=0' ) ;
		    // save to pc
		    $objWriter->save('php://output') ;
		    header( "Refresh: 0" ) ;
			exit ;
		}

		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY a.leave_from DESC, a.created_at DESC LIMIT $start_from, " . LIMIT) ;
		
		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query) ;
		
		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;
		
		?>
		<!-- Header Ends -->
		<div class="warper container-fluid">   
		<div class='container' style="background-color: white; border-radius: 10px;">
            <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
                <div class="row">
                    <div class="pull-left col"> 
						<h1><?= $lang['Leave'] ?> <small><?= $lang['list'] ?></small></h1>
					</div>
					<div class="pull-right col">
				<?php if ( permissionCheck($row_user, 'leave-new') ){ ?>
					<a href="hr-leave.php?page_mode=new"  class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;" target="_blank"><?= $lang['add_new'] ?></a>
				<?php } ?>
        		<a href="?<?= $search_url ?>&export=yes" target="_blank"  class="btn" style="color:white;margin:5px;background-color: #5e5bd0;margin-top: 5px;"><?= $lang['export_as'] ?> Excel</a>
        			</div>
        		</div>
			</div>

			<?php
			if ($_SESSION['system_result'] != ''){
				switch($_SESSION['system_result']){
					case 'failed-action' : 
						echo '<div class="result_error">'.$lang['Sorry invalid action type'].'</div>' ;	
					break ;
					case 'failed-check' : 
						echo '<div class="result_error">'.$lang['Sorry please select at least one'].'</div>' ;	
					break ;
					case 'success-some-update' :
						echo '<div class="result_error">'.$lang['Sorry some of the staff not enough day to confirm'].'</div>' ;	
					break ;
					case 'success-update' :
						echo '<div class="result_success">'.$lang['Thank you status updated sucessfully'].'</div>' ;	
					break ;
				}
				unset($_SESSION['system_result']) ;
			}
			?>

			<div class="panel panel-default" id="basic-table-title">
		        <div class="panel-heading">search</div>
		        <div class="panel-body">
		            <form method="get" class="form-horizontal">

		            	<div class="form-group">
		                    <label class="col-sm-2 control-label">Staff Name</label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
		                    </div>
		                </div>

		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Staff ID</label>
		                    <div class="col-sm-9">
		                        <input type="text" name="search_idno" value="<?= $search_idno ?>" class="form-control" />
		                    </div>
		                </div>

		            	<div class="form-group">
		                    <label class="col-sm-2 control-label">Type</label>
		                    <div class="col-sm-9">
		                        <select name="ty_type" class="form-control">
			                        <option value="" <?= ($ty_type == '' ? 'selected' : '') ?> ><?= $lang['All'] ?></option>
							    	<option value="unpaid" <?= ($ty_type == 'unpaid' ? 'selected' : '') ?> ><?= $lang['Unpaid'] ?></option>
							    	<option value="annual" <?= ($ty_type == 'annual' ? 'selected' : '') ?> ><?= $lang['Annual'] ?></option>
							    	<option value="sick" <?= ($ty_type == 'sick' ? 'selected' : '') ?> ><?= $lang['Sick leave'] ?></option>
							    </select>
		                    </div>
		                </div>

		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Status</label>
		                    <div class="col-sm-9">
		                        <select name="st_type" class="form-control">
			                        <option value="" <?= ($st_type == '' ? 'selected' : '') ?> ><?= $lang['All'] ?></option>
							    	<option value="pending" <?= ($st_type == 'pending' ? 'selected' : '') ?> ><?= $lang['Pending'] ?></option>
							    	<option value="confirmed" <?= ($st_type == 'confirmed' ? 'selected' : '') ?> ><?= $lang['Confirmed'] ?></option>
							    	<option value="rejected" <?= ($st_type == 'rejected' ? 'selected' : '') ?> ><?= $lang['Rejected'] ?></option>
							    </select>
		                    </div>
		                </div>

		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Date From</label>
		                    <div class="col-sm-9">
		                        <input type="date" name="search_date_from" value="<?= $search_date_from ?>" class="form-control" />
		                    </div>
		                </div>

		                <div class="form-group">
		                    <label class="col-sm-2 control-label">Date To</label>
		                    <div class="col-sm-9">
		                        <input type="date" name="search_date_to" value="<?= $search_date_to ?>" class="form-control" />
		                    </div>
		                </div>

		                <div class="form-group">
		                    <div class="col-sm-11">
		                        <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
		                        <input type="hidden" name="ist_type" value="<?= $ist_type ?>" />
		                        <input type="hidden" name="sort_by" value="<?= $sort_by ?>" />
		                        <input type="hidden" name="sort_by_type" value="<?= $sort_by_type ?>" />
		                        <button type="submit" class="btn" style="color:white;float:right;background-color: #5e5bd0;margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
		                    </div>
		                </div>
		            </form>
		        </div>
		    </div>

            <form method="post">

            	<?php if ( permissionCheck($row_user, 'leave-update') ){ ?>
                <div class="panel panel-default">
                    <div class="panel-body">
                        <select name="page_action" class="form-control selectpicker">
                            <option value=""><?= $lang['select'] ?></option>
                            <option value="confirmed"><?= $lang['Confirmed'] ?></option>
                            <option value="rejected"><?= $lang['Rejected'] ?></option>
                        </select>
                        <input type="hidden" name="hide" value="1" />
                        <input type="hidden" name="hide_status" value="action" />
                        <input type="submit" class="btn" style="color:white;background-color: #5e5bd0;width: 100px;" value="<?= $lang['submit'] ?>" />
                    </div>
                </div>
                <?php } ?>

                <div class="panel panel-default">
                    <div class="panel-heading"></div>
                    <div class="panel-body">
						<table cellpadding="0" cellspacing="0" border="0" class="listing-table responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th width="50">&nbsp;</th>
									<th width="50">&nbsp;</th>
									<th><?= $lang['Request By'] ?></th>
									<th>Type</th>
									<th><?= $lang['From To'] ?></th>
									<th><?= $lang['File'] ?></th>
									<th><?= $lang['Reason'] ?></th>
									<th>Status</th>
									<th><?= $lang['Date Created'] ?></th>
									<th><?= $lang['Date Approved'] ?></th>
								</tr>
							</thead>
							<tbody>
								<?php

								$get_user_id = [] ;
								$array_page  = [] ;
								if ( $mysqli_page->num_rows > 0 ){
									while ( $row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ){
										if ( $row_page['leave_updated_author'] > 0 ){
											$get_user_id[$row_page['leave_updated_author']] = $row_page['leave_updated_author'] ;
										}
										$array_page[]  = $row_page ;
									}
								}

								// get author id
								$author_query = $mysqli->query( "SELECT user_id, user_name FROM system_user
																 WHERE user_trash = '0' AND user_id IN (".implode( ', ', $get_user_id ).")" ) ;
								$array_author = [] ;
								if ( $author_query->num_rows > 0 ){
									while ( $row_author = $author_query->fetch_assoc() ){
										$array_author[$row_author['user_id']] = $row_author['user_name'] ;
									}
								}

								if ( arrayCheck($array_page) ){
									foreach ( $array_page as $row_page ){

										if ( $row_page['leave_incharge_status'] == 'pending' ){
											$button_incharge =  '<button class="btn btn-xs bg-yellow margin" type="button"><b>'.$lang['PENDING'].'</b></button>' ;
										}elseif ( $row_page['leave_incharge_status'] == 'confirmed' ){
											$button_incharge = '<button class="btn btn-xs bg-green margin" type="button"><b>'.$lang['CONFIRMED'].'</b></button>' ;
										}else{
											$button_incharge = '<button class="btn btn-xs bg-red margin" type="button"><b>'.$lang['REJECTED'].'</b></button>' ;
										}

										if ( $row_page['leave_status'] == 'pending' ){
											$button =  '<button class="btn btn-xs bg-yellow margin" type="button"><b>'.$lang['PENDING'].'</b></button>' ;
										}elseif ( $row_page['leave_status'] == 'confirmed' ){
											$button = '<button class="btn btn-xs bg-green margin" type="button"><b>'.$lang['CONFIRMED'].'</b></button>' ;
										}else{
											$button = '<button class="btn btn-xs bg-red margin" type="button"><b>'.$lang['REJECTED'].'</b></button>' ;
										}

										echo '
										 <tr class="odd gradeX" id="'.$row_page['leave_id'].'">
											<td>' ;
												// if ( $row_page['leave_status'] == 'pending' ){
													echo '
													<div class="checkbox multiple_trash">
														<input type="checkbox" name="multiple_trash['.$row_page['leave_id'].']" class="trash_button" value="'.$row_page['staff_id'].'">
														<label for="checkbox1"></label>
													</div>' ;
												// }
											echo '
											</td>
											<td class="align_center">' ;
												if ( permissionCheck($row_user, 'leave-update') ){
													echo '
										 			<a title="'.$lang['Edit Current'].$lang['Leave'].'" href="hr-leave.php?page_mode=edit&page='.$row_page['leave_id'].'"><i class="fa fa-edit"></i></a>' ;
										 			if($row_page['leave_status'] == 'pending'){
										 				echo '
														<span class="order_print_span">|</span>
											 			<a title="'.$lang['trash'].$lang['Leave'].'" href="hr-leave.php?page_mode=trash&page='.$row_page['leave_id'].'&ist_type='.$ist_type.'" class="leave_trash"><i class="fa fa-trash"></i></a>
											 			' ;
										 			}													
												}else{
													echo '-' ;
												}
											echo '
										 	</td>
											<td>'.dataFilter($row_page['staff_name']).'<br />( '.dataFilter($row_page['staff_idno']).' )</td>
											<td class="align_center">'.ucwords($lang[$row_page['leave_type']]).'</td>
											<td class="align_center">'.dataFilter($row_page['leave_from']) . ( $row_page['leave_from'] != $row_page['leave_to'] ? ' ~ ' . dataFilter($row_page['leave_to']) : '' ) .' ('.dataFilter($row_page['leave_day']).')</td>
											<td>' ;
												if ( $row_page['leave_file'] != '' ){
													echo '
													<a href="'.PATH.'/uploads/Leave/'.dataFilter($row_page['leave_file']).'" class="fancybox">
														<img src="'.PATH.'/uploads/Leave/'.dataFilter($row_page['leave_file']).'" style="width:50px" />
													</a>' ;
												}
											echo '
											</td>
											<td>'.dataFilter($row_page['leave_reason']).'</td>
											<td class="align_center">'.$button.'</td>
											<td class="align_center">'.resetDateTimeFormat($row_page['created_at']).'</td>
											<td class="align_center">
												'.resetDateTimeFormat($row_page['updated_at']).'
												'.( $row_page['leave_updated_author'] > 0 ? '<br /> ( '.$array_author[$row_page['leave_updated_author']].' )' : '' ).'
											</td>
										</tr>' ;
									}
								}else{
									echo '
									<tr class="odd gradeX">
										<td class="border_none">'.$lang['no_data'].'</td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
						<?= $page_pagination['page_pagination'] ?>
                    </div>
                </div>
			</form>
		</div>
		</div>
		<script>
			$(document).ready(function(){
				$(document).on("click",".leave_trash", function(){
					if (confirm("are you sure want to remove this item.")) {
		    
				} else {
					return false;
				}
				})											 				
			});
			
		</script>
		<?php
	break ;
}
// footer
include 'requires/page_footer.php' ;
?>