<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ApiModel extends CI_Model
{
    
    function __construct()
    {
        parent::__construct();
        
        $requestAsRaw = file_get_contents("php://input");
        $requestAsRaw = json_decode($requestAsRaw,true);

    }


    function getHotelList($lat,$long,$search){

        $confitional_where = "";
        if($search!=""){
            $confitional_where .= " AND fsName LIKE '%".$search."%'";
        }

      $hotelList = "SELECT
      hotels.*, 
      (111.111 * DEGREES(ACOS(LEAST(1.0, COS(RADIANS(fiLatitude))
         * COS(RADIANS($lat))
         * COS(RADIANS(fiLongitude - $long))
         + SIN(RADIANS(fiLatitude))
         * SIN(RADIANS($lat)))))) AS fsDistance
         FROM hotels
         WHERE 1=1 ".$confitional_where."
         ORDER BY fsDistance;";

        $tableDetails = $this->db->query($hotelList);
        return $tableDetails->result_array();
    }

    function getHotelDetails($hotelId){
        $this->db->select('hotels.*');
        $this->db->where("hotels.id",$hotelId);
        $tableDetails = $this->db->get('hotels');
        return $tableDetails->result_array();
    }

    function getMenDetails($menuId){
        $this->db->select('hotel_menu.*');
        $this->db->where("hotel_menu.id",$menuId);
        $tableDetails = $this->db->get('hotel_menu');
        return $tableDetails->result_array();
    }


    function getHotelMenu($where=array()){
        $this->db->select('hotel_menu.*');
        // $this->db->join('hotel_menu','hotels.fiMenu=hotel_menu.id', 'Inner');
        //$this->db->where("hotels.id");
        if(count($where) > 0){
            $this->db->where($where);
        }

        $tableDetails = $this->db->get('hotel_menu');
        return $tableDetails->result_array();
    }

// Serching Query
    function getMenufacSerach($fiHotelsId,$search){

        $confitional_where = "";
        if($search!=""){
            $confitional_where .= " fsTitle LIKE '%".$search."%'";
        }
        
        $query = 'select * from hotel_menu where fiHotelId='.$fiHotelsId. ' and'.$confitional_where.' limit 5';
        $tableDetails = $this->db->query($query);
        $tableDetails = $tableDetails->result_array();
        //print_r($tableDetails);
        return $tableDetails;

    }


    function getCategorySerach($fiHotelsId,$search){

        $confitional_where = "";
        if($search!=""){
            $confitional_where .= " fsTitle LIKE '%".$search."%'";
        }
        
        $query = 'select hotel_category.*, (select count(*) from hotel_facilitates where fiCategoryId=hotel_category.id) as count from hotel_category where fiHotelId='.$fiHotelsId. ' and'.$confitional_where.' limit 5';
        $tableDetails = $this->db->query($query);

        $tableDetails = $tableDetails->result_array();
        //print_r($tableDetails); die;
        return $tableDetails;

    }

    function getSubfacSerach($fiHotelsId,$search){

        $confitional_where = "";
        if($search!=""){
            $confitional_where .= " fsTitle LIKE '%".$search."%'";
        }
        
        $query = 'select * from hotel_facilitates where fiHotelsId='.$fiHotelsId. ' and'.$confitional_where.' limit 5';
        $tableDetails = $this->db->query($query);
        $tableDetails = $tableDetails->result_array();
        //print_r($tableDetails);
        return $tableDetails;

    }

//  End
    function getCategory($menuId){
        $tableDetails = $this->db->query('select hotel_category.*,(select count(*) from hotel_facilitates where fiCategoryId=hotel_category.id) as count from hotel_category where fiMenuId='.$menuId);
        return $tableDetails->result_array();
    }

    function getHotelFacility($fiCategoryId,$menuId,$hotelId){
        $this->db->select('*')
        ->from('hotel_facilitates')->where('fiCategoryId',$fiCategoryId)->where('fiMenuId',$menuId)->where('fiHotelsId',$hotelId);

        $tableDetails = $this->db->get();
        return $tableDetails->result_array();
    }


    function getHotelFacilities($id){
        $this->db->select('*')
        ->from('hotel_facilitates')->where('id',$id);
        $tableDetails = $this->db->get();
        return $tableDetails->result_array();   
    }


    function getMenubarList($hotelId){
        $tableDetails = $this->db->select('*')->from('hotel_static_pages')->where('fiHotelId',$hotelId)->get();
        return $tableDetails->result_array();
    }
    
    
}