<?php

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

class Hotels extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Hotels_model');
        $this->load->library('form_validation');
    }


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

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

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

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

        $pageData = array(
            'header'=>array('pageTitle'=>'Hotels'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotels/hotels_list',$pageData);
    }


    public function create() 
    {
        $data = array(
            'button' => 'Create',
            'action' => site_url('hotels/create_action'),
	    'Id' => set_value('Id'),
        'fsName' => set_value('fsName'),
        'fsImage' => set_value('fsImage'),
	    'fsBannerImage' => set_value('fsBannerImage'),
	    'fsCountry' => set_value('fsCountry'),
	    'fsState' => set_value('fsState'),
	    'fsCity' => set_value('fsCity'),
	    'fiLatitude' => set_value('fiLatitude'),
	    'fiLongitude' => set_value('fiLongitude'),
	    'fsLocality' => set_value('fsLocality'),
	    'fsAddress' => set_value('fsAddress'),
        'ftTimeZone' => set_value('ftTimeZone'),
	    'fsShortDesc' => set_value('fsShortDesc'),
	    'fiMenu' => explode(",",set_value('fiMenu')),
	    'fiMobileNo' => set_value('fiMobileNo'),
	    'fsEmail' => set_value('fsEmail'),
	    'fsPassword' => set_value('fsPassword'),
	    'fiStatus' => set_value('fiStatus'),
	    'fdCreateDate' => set_value('fdCreateDate'),
	    'fdUpdateDate' => set_value('fdUpdateDate'),
	);

        $query = $this->db->select('*')->from('menu')->get();
        $data['menuLists'] = $query->result_array(); 

        $pageData = array(
            'header'=>array('pageTitle'=>'Facilities create'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotels/hotels_form',$pageData);
    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
        'fsName' => $this->input->post('fsName',TRUE),
        'fsImage' => $this->input->post('fsImage',TRUE),
		'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
		'fsCountry' => $this->input->post('fsCountry',TRUE),
		'fsState' => $this->input->post('fsState',TRUE),
		'fsCity' => $this->input->post('fsCity',TRUE),
		'fiLatitude' => $this->input->post('fiLatitude',TRUE),
		'fiLongitude' => $this->input->post('fiLongitude',TRUE),
		'fsLocality' => $this->input->post('fsLocality',TRUE),
		'fsAddress' => $this->input->post('fsAddress',TRUE),
        'ftTimeZone' => $this->input->post('ftTimeZone',TRUE),
		'fsShortDesc' => $this->input->post('fsShortDesc',TRUE),
		'fiMenu' => implode(",",$this->input->post('fiMenu',TRUE)),
		'fiMobileNo' => $this->input->post('fiMobileNo',TRUE),
		'fsEmail' => $this->input->post('fsEmail',TRUE),
		'fsPassword' => md5($this->input->post('fsPassword',TRUE)),
		'fiStatus' => $this->input->post('fiStatus',TRUE),
		'fdCreateDate' => date('Y-m-d H:i:s'),
	    ); 

            // Upload Image

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

             // End

            // Upload Image

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

             // End

            $this->Hotels_model->insert($data);
            $this->session->set_flashdata('message', 'Create Record Success');
            redirect(site_url('hotels'));
        }
    }
    
    public function update($id) 
    {
        $row = $this->Hotels_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('hotels/update_action'),
		'Id' => set_value('Id', $row->Id),
        'fsName' => set_value('fsName', $row->fsName),
        'fsImage' => set_value('fsImage', $row->fsImage),
		'fsBannerImage' => set_value('fsBannerImage', $row->fsBannerImage),
		'fsCountry' => set_value('fsCountry', $row->fsCountry),
		'fsState' => set_value('fsState', $row->fsState),
		'fsCity' => set_value('fsCity', $row->fsCity),
		'fiLatitude' => set_value('fiLatitude', $row->fiLatitude),
		'fiLongitude' => set_value('fiLongitude', $row->fiLongitude),
		'fsLocality' => set_value('fsLocality', $row->fsLocality),
		'fsAddress' => set_value('fsAddress', $row->fsAddress),
        'ftTimeZone' => set_value('ftTimeZone', $row->ftTimeZone),
		'fsShortDesc' => set_value('ftTimeZone', $row->fsShortDesc),
		// 'fiMenu' => explode(",",set_value('fiMenu', $row->fiMenu)),
		'fiMobileNo' => set_value('fiMobileNo', $row->fiMobileNo),
		'fsEmail' => set_value('fsEmail', $row->fsEmail),
		'fsPassword' => set_value('fsPassword', $row->fsPassword),
        'fsShortLink' => set_value('fsShortLink', $row->fsShortLink),
		'fiStatus' => set_value('fiStatus', $row->fiStatus),
		'fdCreateDate' => set_value('fdCreateDate', $row->fdCreateDate),
		'fdUpdateDate' => set_value('fdUpdateDate', $row->fdUpdateDate),
	    );

            $query = $this->db->select('*')->from('menu')->get();
            $data['menuLists'] = $query->result_array(); 

            $pageData = array(
	            'header'=>array('pageTitle'=>'Facilities update'),
	            'body'=>$data,
	            'footer'=>array(),
	        );
	        loadView('hotels/hotels_form',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotels'));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('Id', TRUE));
        } else {
            $data = array(
        'fsName' => $this->input->post('fsName',TRUE),
        // 'fsImage' => $this->input->post('fsImage',TRUE),
		// 'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
		'fsCountry' => $this->input->post('fsCountry',TRUE),
		'fsState' => $this->input->post('fsState',TRUE),
		'fsCity' => $this->input->post('fsCity',TRUE),
		'fiLatitude' => $this->input->post('fiLatitude',TRUE),
		'fiLongitude' => $this->input->post('fiLongitude',TRUE),
		'fsLocality' => $this->input->post('fsLocality',TRUE),
		'fsAddress' => $this->input->post('fsAddress',TRUE),
        'ftTimeZone' => $this->input->post('ftTimeZone',TRUE),
		'fsShortDesc' => $this->input->post('fsShortDesc',TRUE),
		// 'fiMenu' => implode(",",$this->input->post('fiMenu',TRUE)),
		'fiMobileNo' => $this->input->post('fiMobileNo',TRUE),
		'fsEmail' => $this->input->post('fsEmail',TRUE),
		'fiStatus' => $this->input->post('fiStatus',TRUE),
		'fdUpdateDate' => date('Y-m-d H:i:s'),
	    );

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

             // End

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

             // End

            if($this->input->post('fsPassword',TRUE)!=""){
                $data['fsPassword'] = md5($this->input->post('fsPassword',TRUE));
            }

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

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

    public function _rules() 
    {
	$this->form_validation->set_rules('fsName', 'fsname', 'trim|required');
	$this->form_validation->set_rules('fsCountry', 'fscountry', 'trim|required');
	$this->form_validation->set_rules('fsState', 'fsstate', 'trim|required');
	$this->form_validation->set_rules('fsCity', 'fscity', 'trim|required');
	$this->form_validation->set_rules('fiLatitude', 'filatitude', 'trim|required');
	$this->form_validation->set_rules('fiLongitude', 'filongitude', 'trim|required');
	$this->form_validation->set_rules('fsLocality', 'fslocality', 'trim|required');
	$this->form_validation->set_rules('fsAddress', 'fsaddress', 'trim|required');
	$this->form_validation->set_rules('ftTimeZone', 'fttimezone', 'trim|required');
	// $this->form_validation->set_rules('fiMenu', 'fimenu', 'trim|required');
	$this->form_validation->set_rules('fiMobileNo', 'fimobileno', 'trim|required');
	$this->form_validation->set_rules('fsEmail', 'fsemail', 'trim|required');
	// $this->form_validation->set_rules('fsPassword', 'fspassword', 'trim|required');

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

}

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