<?php 
session_start();
require_once '../../Server/connection.php';
$staff_id = $_SESSION['staff_id'];
$query153 = "select * from staff where staff_id='$staff_id'";
$result153 = $conn->query($query153);
$row153 = $result153->fetch_array();
$dept_id = $row153['dept_id'];

$assigned_by = $staff_id;

$query154 = "select * from staff where dept_id='$dept_id' and staffcate_id=3";
$result154 = $conn->query($query154);
$row154 = $result154->fetch_array();
$assigned_to = $row154['staff_id'];

if($_POST){
  $leaveType_id = $_POST['leaveType_id'];
  $leave_remarks = addslashes($_POST['leave_remarks']);
  
  if($leaveType_id==1){
  $days_requested = $_POST['days_requested_annual'];  
  }else if($leaveType_id==2){
  $days_requested = $_POST['days_requested_maternity'];  
  }else if($leaveType_id==3){
  $days_requested = $_POST['days_requested_paternity'];  
  }else{
  $days_requested = $_POST['days_requested'];  
  }
  
  
  $action = "fowarded";
  $pendStatus = 0;
  $perm_id = 123;
  
  
  $year=date("Y");
  
  $sql22 = "Select * from leave_requests where year='$year'";
  $result22 = $conn->query($sql22);
  $num22 = mysqli_num_rows($result22);
  
  if($num22>=0 && $num22<9){
  $la_No = $num22 + 1;
  $y = date("Y");
  $leaveRequestNo = "EECL/LA"."/".$y."/".'000'.$la_No;  
    }else if($num22>=9 && $num22<99){
   $la_No = $num22 + 1;
  $y = date("Y");
  $leaveRequestNo = "EECL/LA"."/".$y."/".'00'.$la_No;  	 
	 }else if($num22>=99 && $num22<999){
   $la_No = $num22 + 1;
  $y = date("Y");
  $leaveRequestNo = "EECL/LA"."/".$y."/".'0'.$la_No;  	 
	 }else{
	$la_No = $num22 + 1;
  $y = date("Y");
  $leaveRequestNo = "EECL/LA"."/".$y."/".$la_No;  	 
	 }
	 
	 $date = date("Y-m-d");
	$month1 = date('m', strtotime($date));
	$year = date('Y', strtotime($date));
	
	if($month1==1){
	 $month = 'JAN';
	  }else if($month1==2){
	 $month = 'FEB';	  
	  }else if($month1==3){
	$month = 'MARCH';	  
	  }else if($month1==4){
	$month = 'APR';		  
	  }else if($month1==5){
	$month = 'MAY';	  
	  }else if($month1==6){
	$month = 'JUNE';	  
	  }else if($month1==7){
	$month = 'JULY';	  
	  }else if($month1==8){
	$month = 'AUG';	  
	  }else if($month1==9){
	$month = 'SEPT';	  
	  }else if($month1==10){
	$month = 'OCT';	  
	  }else if($month1==11){
	$month = 'NOV';	  
	  }else{
	$month = 'DEC';	  
	  }
  
  
  
  $invItemSql = "INSERT INTO leave_requests (leave_requestCode,staff_id,assigned_by,assigned_to,leaveType_id,permission_id,month,year,action,pendStatus) 
				VALUES ('$leaveRequestNo','$staff_id','$assigned_by','$assigned_to','$leaveType_id','$perm_id','$month','$year','$action','$pendStatus')";

  $result11 = $conn->query($invItemSql);
  
  
  $invItemSql1 = "INSERT INTO leavestatus (leave_requestCode,staff_remarks,leave_days_requested) 
				VALUES ('$leaveRequestNo','$leave_remarks','$days_requested')";

  $result12 = $conn->query($invItemSql1);
  
  if($result11&&$result12){
					
			$_SESSION['success_message'] = "Leave Application Submitted Successfully";
                       header("Location: ../request_leave.php");
                             exit();
			
            }
  
  
  
  
    
	  
  
}