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

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

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

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Hotel_menu_model->total_rows($q);
        $hotel_menu = $this->Hotel_menu_model->get_limit_data($config['per_page'], $start, $q);

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

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

        $pageData = array(
            'header'=>array('pageTitle'=>'Menu'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotel_menu/menu_list',$pageData);
    }

    public function create() 
    {
        $data = array(
            'button' => 'Create',
            'action' => site_url('hotel_menu/create_action'),
	    'id' => set_value('id'),
        'fsTitle' => set_value('fsTitle'),
	    'fsImage' => set_value('fsImage'),
	    'fiStatus' => set_value('fiStatus'),
	    'fdCreateDate' => set_value('fdCreateDate'),
	    'fdUpdateDate' => set_value('fdUpdateDate'),
	);

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

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
        '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/menu_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_menu_model->insert($data);
        $this->session->set_flashdata('message', 'Create Record Success');
        redirect(site_url('hotel_menu'));
        }
    }
    
    public function update($id) 
    {
        $row = $this->Hotel_menu_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('hotel_menu/update_action'),
		'id' => set_value('id', $row->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),
	    );

            $pageData = array(
            'header'=>array('pageTitle'=>'Menu Update'),
            'body'=>$data,
            'footer'=>array(),
        );

        loadView('hotel_menu/menu_form',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_menu'));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('id', TRUE));
        } else {
            $data = array(
        '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/menu_image/" . time().'-'.rand(1111,9999).'.'.$ext;
                if (move_uploaded_file($_FILES["fsImage"]["tmp_name"], $target_file)) {
                }
                $SampleImage = $target_file;
                $data['fsImage'] = $SampleImage;
            }

            $this->Hotel_menu_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
            redirect(site_url('hotel_menu'));
        }
    }
    
    public function delete($id) 
    {
        $row = $this->Hotel_menu_model->get_by_id($id);

        if ($row) {
            $this->Hotel_menu_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('hotel_menu'));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_menu'));
        }
    }

    public function _rules() 
    {
    $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 Menu.php */
/* Location: ./application/controllers/Menu.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-13 09:27:44 */
/* http://harviacode.com */