<?php

	header('Content-Type: application/json');
	ini_set('display_startup_errors',1);
	ini_set('display_errors',1);
	error_reporting(-1);
	date_default_timezone_set('Asia/Kolkata');
	require_once('database.php');



	if(!isset($_REQUEST['firstName']) || $_REQUEST['firstName'] == '') {
		$validationErr = "Please enter First Name!";
	} else if(!isset($_REQUEST['lastName']) || $_REQUEST['lastName'] == '') {
		$validationErr = "Please enter Last Name!";
	} else if(!isset($_REQUEST['emailID']) || $_REQUEST['emailID'] == '') {
		$validationErr = "Please enter Email Address!";
	} else if(!isset($_REQUEST['mobileNumber']) || $_REQUEST['mobileNumber'] == '') {
		$validationErr = "Please enter Mobile Number!";
	} else if(!isset($_REQUEST['username']) || $_REQUEST['username'] == '') {
		$validationErr = "Please enter User Name!";
	} else if(!isset($_REQUEST['password']) || $_REQUEST['password'] == '') {
		$validationErr = "Please enter password!";
	} else if(!isset($_REQUEST['confirmPassword']) || $_REQUEST['confirmPassword'] == '') {
		$validationErr = "Please enter confirm password!";
	} else if($_REQUEST['password'] != $_REQUEST['confirmPassword']) {
		$validationErr = "Password and Confirm Password must be same!";
	} 

	if(!empty($validationErr)){

		$result = array();
		$result['status'] = '0';
		$result['message'] = $validationErr;
		print $return = json_encode($result);
		exit;

	}

	
	$firstName    = $_REQUEST['firstName'];
	$lastName     = $_REQUEST['lastName'];
	$email        = $_REQUEST['emailID'];
	$mobileNumber = $_REQUEST['mobileNumber'];
	$username     = $_REQUEST['username'];
	$password     = $_REQUEST['password'];
	$role_id      = $_REQUEST['role'];


	
	class AdminUsers
	{

		private $conn;
		public  $firstName;
		public  $lastName;
		public  $username;
		public  $email;
		public  $mobileNumber;
		public  $date_entered;
		public  $date_modified;
		public  $role_id;
		public  $id;


		function __Construct()
		{
			$dbconnect  = new Database();
			$this->conn = $dbconnect->getConnection();
		}


		function checkUserExist()
		{

			$sql = "SELECT id FROM my_admin_users WHERE email='$this->email' OR username='$this->username'";
			$stmt = $this->conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);

			if(empty($result['id']))
				return false;

			else
				return true;

		}

		function addUser()
		{

			try{

				$date = date("Y-m-d H:i:s");

				$sql = "INSERT INTO my_admin_users(firstname, lastname, email, phone, username, password, role_id, status, date_entered) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
				$stmt = $this->conn->prepare($sql);
				$stmt->execute(array($this->firstName, $this->lastName, $this->email, $this->mobileNumber, $this->username, $this->password, $this->role_id, 1, $date));
				return true;				
			}

			catch(Exception $ex){
				echo $ex->getMessage();
			   return false;
			}

		}




	}

	
	$user = new AdminUsers();
	$user->firstName    = $_REQUEST['firstName']; 
	$user->lastName     = $_REQUEST['lastName']; 
	$user->email        = $_REQUEST['emailID'];
	$user->mobileNumber = $_REQUEST['mobileNumber'];
	$user->username     = $_REQUEST['username'];
	$user->role_id      = $_REQUEST['role'];
	$user->password     = md5($_REQUEST['password']);

	
	if(!$user->checkUserExist())
	{

		if($user->addUser())
		{
			$result = array();
			$result['status'] = '1';
			$result['message'] = 'Admin User added successfully!';
			print json_encode($result);
			exit;
		}

		else
		{
			$result = array();
			$result['status'] = '0';
			$result['message'] = 'Request Failed. Please try later!';
			print json_encode($result);
			exit;
		}


	}

	else
	{
		$result = array();
		$result['status'] = '0';
		$result['message'] = 'User with the same username or email address exist.';
		print json_encode($result);
		exit;
	}




?>