<?php 

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

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

// Logical section of page
$restaurantObj =  new  restaurant();
$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']) && $restaurantObj->checkEmailExist($_REQUEST['email'],$_REQUEST['id'])){
			echo "Email already exist";
			exit;
		}else if($restaurantObj->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'])){
	$restaurantDetail = $restaurantObj->fromId($_REQUEST['id']);
}
$userList = $userObj->getAllRecords("*",'',"ORDER BY id ASC");

if(isset($_POST['submit'])){
	
	$restaurantData = new stdClass();
	
	if(isset($_FILES['profile_image']) && $_FILES['profile_image']['name']!=''){
		$image=$_FILES['profile_image']['name'];
		$image=time().'_'.$image;
		$extension = strtolower(getExtension($image));
		
		if($extension!='jpg' and $extension!='gif' and $extension!='jpeg' and $extension!='png'){
			header('location:addpackage.php?$failedMsg=Invalid image extension.');
			exit;
		}
		
		move_uploaded_file($_FILES['profile_image']['tmp_name'], DIR_UPLOAD_BANNER.$image);		
		$restaurantData->profile_image	= $image;	
	}else{
		$restaurantData->profile_image	= $_POST['uploaded_file'];	
	}
	
	$restaurantData->name 		= $_POST['name'];
	$restaurantData->added_by 		= $_POST['added_by'];
	$restaurantData->status  		= $_POST['status'];
	$restaurantData->created = date('Y-m-d H:i:s');
	$restaurantData->contact_no= $_POST['contact_no'];
	$restaurantData->address 		= $_POST['address'];
	$restaurantData->address 		= $_POST['address'];
	
	// edit restaurant details 
	if(isset($_POST['id'])){

		$restaurantData->id = $_POST['id'];
		$pageD = $restaurantObj->fromId($restaurantData->id);
		
		if(isset($_FILES['profile_image']) && $_FILES['profile_image']['name']!=''){
			if($pageD->profile_image!='' && file_exists(DIR_UPLOAD_BANNER.$pageD->profile_image)){
				unlink(DIR_UPLOAD_BANNER.$pageD->profile_image);
			}
		}	
		$rs = $restaurantObj->updateRecords($restaurantData);
		if($rs){
			$successMsg ="Restaurant Details Updated successfully";			
			header('Location:restaurantlist.php?successMsg='.$successMsg);
			exit;
		}
		
	// add restaurant details 	
	}else{
	
		$rs = $restaurantObj->addRecords($restaurantData);
		if($rs){
			$successMsg ="Restaurant Details Added successfully";			
			header('Location:restaurantlist.php?successMsg='.$successMsg);
			exit;
		}
		
	}
}



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

?>
