<?php
session_start();
require_once '../../Server/connection.php';


    $leavetype_id  = $_POST['leavetype_id'];
	$query153 = "select * from leave_type where leaveType_id='$leavetype_id'";
    $result153 = $conn->query($query153);
    $row153 = $result153->fetch_array();
    $leaveDur = $row153['duration'];
	
	if($leavetype_id==1){
	$staff_id  = $_POST['staff_id'];
	$leaveDate  = $_POST['leaveDate'];
	$leaveEndDate = date('Y-m-d', strtotime($leaveDate.' +$leaveDur day'));
	$remarks  = addslashes($_POST['remarks']);
	$upl_status = $_POST['upl_status'];
	
		if($upl_status=='Yes'){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql12 = "INSERT INTO staffdocs (staff_id,fileName) 
				VALUES ('$staff_id','$location')";

		$resFSql12 = $conn->query($fSql12);
	          }
			
              }else{
				  
		$resFSql12 = 'Yes';
				  
			  }
			  
	 $fSql122 = "INSERT INTO leaves (leaveType_id,staff_id,leavestartDate,leaveendDate,remarks) 
				VALUES ('$leavetype_id','$staff_id','$leaveDate','$leaveEndDate','$remarks')";

	 $resFSql122 = $conn->query($fSql122);


	 
	 
	 if($resFSql12&&$resFSql122){
		 
		$_SESSION['success_message'] = "Staff Has Been Granted Leave Successfully";
  header("Location: ../grantLeave.php");
  exit();
		 
	 }
	
	
	
	
	}else if($leavetype_id==2){
	$staff_id  = $_POST['staff_id1'];
    $leaveDate  = $_POST['leaveDate'];
	$leaveEndDate = date('Y-m-d', strtotime($leaveDate.' +$leaveDur day'));
    $remarks  = addslashes($_POST['remarks']);
    $upl_status = $_POST['upl_status'];	
	
			if($upl_status=='Yes'){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql12 = "INSERT INTO staffdocs (staff_id,fileName) 
				VALUES ('$staff_id','$location')";

		$resFSql12 = $conn->query($fSql12);
	          }
			
              }else{
				  
		$resFSql12 = 'Yes';
				  
			  }
			  
	 $fSql122 = "INSERT INTO leaves (leaveType_id,staff_id,leavestartDate,leaveendDate,remarks) 
				VALUES ('$leavetype_id','$staff_id','$leaveDate','$leaveEndDate','$remarks')";

	 $resFSql122 = $conn->query($fSql122);


	 
	 
	 if($resFSql12&&$resFSql122){
		 
		$_SESSION['success_message'] = "Staff Has Been Granted Leave Successfully";
  header("Location: ../grantLeave.php");
  exit();
		 
	 }
	
	
	
	}else{
	$staff_id  = $_POST['staff_id2'];
    $leaveDate  = $_POST['leaveDate'];
	$leaveEndDate = date('Y-m-d', strtotime($leaveDate.' +$leaveDur day'));
    $remarks  = addslashes($_POST['remarks']);	
	$upl_status = $_POST['upl_status'];
	
			if($upl_status=='Yes'){
  foreach ($_FILES["file"]["name"] as $key => $name){
		$newFilename = time() . "_" . $name;
		move_uploaded_file($_FILES["file"]["tmp_name"][$key],'../../files/'.$newFilename);
		$location = '../../files/' . $newFilename;
		
		$fSql12 = "INSERT INTO staffdocs (staff_id,fileName) 
				VALUES ('$staff_id','$location')";

		$resFSql12 = $conn->query($fSql12);
	          }
			
              }else{
				  
		$resFSql12 = 'Yes';
				  
			  }
			  
	 $fSql122 = "INSERT INTO leaves (leaveType_id,staff_id,leavestartDate,leaveendDate,remarks) 
				VALUES ('$leavetype_id','$staff_id','$leaveDate','$leaveEndDate','$remarks')";

	 $resFSql122 = $conn->query($fSql122);


	 
	 
	 if($resFSql12&&$resFSql122){
		 
		$_SESSION['success_message'] = "Staff Has Been Granted Leave Successfully";
  header("Location: ../grantLeave.php");
  exit();
		 
	 }
	
	
	}
    
	
?>