<?PHP
         
    include './conn.php';
	session_start();
	$staff_id = $_SESSION['staff_id'];
	$leave_duration = $_POST['leave_duration'];
	$leave_type = $_POST['leave_type'];
	$leave_date = $_POST['leave_date'];
	$start_date = $_POST['start_date'];
	$end_date = $_POST['end_date'];
	$no_of_days = $_POST['days_requested'];
	$status = "Pending";
	$reason = $_POST['reason'];

	
	$connection = @mysqli_connect("localhost", "root", "") or die(mysqli_error($conn));
	
	$sql4 = "SELECT * FROM lms.leave_statistics WHERE staff_id = '".$staff_id."' AND leave_type = '".$leave_type."'";
	$result4 = mysqli_query($conn, $sql4) or die(mysqli_error($conn));
	while($row4 = mysqli_fetch_array($result4))
	{
		$maximum_leaves = $row4['maximum_leaves'];
		$leaves_taken = $row4['leaves_taken'];
		$maximum_leaves_this_year = $row4['maximum_leaves_this_year'];
	}
	
	$new = intval($leaves_taken) + intval($no_of_days);
	$balance_leaves = intval($maximum_leaves)- intval($leaves_taken);

	$new2 = intval($maximum_leaves) + intval($balance_leaves);
	
	if($no_of_days > $maximum_leaves_this_year)
	{
		echo	"<script>
				alert('Maximum ".$maximum_leaves_this_year." Days Allowed.');
				window.location=\"apply_leave.php\";</script>";
	}
	if($new > $maximum_leaves_this_year)
	{
		echo	"<script>
				alert('You have already taken your maximum " .$leaves_taken." leaves this month, Now you can only request for ".$balance_leaves." days this year');
				window.location=\"apply_leave.php\";</script>";
	}
	else
	{
	
		if($leave_duration == "one_day")
		{
			$sql = "SELECT * FROM lms.leave_requests WHERE start_date = '".$leave_date."' AND end_date = '".$leave_date."' AND staff_id = '".$staff_id."'";
			$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
			if(mysqli_num_rows($result) == 0 )
			{
				$sql3 = "INSERT INTO lms.leave_requests VALUES ('".$staff_id."', '".$leave_type."', '".$leave_date."', '".$leave_date."', '1', '".date("Y-m-d")."', '".$status."', '".$reason."')";
				mysqli_query($conn, $sql3) or die(mysqli_error($conn));
			echo	"<script>
					alert(\"Leave Request Submitted.\");
					window.location=\"apply_leave.php\";</script>";
			}
			else
			{
				echo	"<script>
					alert(\"You have already taken a leave for these days !.\");
					window.location=\"apply_leave.php\";</script>";
			}
		}
		
		else if($leave_duration == "multiple_days")
		{
			$sql1 = "SELECT start_date, end_date FROM lms.leave_requests WHERE  '".$start_date."' BETWEEN start_date AND end_date AND staff_id = '".$staff_id."'";
		
			$sql2 = "SELECT start_date, end_date FROM lms.leave_requests WHERE  '".$end_date."' BETWEEN start_date AND end_date AND staff_id = '".$staff_id."'";
		
			$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn));
			$result2 = mysqli_query($conn, $sql2) or die(mysqli_error($conn));
		
			if(mysqli_num_rows($result1) == 0 && mysqli_num_rows($result2) == 0)
			{
				
				$sql3 = "INSERT INTO lms.leave_requests VALUES ('".$staff_id."', '".$leave_type."', '".$start_date."', '".$end_date."', '".$no_of_days."', '".date("Y-m-d")."', '".$status."', '".$reason."')";
				mysqli_query($conn, $sql3) or die(mysqli_error($conn));
			echo	"<script>
					alert(\"Leave Request Submitted.\");
					window.location=\"apply_leave.php\";</script>";
			}
			else
			{
				echo	"<script>
							alert(\"You have already taken a leave for these days !.\");
							window.location=\"apply_leave.php\";</script>";
			}
		}
	}
	
	mysqli_close($conn);
	
	
?>