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

if(isset($_POST["btn_action"])){
if($_POST["btn_action"]==="Add-Staff"){
	$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";
	}
}

if($_POST["btn_action"]==="Add-Student"){
	
	/*
	// ECHO $_POST["pg"];
	$sql=$connect->prepare("SELECT * FROM students WHERE adm='$adm_no'");
  $sql->execute();
 $count=COUNT($sql->fetchAll());
	if($count===0){*/
	
		if($_POST["user_action"]==="admin"){
			$adm_no=$_POST["adm"];
			$pass=$adm_no;
			$appv=1;
		}else{
			$adm_no="";
			$pass=$_POST["pass"];
			$appv=0;
		}
		
		$query = "
			INSERT INTO students (adm,fname,lname,parent,gender,dob,date,phone,email,country,county,approval,pass) 
			VALUES (:adm_no,:fname,:lname,:parent,:gender,:dob,:date,:phone,:email,:country,:county,:approval,:password)
			";
		$statement=$connect->prepare($query);
		$statement->execute(
			array(
				":adm_no"		=>	$adm_no, 
				":fname" 		=>	$_POST["fname"],
				":lname"		=>	$_POST["lname"], 
				":parent"		=>	$_POST["pg"], 
				":gender"		=>	$_POST["gender"],  
				":dob"			=>	$_POST["dob"], 
				":date"			=>	time(), 
				":phone"		=>	$_POST["pno"], 
				":email"		=>	$_POST["user_email"],
				":country" 		=>	$_POST["country"], 
				":county" 		=>	$_POST["county"], 
				":approval"		=>	$appv, 
				":password"		=>	password_hash($pass,PASSWORD_DEFAULT)
			)
		);
		$result = $statement->fetchAll();
			if(isset($result))
			{
				echo 'New Student Account Added';
			}else{
				echo "Failed to Submit";
			}
	/*
	}else{
		
		echo "Student Admision  Number Already Exists";
	}*/
}


if($_POST["btn_action"]==="Add-Subscriber"){
	$email=$_POST["user-email"];
	// ECHO $_POST["pg"];
	$sql=$connect->prepare("SELECT * FROM subscribers WHERE email='$email'");
  $sql->execute();
  $count=COUNT($sql->fetchAll());
	if($count===0){
		
		$query = "
			INSERT INTO subscribers (email,date,status) 
			VALUES (:email,:date,:status)
			";
		$statement=$connect->prepare($query);
		$statement->execute(
			array(
				":email"		=>	$email, 
				":date" 		=>	time(),
				":status"		=>	"1", 
			)
		);
		$result = $statement->fetchAll();
			if(isset($result))
			{
				echo 'Successfuly subscribed';
			}
	
	}else{
		
		echo "You are Already Subscribed";
	}
}

}

?>