<?php 

// Include main file
include_once('../libs/main.php');

// Check user Login
include_once('checkLogin.php');

// Logical section of page
$userObj =  new  user();

$successMsg = '';

if(isset($_REQUEST['successMsg'])){
	$successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
	$failedMsg = $_REQUEST['failedMsg'];
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
	if($_REQUEST['email']==''){
		echo 'Please enter email address';
		exit;
	}else if(validateEmail($_REQUEST['email'])){
		
		if(isset($_REQUEST['id']) && $userObj->checkEmailExist($_REQUEST['email'],$_REQUEST['id'])){
			echo "Email already exist";
			exit;
		}else if($userObj->checkEmailExist($_REQUEST['email'])){
			echo "Email already exist";
			exit;
		}
	}else{
		echo 'Please enter valid email address';
		exit;
	}
	
	return true;
}



if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['id'])){
	$userDetail = $userObj->fromId($_REQUEST['id']);
}


if(isset($_POST['submit'])){
	
	$userData = new stdClass();
	
	
	
	$userData->name 		= $_POST['name'];
	$userData->mobile_no 		= $_POST['mobile_no'];
	$userData->email 		= $_POST['email'];
	$userData->status  		= $_POST['status'];
	$userData->created = date('Y-m-d H:i:s');
	$userData->where_did_hear= $_POST['where_did_hear'];
	$userData->address 		= $_POST['address'];
	$userData->designation 		= $_POST['designation'];
	$userData->type 	= $_POST['type'];
	$userData->address 		= $_POST['address'];
	$userData->nationality 		= $_POST['nationality'];
	$userData->emirates 		= $_POST['emirates'];
	$userData->user_last_login_date = date('Y-m-d');	
	
	// edit user details 
	if(isset($_POST['id'])){

		$userData->id = $_POST['id'];
		$pageD = $userObj->fromId($userData->id);
		
		
		$rs = $userObj->updateRecords($userData);
		if($rs){
			$successMsg ="User Details Updated successfully";			
			header('Location:userlist.php?successMsg='.$successMsg);
			exit;
		}
		
	// add user details 	
	}else{
	
		$rs = $userObj->addRecords($userData);
		if($rs){
			$successMsg ="User Details Added successfully";			
			header('Location:userlist.php?successMsg='.$successMsg);
			exit;
		}
		
	}
}



// Include Template 
include_once('../libs/routs.php')

?>
