<?PHP
	include './conn.php';

	$first_name = $_POST['first_name'];
	$middle_name = $_POST['middle_name'];
	$last_name = $_POST['last_name'];
	$Staff_id = $_POST['Staff_id'];
	$password = $_POST['password'];
	$user_type = "Staff";
	$branch = $_POST['branch'];
	$reg_year = $_POST['date'];


	// Sql query
	$sql1 = "SELECT staff_id FROM lms.staff WHERE staff_id = '".$Staff_id."' ";
	
	$sql2 = "INSERT INTO lms.staff (staff_id, first_name, middle_name, last_name, reg_year, branch) VALUES ('$Staff_id','$first_name', '$middle_name', '$last_name', '$reg_year', '$branch')";
	$sql3 = "INSERT INTO lms.login (user_id, password, user_type) VALUES ('$Staff_id', '$password', '$user_type')"; 
	//$sql4 = "INSERT INTO leave_statistics (staff_id) VALUES ('$Staff_id')";
	$sql5 = "SELECT * FROM lms.leave_types";
	$result4 = mysqli_query($conn, $sql5);
	$result1 = mysqli_query($conn,$sql1);
	$row = mysqli_fetch_array($result1);
	if($row == 0)
	{
		// Firing query
		$result2 = mysqli_query($conn,$sql2);
		$result3 = mysqli_query($conn,$sql3);
		while($row4 = mysqli_fetch_array($result4))
		{
			$leave_type = $row4['leave_type'];
			$no_of_days = $row4['no_of_days'];
			$sql4 = "INSERT INTO lms.leave_statistics (staff_id, leave_type, maximum_leaves, year) VALUES ('$Staff_id', '$leave_type', '$no_of_days', '$reg_year')";
			mysqli_query($conn,$sql4) or die(mysqli_error($conn));
		}
		echo 	"<script>
					alert(\"Staff added successfully.\");
				</script>";
		
		echo "<script>window.location=\"add_staff.php\";</script>";
	}
	else
	{
		echo 	"<script>
					alert(\"Staff ID already exist, Please use differen Staff ID.\");
				</script>";
		echo "<script>window.location=\"add_staff.php\";</script>";
	}
		
	
	// Closing Connection
	mysqli_close($conn);
	
?>