<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api extends CI_Controller {
	public $requestParams;
	public $auth;
	public function __construct(){
		parent::__construct();
		$this->load->model('ApiModel');
		header('Content-Type: application/json');

		//$requestAsRaw = file_get_contents("php://input");
		//$requestAsRaw = file_get_contents("php://input");
		//$requestAsRaw = json_decode($requestAsRaw,true);
        $requestAsRaw = $_REQUEST;
		$this->requestParams = $requestAsRaw;
		
		if(empty($this->requestParams)){
			$requestAsRaw = file_get_contents("php://input");
			$requestAsRaw = json_decode($requestAsRaw,true);
			$this->requestParams = $requestAsRaw;
		}

		//print_r($this->requestParams);
	}

	/* API For getHotelList */
	public function getHotelList()
	{
		$requestParams = $this->requestParams;

		if(isset($requestParams['fdLat']) && isset($requestParams['fdLong']) && isset($requestParams['fsSearch'])){
			$HotelListDetails = $this->ApiModel->getHotelList($requestParams['fdLat'],$requestParams['fdLong'],$requestParams['fsSearch']);
			$responseParams = array();
			$foMenuList = array();
			if(count($HotelListDetails) > 0){
				foreach($HotelListDetails as $HotelMenu){
					$foMenuList[] = array(
						'fiHotelsId'=>$HotelMenu['Id'],
						'fsShortDesc'=>$HotelMenu['fsShortDesc'],
						'fsHotelName'=>$HotelMenu['fsName'],
						'fsAddress'=>$HotelMenu['fsAddress'],
						'fsDistance'=>number_format($HotelMenu['fsDistance'], 2),
						'fsImage'=>base_url().$HotelMenu['fsImage'],
						'fsAppBackgroundImage'=>base_url().$HotelMenu['fsBannerImage'],
						'fdLong'=> $HotelMenu['fiLongitude'],
						'fdLat'=> $HotelMenu['fiLatitude'],
					);
				}
			}
			$responseParams['fbIsSuccess'] = true;
			$responseParams['foHotelList'] = $foMenuList;
		}else{
			$responseParams['fbIsError'] = false;
			$responseParams['fsMessage'] = "Parameter is missing.";
		}

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}

	
	/* API For Get getHotelMenu */
	public function getHotelMenu()
	{
		$requestParams = $this->requestParams;
		$responseParams = array();

		if(isset($requestParams['fiHotelsId'])){
			$fiMenus = $this->ApiModel->getHotelMenu(array('fiHotelId'=>$requestParams['fiHotelsId']));

				/* Store Or Update Device Details */
				//$requestParams['Device-Details'] = str_replace("\n", $requestParams['Device-Details']);
				//echo $requestParams['Device-Details'] = stripslashes($requestParams['Device-Details']);
				$deviceDetails = $requestParams['Device-Details'];
				// print_r($deviceDetails); die;
				$deviceId = isset($deviceDetails['deviceId'])?$deviceDetails['deviceId']:'';
				$fsToken = $requestParams['fsToken'];
				$platformId = isset($deviceDetails['platformId'])?$deviceDetails['platformId']:'1';
				
				
				$foDeviceDetails = $this->db->select('*')->from('token')->where('fiDeviceId',$deviceId)->get();
				$foDeviceDetails = $foDeviceDetails->result_array();

				$addParams = array(
					'fiDeviceId'=>$deviceId,
					'fsToken'=>$fsToken,
					'foDeviceDetails'=>json_encode($deviceDetails),
					'fiHotelId'=>$requestParams['fiHotelsId'],
					'fiStatus'=>0,
					'fsDeviceType'=>$platformId
				);
				if(count($foDeviceDetails) > 0){
					$addParams['fdUpdateDate'] = date('Y-m-d H:i:s');
					$this->db->update('token',$addParams,array('fiDeviceId'=>$deviceId));
				}else{
					$addParams['fdCreateDate'] = date('Y-m-d H:i:s');
					$this->db->insert('token',$addParams);
				}

				/* Store Or Update Device Details END*/


				
				foreach ($fiMenus as $key => $value) {
					$HotelMenu = $value;
					$foMenuList[] = array(
						'fiMenuId'=>$HotelMenu['id'],
						'fsMenuName'=>$HotelMenu['fsTitle'],
						'fsImage'=>base_url('/').$HotelMenu['fsImage']
					);
				}

				$HotelListDetails = $this->ApiModel->getHotelDetails($requestParams['fiHotelsId']);
				$responseParams = array();
				$foHotelList = array();
				if(count($HotelListDetails) > 0){
					foreach($HotelListDetails as $HotelMenu){
						$foHotelList[] = array(
							'fiHotelsId'=>$HotelMenu['Id'],
							'fsShortDesc'=>$HotelMenu['fsShortDesc'],
							'fsHotelName'=>$HotelMenu['fsName'],
							'fsAddress'=>$HotelMenu['fsAddress'],
							'fsImage'=>base_url().$HotelMenu['fsImage'],
							'fsAppBackgroundImage'=>base_url().$HotelMenu['fsBannerImage']
						);
					}
				}
				$responseParams['fbIsSuccess'] = true;
				$responseParams['foMenuList'] = $foMenuList;
				$responseParams['foHotelList'] = $foHotelList;
			
			
		}else{
			$responseParams['fbIsError'] = false;
			$responseParams['fsMessage'] = "Hotel id must be required.";
		}

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}

	/* API For Get getCategorySerachValue  Seraching */
	public function getCategorySerachValue()
	{
		
		$requestParams = $this->requestParams;

		if(isset($requestParams['fiHotelsId']) && isset($requestParams['fsSearch'])){
			$categoryDetailsSearch = $this->ApiModel->getCategorySerach($requestParams['fiHotelsId'],$requestParams['fsSearch']);
			$responseParams = array(); 
			$foCatListSearch = array();
			
			
			foreach($categoryDetailsSearch as $SubCatMenu){
				$foCatListSearch[] = array(
					'fiCatId'=>$SubCatMenu['id'],
					'fiMenuId'=>$SubCatMenu['fiMenuId'],
					'fsTitle'=>$SubCatMenu['fsTitle'],
					'fsImage'=>base_url().$SubCatMenu['fsImage'],
					'fiCount'=>$SubCatMenu['count']
				);
			}
			

			$SubFacilityDetailsSearch = $this->ApiModel->getSubfacSerach($requestParams['fiHotelsId'],$requestParams['fsSearch']);
			$responseParams = array(); 
			$foFacilityListSearch = array();
				foreach($SubFacilityDetailsSearch as $SubFacMenu){
					// print_r($SubFacMenu); die;
					$foFacilityListSearch[] = array(
						'fiFacilityId'=>$SubFacMenu['id'],
						'fsFacilityMenuId'=>$SubFacMenu['fiMenuId'],
						'fsFacilityName'=>$SubFacMenu['fsTitle'],
						'fsFacilityicon'=>base_url('/').$SubFacMenu['fsImage'],
						'fsFacilityBanner'=>base_url('/').$SubFacMenu['fsBannerImage'],
						'fsDescriptionLink'=>base_url().'api/getDescription/'.$SubFacMenu['id']
					);
				}
			
			$SubMenuDetailsSearch = $this->ApiModel->getMenufacSerach($requestParams['fiHotelsId'],$requestParams['fsSearch']);
			$responseParams = array(); 
			$foMenuListSearch = array();
				foreach($SubMenuDetailsSearch as $hotelMenu){
				 // print_r($hotelMenu); die;
					$foMenuListSearch[] = array(
						'fiMenuId'=>$hotelMenu['id'],
						'fiMenuHotel'=>$hotelMenu['fiHotelId'],
						'fsMenuName'=>$hotelMenu['fsTitle'],
						'fsMenuImage'=>base_url('/').$hotelMenu['fsImage']
					);
				}


			$responseParams['fbIsSuccess'] = true;
			$responseParams['foCatList'] = $foCatListSearch;
			$responseParams['foFacilityList'] = $foFacilityListSearch;
			$responseParams['foMenuList'] = $foMenuListSearch;
		}else{
			$responseParams['fbIsError'] = false;
			$responseParams['fsMessage'] = "Sub Facility id must be required.";
		}

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}



	/* API For Get getCategory */
	public function getCategory()
	{
		$requestParams = $this->requestParams;

		if(isset($requestParams['fiMenuId'])){

			$SubFacilityDetails = $this->ApiModel->getCategory($requestParams['fiMenuId']);

			$responseParams = array(); 
			$foSubCatList = array();
			if(count($SubFacilityDetails) > 0){
				foreach($SubFacilityDetails as $SubFacMenu){
					$foSubCatList[] = array(
						'fiCatId'=>$SubFacMenu['id'],
						'fiMenuId'=>$SubFacMenu['fiMenuId'],
						'fiCount'=>$SubFacMenu['count'],
						'fsTitle'=>$SubFacMenu['fsTitle'],
						'fsImage'=>base_url().$SubFacMenu['fsImage']
					);
				}
			}
			$responseParams['fbIsSuccess'] = true;
			$responseParams['foCatList'] = $foSubCatList;
		}else{
			$responseParams['fbIsError'] = false;
			$responseParams['fsMessage'] = "Sub Facility id must be required.";
		}

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}


	/* API For Get getHotelFacility */
	public function getHotelFacility()
	{
		$requestParams = $this->requestParams;

		if(isset($requestParams['fiCategoryId']) && $requestParams['fiMenuId'] && isset($requestParams['fiHotelId'])){
			$HotelFacilityDetails = $this->ApiModel->getHotelFacility($requestParams['fiCategoryId'],$requestParams['fiMenuId'],$requestParams['fiHotelId']);

			$responseParams = array();
			$foFaciliyList = array();
			if(count($HotelFacilityDetails) > 0){
				foreach($HotelFacilityDetails as $HotelMenu){
					$foFaciliyList[] = array(
						'fiFacilityId'=>$HotelMenu['id'],
						'fsFacilityName'=>$HotelMenu['fsTitle'],
						'fsFacilityicon'=>base_url('/').$HotelMenu['fsImage'],
						'fsFacilityBanner'=>base_url('/').$HotelMenu['fsBannerImage'],
						'fsDescriptionLink'=>base_url().'api/getDescription/'.$HotelMenu['id']
					);
				}
			}
			$responseParams['fbIsSuccess'] = true;
			$responseParams['foFaciliyList'] = $foFaciliyList;
		}else{
			$responseParams['fbIsError'] = false;
			$responseParams['fsMessage'] = "Hotel id, category id and menu id must be required.";
		}

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}


	/* API For Get getHotelMenu */
	public function getMenubarList()
	{
		$requestParams = $this->requestParams;


			$SubFacilityDetails = $this->ApiModel->getMenubarList($requestParams['fiHotelId']);

			$responseParams = array(); 
			$foMenuBarList = array();
			if(count($SubFacilityDetails) > 0){
				foreach($SubFacilityDetails as $MenuBar){
					$foMenuBarList[] = array(
						'fiPageId'=>$MenuBar['ID'],
						'fiHotelId'=>$MenuBar['fiHotelId'],
						'fiMenuTitle'=>$MenuBar['fsTitle'],
						'fiDecLink'=>base_url().'page/byId?fiPageId='.$MenuBar['ID']
						
					);
				}
			}
			$responseParams['fbIsSuccess'] = true;
			$responseParams['foMenuBarList'] = $foMenuBarList;
		

		/* Sent Response To APP */
		echo json_encode($responseParams);
		die;
	}


	public function getDescription($id)
	{
		header("Content-Type: text/html");
		
		?>
		<html>
			<thead>
				<title>Description</title>
				<meta name="viewport" content="width=device-width, initial-scale=1">
			</thead>
			<body style="font-size: 17px; font-family: Helvetica Neue,Helvetica,Arial,sans-serif; ">
				<?php
					$HotelFacilityDetails = $this->ApiModel->getHotelFacilities($id);
					if(count($HotelFacilityDetails) > 0){
						echo $HotelFacilityDetails[0]['fsDescription'];
					}else{
						echo "<h1>Sorry, This facilities not found.</h1>";
					}
				?>
			</body>
		</html>
		<?php
		die;
	}
}	

	
