<?php

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

class Hotel_category extends CI_Controller
{
    public $userDetails;
    function __construct()
    {
        parent::__construct();
        $this->load->model('Hotel_category_model');
        $this->load->library('form_validation');
        $this->load->model('ApiModel');
        $this->userDetails = $this->session->userdata('isUserLoggedIn');
    }

    public function index($menu_id)
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'hotel_category/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'hotel_category/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'hotel_category/';
            $config['first_url'] = base_url() . 'hotel_category/';
        }

        $config['per_page'] = 100;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Hotel_category_model->total_rows($q,$menu_id,$this->userDetails['Id']);
        $Hotel_category = $this->Hotel_category_model->get_limit_data($config['per_page'], $start, $q, $menu_id,$this->userDetails['Id']);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
            'hotel_category_data' => $Hotel_category,
            'q' => $q,
            'menu_id' => $menu_id,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
        );

        $sigleMeu = $this->ApiModel->getMenDetails($menu_id);
        $HotelMenu = $sigleMeu[0];

        $pageData = array(
            'header'=>array('pageTitle'=>'Hotel Facility'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotel_category/hotel_category_list',$pageData);
    }

    public function create($menu_id) 
    {

        $data = array(
            'button' => 'Create',
            'action' => site_url('hotel_category/create_action'),
        'id' => set_value('id'),
	    'fiMenuId' => $menu_id,
        'fsTitle' => set_value('fsTitle'),
	    'fsImage' => set_value('fsImage'),
	    'fiStatus' => set_value('fiStatus'),
	    'fdCreateDate' => set_value('fdCreateDate'),
	    'fdUpdateDate' => set_value('fdUpdateDate'),
	);

        $query = $this->db->select('*')->from('hotel_menu')->where('fiHotelId',$this->userDetails['Id'])->get();
        $data['menuLists'] = $query->result_array();

        $pageData = array(
            'header'=>array('pageTitle'=>'Hotel Facility create'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotel_category/hotel_category_form',$pageData);
    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fiHotelId' => $this->userDetails['Id'],
        'fsTitle' => $this->input->post('fsTitle',TRUE),
		'fsImage' => $this->input->post('fsImage',TRUE),
		'fdCreateDate' => date('Y-m-d H:i:s'),
	    ); 

        // Upload Image

        $SampleImage = "";
        $ext = pathinfo($_FILES["fsImage"]["name"], PATHINFO_EXTENSION);
        $target_file = "assets/uploads/category_image/" . time().'-'.rand(1111,9999).'.'.$ext;
        if (move_uploaded_file($_FILES["fsImage"]["tmp_name"], $target_file)) {
        }
        $SampleImage = $target_file;
        $data['fsImage'] = $SampleImage;

         // End

        $this->Hotel_category_model->insert($data);
        $this->session->set_flashdata('message', 'Create Record Success');
        redirect(site_url('hotel_category/index/'.$this->input->post('fiMenuId',TRUE)));
        }
    }
    
    public function update($id,$isDirectCall = false) 
    {

        if($isDirectCall==true){
            $_REQUEST['menu_id'] = $id['menu_id'];
        }

        $menu_id = $_REQUEST['menu_id'];

        $row = $this->Hotel_category_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('hotel_category/update_action'),
                'id' => set_value('id', $row->id),
        		'fiMenuId' => $menu_id,
                'fsTitle' => set_value('fsTitle', $row->fsTitle),
        		'fsImage' => set_value('fsImage', $row->fsImage),
        		'fiStatus' => set_value('fiStatus', $row->fiStatus),
        		'fdCreateDate' => set_value('fdCreateDate', $row->fdCreateDate),
        		'fdUpdateDate' => set_value('fdUpdateDate', $row->fdUpdateDate),
    	    );

            $query = $this->db->select('*')->from('hotel_menu')->where('fiHotelId',$this->userDetails['Id'])->get();
            $data['menuLists'] = $query->result_array();

            $pageData = array(
            'header'=>array('pageTitle'=>'Hotel Facility update'),
            'body'=>$data,
            'footer'=>array(),
        );

        loadView('hotel_category/hotel_category_form',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_category/index/'.$menu_id));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('id', TRUE));
        } else {
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fsTitle' => $this->input->post('fsTitle',TRUE),
		// 'fsImage' => $this->input->post('fsImage',TRUE),
		'fdUpdateDate' => date('Y-m-d H:i:s'),
	    ); 

            if($_FILES["fsImage"]["name"]!=""){
                $SampleImage = "";
                $ext = pathinfo($_FILES["fsImage"]["name"], PATHINFO_EXTENSION);
                $target_file = "assets/uploads/category_image/" . time().'-'.rand(1111,9999).'.'.$ext;
                if (move_uploaded_file($_FILES["fsImage"]["tmp_name"], $target_file)) {
                }
                $SampleImage = $target_file;
                $data['fsImage'] = $SampleImage;
            }
            

            $this->Hotel_category_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
            redirect(site_url('hotel_category/index/'.$this->input->post('fiMenuId',TRUE)));
        }
    }
    
    public function delete($id) 
    {
        $row = $this->Hotel_category_model->get_by_id($id);

        if ($row) {
            $this->Hotel_category_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('hotel_category/index/'.$_REQUEST['menu_id']));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_category/index/'.$_REQUEST['menu_id']));
        }
    }

    public function _rules() 
    {
     $this->form_validation->set_rules('fiMenuId', 'fiMenuId', 'trim|required');

    $this->form_validation->set_rules('fsTitle', 'fstitle', 'trim|required');
	 // $this->form_validation->set_rules('fsImage', 'fsImage', 'trim|required');

	$this->form_validation->set_rules('id', 'id', 'trim');
	$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

}

/* End of file category.php */
/* Location: ./application/controllers/category.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-13 09:27:44 */
/* http://harviacode.com */