<?php
include('dbconnection/dbconnection.php');

if(isset($_POST["btn_action"])){
if($_POST["btn_action"]==="Add"){
	$nidno=$_POST["idno"];
	$u_email=$_POST["user_email"];
	$phone=$_POST["pno"];
	$sql=$connect->prepare("SELECT * FROM staff WHERE national_id='$nidno' OR email='$u_email'");
  $sql->execute();
 $count=COUNT($sql->fetchAll());
	if($count===0){
		
		$sql=$connect->prepare("SELECT * FROM staff WHERE phone='$phone' ");
  $sql->execute();
   $count=COUNT($sql->fetchAll());
		if($count===0){
		$query = "
			INSERT INTO staff (national_id, fname, other_names, gender,edlevel,course,rank, dob, date,phone,email,status,account,password) 
			VALUES (:id, :fname, :onames,:gender,:edlevel,:course, :job, :dob, :date, :phone, :email,:status,:account_type, :password)
			";
		$statement=$connect->prepare($query);
		$statement->execute(
		array(
			":id"			=>	$nidno, 
			":fname" 		=>	$_POST["fname"],
			":onames"		=>	$_POST["lname"], 
			":gender"		=>	$_POST["gender"], 
			":edlevel"			=>	$_POST["edulevel"], 
			":course"			=>	$_POST["course"], 
			":job"			=>	$_POST["jobpos"], 
			":dob"			=>	$_POST["dob"], 
			":date"			=>	time(), 
			":phone"		=>	$phone, 
			":email"		=>	$u_email,
			":status"		=>	"1", 
			":account_type" =>	"staff", 
			":password"		=>	password_hash($_POST["idno"],PASSWORD_DEFAULT)
		));
		$result = $statement->fetchAll();
			if(isset($result))
			{
				echo 'New Staff User Added';
			}
	}else{
		
		echo "Phone Number Already Exists";
	}
	}else{
		
		echo "User Email or National ID  Already Exists";
	}
}
}

?>