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

    $staff_id  = $_POST['staff_id'];
	$permDate  = $_POST['permDate'];
	$permEndDate  = $_POST['permEndDate'];
	$remarks  = addslashes($_POST['remarks']);
	$upl_status = $_POST['upl_status'];
	
	$perm_Date = date('Y-m-d', strtotime($permDate));
	$perm_EndDate = date('Y-m-d', strtotime($permEndDate));
	
	 $current_date = date("Y-m-d");
	                         $d1 = strtotime("$perm_Date");
                             $d2 = strtotime("$perm_EndDate");
                             $min_date = min($d1, $d2);
                             $max_date = max($d1, $d2);
                                    $i = 0;
				while (($min_date = strtotime("+1 YEAR", $min_date)) <= $max_date) {
                                   $i++;
                                      }
									  $Year = $i;
									  $age=$Year;
									  
							if($age>0) {
									$age=$Year; 
										 
									 }else{
										 
						$d11 = strtotime("$perm_Date");
                             $d22 = strtotime("$perm_EndDate");
                             $min_date = min($d11, $d22);
                             $max_date = max($d11, $d22);
                                    $i = 0;
				while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
                                   $i++;
                                      }					  
										  
								$Month = $i;		  
								$age1 = $Month.' '.'Months';
                                  
                                  if($age1>0){
									 $age =  $age1;
								      }else{
									$d111 = strtotime("$perm_Date");
                             $d222 = strtotime("$perm_EndDate");
                             $min_date = min($d111, $d222);
                             $max_date = max($d111, $d222);
                                    $i = 0;
				while (($min_date = strtotime("+1 DAY", $min_date)) <= $max_date) {
                                   $i++;
                                      }	

                                $day = $i;		  
								$age1 = $day.' '.'Days';									  
								$age =  $age1;	  
									  }								  
										 
									 }
	
	
	
	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 staff_permission (staff_id,perm_date,permend_date,duration,remarks) 
				VALUES ('$staff_id','$perm_Date','$perm_EndDate','$age','$remarks')";

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


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

?>