<?php 

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

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

// Logical section of page
$nominationObj =  new  nomination();
$userObj = new user();
$categoryObj = new category();
$restaurantObj = new restaurant();
$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']) && $nominationObj->checkEmailExist($_REQUEST['email'],$_REQUEST['id'])){
			echo "Email already exist";
			exit;
		}else if($nominationObj->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'])){
	$nominationDetail = $nominationObj->fromId($_REQUEST['id']);
}
$userList = $userObj->getAllRecords("*",'',"ORDER BY id ASC");
$categoryList = $categoryObj->getAllRecords("*",'',"ORDER BY id ASC");
$restaurantList = $restaurantObj->getAllRecords("*",'',"ORDER BY id ASC");
if(isset($_POST['submit'])){
	
	$nominationData = new stdClass();
	
	$nominationData->restaurant_id 		= $_POST['restaurant_id'];
	$nominationData->user_id 		= $_POST['user_id'];
	$nominationData->status  		= $_POST['status'];
	$nominationData->created = date('Y-m-d H:i:s');
	$nominationData->category_id= $_POST['category_id'];
	
	// edit nomination details 
	if(isset($_POST['id'])){

		$nominationData->id = $_POST['id'];
		$rs = $nominationObj->updateRecords($nominationData);
		if($rs){
			$successMsg ="Nomination Details Updated successfully";			
			header('Location:nominationlist.php?successMsg='.$successMsg);
			exit;
		}
		
	// add nomination details 	
	}else{
	
		$rs = $nominationObj->addRecords($nominationData);
		if($rs){
			$successMsg ="Nomination Details Added successfully";			
			header('Location:nominationlist.php?successMsg='.$successMsg);
			exit;
		}
		
	}
}



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

?>
