<?php 
include("../../../include/startup.php"); 
ob_clean();
if(isset($_REQUEST['action']) && $_REQUEST['action']=='validate_username'){
	$uname = $_REQUEST['uname'];
	$id = $_REQUEST['id'];
	$where = "";
	if(isset($id) && $id !=""){
		$where = " AND id != '$id' ";
	}
	$returnArray = array();
	$sql = "SELECT * FROM admin WHERE uname = '$uname' $where ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray['status'] = "failure";
	}else{
		$returnArray['status'] = "success";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='validate_email'){
	
	$email = $_REQUEST['email'];
	$id = $_REQUEST['id'];
	$where = "";
	if(isset($id) && $id !=""){
		$where = " AND id != '$id' ";
	}
	$returnArray = array();
	$sql = "SELECT * FROM admin WHERE email = '$email' $where ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$returnArray['status'] = "failure";
	}else{
		$returnArray['status'] = "success";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='validate_user_reference'){
    $user_reference = mysqli_real_escape_string($conn, $_REQUEST['user_reference']);
    $id = $_REQUEST['id'];

    $where = "";
    if(!empty($id)){
        $where = " AND id != '$id' ";
    }

    $sql = "SELECT id FROM admin WHERE user_reference = '$user_reference' $where LIMIT 1";
    $rs = mysqli_query($conn,$sql);

    echo json_encode([
        "status" => (mysqli_num_rows($rs) > 0) ? "failure" : "success"
    ]);
}
die;
?>