<?php 

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

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

$eventModule = 1;

if(@in_array('add',$event_access[$eventModule])){
}

$successMsg = '';

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

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

// Logical section of page
$adminObj =  new  admin();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['admin_id'])){
	$adminDetail = $adminObj->fromId($_REQUEST['admin_id']);
}

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['admin_id']) && $adminObj->checkEmailExist($_REQUEST['email'],$_REQUEST['admin_id'])){
			echo "Email already exist";
			exit;
		}else if($adminObj->checkEmailExist($_REQUEST['email'])){
			echo "Email already exist";
			exit;
		}
	}else{
		echo 'Please enter valid email address';
		exit;
	}
	
	return true;
}

if(isset($_POST['submit'])){
	
	$adminData->admin_fname 		= $_POST['admin_fname'];	
	$adminData->admin_mname 		= $_POST['admin_mname'];	
	$adminData->admin_lname 		= $_POST['admin_lname'];
	$adminData->admin_email_address = $_POST['admin_email_address'];
	
	if(isset($_POST['admin_id']) &&  $_POST['admin_id']==1){
		$adminData->admin_access 	=  'Full';	
		$adminData->access_event 	= 'Full';
			
	}else{
		
		$adminData->admin_access 		=  (isset($_POST['admin_access']) && count($_POST['admin_access']) > 0) ? implode(',',$_POST['admin_access']) : '';	
		$adminData->access_event = serialize($_POST['access']);
	}
	
	
	$adminData->admin_created_date 	= date('Y-m-d',time());	
	$adminData->admin_last_login_date = date('Y-m-d',time());	
	$adminData->admin_status = $_POST['admin_status'];	
	
	// edit admin details 
	if(isset($_POST['admin_id'])){

		$adminData->admin_id = $_POST['admin_id'];
		$rs = $adminObj->updateRecords($adminData);
		if($rs){
			$successMsg ="Admin Details Updated successfully";			
			header('Location:adminlist.php?successMsg='.$successMsg);
			exit;
		}
		
	// add admin details 	
	}else{
	
		$adminData->admin_password 	= md5($_POST['admin_password']);	
		$rs = $adminObj->addRecords($adminData);
		if($rs){
			$successMsg ="Admin Details Added successfully";			
			header('Location:adminlist.php?successMsg='.$successMsg);
			exit;
		}
	}
	
}


$adminModules = $adminObj->getAllModules();

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

?>