<?php

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

class Hotels_model extends CI_Model
{

    public $table = 'hotels';
    public $id = 'Id';
    public $order = 'ASC';

    function __construct()
    {
        parent::__construct();
    }

    function shorten_URL ($longUrl,$code) {
          $key = 'AIzaSyAA-9frcrZp8TNgSZdbs54rpsPHZBHEzqc';
          $url = 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=' . $key;
          $data = array(
             "longDynamicLink"=>"https://hotelcompendium.page.link?apn=com.hotelcompendium&ibi=com.hotelcompendium&efr=1&isi=1549368280&link=".$longUrl.$code,
             "suffix"=> array(
                "option"=>"UNGUESSABLE"
             )
          );
          // echo "https://hotelcompendium.page.link?apn=com.questcompendium&ibi=com.questcompendium&isi=1532601843&link=".$longUrl.$code; die();


          $headers = array('Content-Type: application/json');

          $ch = curl_init ();
          curl_setopt ( $ch, CURLOPT_URL, $url );
          curl_setopt ( $ch, CURLOPT_POST, true );
          curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
          curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
          curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

          $data = curl_exec ( $ch );
          curl_close ( $ch );

          $short_url = json_decode($data);
          if(isset($short_url->error)){
              return $short_url->error->message;
          } else {
              return $short_url->shortLink;
          }

    }

    // get all
    function get_all()
    {
        $this->db->order_by($this->id, $this->order);
        return $this->db->get($this->table)->result();
    }

    // get data by id
    function get_by_id($id)
    {
        $this->db->where($this->id, $id);
        return $this->db->get($this->table)->row();
    }
    
    // get total rows
    function total_rows($q = NULL) {
        $this->db->like('Id', $q);
    $this->db->or_like('fsName', $q);
    $this->db->or_like('fsImage', $q);
    $this->db->or_like('fsBannerImage', $q);
    $this->db->or_like('fsCountry', $q);
    $this->db->or_like('fsShortDesc', $q);
    $this->db->or_like('fsState', $q);
    $this->db->or_like('fsCity', $q);
    $this->db->or_like('fiLatitude', $q);
    $this->db->or_like('fiLongitude', $q);
    $this->db->or_like('fsLocality', $q);
    $this->db->or_like('fsAddress', $q);
    $this->db->or_like('ftTimeZone', $q);
    $this->db->or_like('fiMenu', $q);
    $this->db->or_like('fiMobileNo', $q);
    $this->db->or_like('fsEmail', $q);
    $this->db->or_like('fsPassword', $q);
    $this->db->or_like('fsShortLink', $q);
    $this->db->or_like('fiStatus', $q);
    $this->db->or_like('fdCreateDate', $q);
    $this->db->or_like('fdUpdateDate', $q);
    $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    // get data with limit and search
    function get_limit_data($limit, $start = 0, $q = NULL) {
        $this->db->select('hotels.*,menu.fsTitle as fsmenu');
        $this->db->join('menu','hotels.fiMenu=menu.id', 'left');
        $this->db->order_by('fsName', $this->order);
        $this->db->like('hotels.Id', $q);
    $this->db->or_like('hotels.fsName', $q);
    $this->db->or_like('hotels.fsImage', $q);
    $this->db->or_like('hotels.fsBannerImage', $q);
    $this->db->or_like('hotels.fsCountry', $q);
    $this->db->or_like('hotels.fsShortDesc', $q);
    $this->db->or_like('hotels.fsState', $q);
    $this->db->or_like('hotels.fsCity', $q);
    $this->db->or_like('hotels.fiLatitude', $q);
    $this->db->or_like('hotels.fiLongitude', $q);
    $this->db->or_like('hotels.fsLocality', $q);
    $this->db->or_like('hotels.fsAddress', $q);
    $this->db->or_like('hotels.ftTimeZone', $q);
    $this->db->or_like('menu.fsTitle', $q);
    $this->db->or_like('hotels.fiMobileNo', $q);
    $this->db->or_like('hotels.fsEmail', $q);
    $this->db->or_like('hotels.fsPassword', $q);
    $this->db->or_like('hotels.fsShortLink', $q);
    $this->db->or_like('hotels.fiStatus', $q);
    $this->db->or_like('hotels.fdCreateDate', $q);
    $this->db->or_like('hotels.fdUpdateDate', $q);
    $this->db->limit($limit, $start);
        return $this->db->get($this->table)->result();
    }

    // insert data
    function insert($data)
    {

        $this->db->insert($this->table, $data);
        $hotelId = $this->db->insert_id();

        
        

        $menuIds = explode(",",$data['fiMenu']);

        if(count($menuIds) > 0){

            $hotelMenus = $this->db->select('*')->where_in('id',$menuIds)->get('menu');
            $hotelMenus = $hotelMenus->result_array();

            if(count($hotelMenus) > 0): foreach ($hotelMenus as $singleMenu) {

                $categoryList = $this->db->select('category.*')->where('fiMenuId',$singleMenu['id'])->order_by('id','DESC')->get('category');
                $categoryList = $categoryList->result_array();

                $singleMenu['fiMenuId'] = $singleMenu['id'];
                unset($singleMenu['id']);
                $singleMenu['fiHotelId'] = $hotelId;
                $this->db->insert('hotel_menu', $singleMenu);
                $fiInsertedMenuId = $this->db->insert_id();

                if(count($categoryList) > 0): foreach ($categoryList as $singleCategory) {

                    $facilitiesList = $this->db->select('facilities.*')->where('fiCategoryId',$singleCategory['id'])->order_by('id','DESC')->get('facilities');
                    $facilitiesList = $facilitiesList->result_array();

                    $singleCategory['fiCategoryId'] = $singleCategory['id'];
                    unset($singleCategory['id']);
                    $singleCategory['fiHotelId'] = $hotelId;
                    $singleCategory['fiMenuId'] = $fiInsertedMenuId;
                    $this->db->insert('hotel_category', $singleCategory);
                    $fiInsertedCategoryId = $this->db->insert_id();

                    if(count($facilitiesList) > 0): foreach ($facilitiesList as $singleFacilities) {
                        $singleFacilities['fiFacilitatesId'] = $singleFacilities['id'];
                        unset($singleFacilities['id']);
                        $singleFacilities['fiHotelsId'] = $hotelId;
                        $singleFacilities['fiMenuId'] = $fiInsertedMenuId;
                        $singleFacilities['fiCategoryId'] = $fiInsertedCategoryId;

                        $this->db->insert('hotel_facilitates', $singleFacilities);
                        $this->db->insert_id();

                        
                    } endif;

                } endif;

            } endif; 

        } 

        //  Static Page Auto Assign 
        $staticPageList = $this->db->select('static_pages.*')->order_by('id','DESC')->get('static_pages');
        $staticPageList = $staticPageList->result_array();

        foreach ($staticPageList as $staticPage) {
            unset($staticPage['ID']);
            $staticPage['fiHotelId'] = $hotelId;
            $this->db->insert('hotel_static_pages', $staticPage);
            $this->db->insert_id();
        } 


        /* Update QR Code */
       
        $randNo = $hotelId;
        $shortLink = $this->shorten_URL(base_url().'/','?token='.$randNo);  
        $shortLink = $shortLink.'?token='.$randNo; 
       
       
        $this->db->set('fsShortLink', $shortLink);
        $this->db->where('id', $hotelId);
        $this->db->update('hotels');

        /* Update QR Code End */
      
    }

    // update data
    function update($id, $data)
    {
        $this->db->where($this->id, $id);
        $this->db->update($this->table, $data);


        $hotelId = $id;


        $menuIds = explode(",",$data['fiMenu']);

        if(count($menuIds) > 0){

            $hotelMenus = $this->db->select('*')->where_in('id',$menuIds)->get('menu');
            $hotelMenus = $hotelMenus->result_array();


            if(count($hotelMenus) > 0): foreach ($hotelMenus as $singleMenu) {

                $countMenu = $this->db->select('hotel_menu.*')->where('fiHotelId',$id)->where('fsTitle',$singleMenu['fsTitle'])->get('hotel_menu');
                $countMenu = $countMenu->result_array();

                if(count($countMenu) == 0){
                    $hotelId = $id;
                    $categoryList = $this->db->select('category.*')->where('fiMenuId',$singleMenu['id'])->order_by('id','DESC')->get('category');
                    $categoryList = $categoryList->result_array();

                    unset($singleMenu['id']);
                    $singleMenu['fiHotelId'] = $hotelId;
                    $this->db->insert('hotel_menu', $singleMenu);
                    $fiInsertedMenuId = $this->db->insert_id();

                    if(count($categoryList) > 0): foreach ($categoryList as $singleCategory) {

                        $facilitiesList = $this->db->select('facilities.*')->where('fiCategoryId',$singleCategory['id'])->order_by('id','DESC')->get('facilities');
                        $facilitiesList = $facilitiesList->result_array();

                        unset($singleCategory['id']);
                        $singleCategory['fiHotelId'] = $hotelId;
                        $singleCategory['fiMenuId'] = $fiInsertedMenuId;
                        $this->db->insert('hotel_category', $singleCategory);
                        $fiInsertedCategoryId = $this->db->insert_id();

                        if(count($facilitiesList) > 0): foreach ($facilitiesList as $singleFacilities) {
                            unset($singleFacilities['id']);
                            $singleFacilities['fiHotelsId'] = $hotelId;
                            $singleFacilities['fiMenuId'] = $fiInsertedMenuId;
                            $singleFacilities['fiCategoryId'] = $fiInsertedCategoryId;

                            $this->db->insert('hotel_facilitates', $singleFacilities);
                            $this->db->insert_id();
                        } endif;

                    } endif;    
                }
                

            } endif; 
        }

        /* Update QR Code */

        // $randNo = $hotelId;
        // $shortLink = $this->shorten_URL(base_url().'/','?token='.$randNo);  
        // $shortLink = $shortLink.'?token='.$randNo; 
       
       
        // $this->db->set('fsShortLink', $shortLink);
        // $this->db->where('id', $hotelId);
        // $this->db->update('hotels');

        /* Update QR Code End */

    }

    // delete data
    function delete($id)
    {
        $this->db->where($this->id, $id);
        $this->db->delete($this->table);

        $this->db->where('fiHotelId', $id);
        $this->db->delete('hotel_menu'); 

        $this->db->where('fiHotelId', $id);
        $this->db->delete('hotel_category'); 

        $this->db->where('fiHotelsId', $id);   
        $this->db->delete('hotel_facilitates');

        $this->db->where('fiHotelId', $id);
        $this->db->delete('hotel_static_pages'); 


    }

}

/* End of file Hotels_model.php */
/* Location: ./application/models/Hotels_model.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-16 05:33:16 */
/* http://harviacode.com */