<?php

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

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


    public function ajexfile(){

        $type = $_GET['type'];
        $CKEditor = $_GET['CKEditor'];
        $funcNum  = $_GET['CKEditorFuncNum'];

        //print_r($_FILES);

        // Image upload
        if($type == 'image'){
            
            $allowed_extension = array(
                "png",
                "jpg",
                "jpeg"
            );
            
            // Get image file extension
            $file_extension = pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION);
            
            if(in_array(strtolower($file_extension),$allowed_extension)){
                echo $_FILES['upload']['tmp_name'];
                if( move_uploaded_file($_FILES['upload']['tmp_name'], "assets/uploads/".$_FILES['upload']['name'])){

                    // File path
                    if(isset($_SERVER['HTTPS'])){
                        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
                    }
                    else{
                        $protocol = 'http';
                    }
                    $url = base_url()."assets/uploads/".$_FILES['upload']['name'];

                    echo '<script>window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'")</script>';
                }
                
            }
            
            exit;
        }

        // File upload
        if($type == 'file'){
            
            $allowed_extension = array(
                "doc",
                "pdf",
                "docx"
            );
            
            // Get image file extension
            $file_extension = pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION);
            
            if(in_array(strtolower($file_extension),$allowed_extension)){

                if(move_uploaded_file($_FILES['upload']['tmp_name'], "assets/uploads/".$_FILES['upload']['name'])){

                    // File path
                    if(isset($_SERVER['HTTPS'])){
                        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
                    }
                    else{
                        $protocol = 'http';
                    }

                    $url = base_url()."assets/uploads/".$_FILES['upload']['name'];

                    echo '<script>window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'")</script>';
                }
                 
            }
            
            exit;
        }
    }  


// Ajax to Category and Sub category Menge (Controller, Vivew,)
    public function ajax_category() {
        $query = $this->db->select('*')->from('category')->where('category.fiMenuId', $this->input->post('MenuId'))->get();
        $categoryLists = $query->result_array(); 
        ?>
         <option>Select Category</option> 
         <?php
            if(isset($categoryLists)){
                  foreach($categoryLists as $categorylistvalue){
                ?>
                <option value="<?php echo $categorylistvalue['id'] ?>" <?php echo $categorylistvalue['id']=='fiMenuId'?'selected':''; ?>>  <?php echo $categorylistvalue['fsTitle'] ?></option>
                <!-- <option value="<?php echo $categorylistvalue['id'] ?>"> <?php echo $categorylistvalue['fsTitle'] ?></option> -->
                <?php

                  }
            }
                                
    }  

// Ajax to Category and Sub category Menge End


    public function menus($menu_id)
    {
        // echo "string"; die()
        $categoryid = isset($_REQUEST['categoryid'])?$_REQUEST['categoryid']:'';
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'facilities/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'facilities/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'facilities/menus/'.$menu_id.'?categoryid='.$categoryid;
            $config['first_url'] = base_url() . 'facilities/menus/'.$menu_id.'?categoryid='.$categoryid;
        }


         //****************** Ajex Code Insert and Update(3) ******************


        // print_r($_REQUEST['facilitates_id']); die();

        if (isset($_REQUEST['submit'])) {

                if ($_REQUEST['id'] > 0) {
                    $data = array(
                        'id' =>  $_REQUEST['id'],
                        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
                        'fiCategoryId' => $this->input->post('fiCategoryId',TRUE),
                        'fsTitle' => $this->input->post('fsTitle',TRUE),
                        //'fsImage' => $this->input->post('fsImage',TRUE),
                        // 'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
                        'fsDescription' => $this->input->post('fsDescription',TRUE),
                        // 'fiStatus' => $this->input->post('fiStatus',TRUE),
                        // 'fdCreateDate' => $this->input->post('fdCreateDate',TRUE),
                        'fdUpdateDate' => date("d-m-Y"),
                        ); 

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

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

                             // End


                    $this->db->where('id', $_REQUEST['id']);
                    $this->db->update('facilities',$data);
                    
                }else{

                    $data = array(
                        'id' =>  $_REQUEST['id'],
                        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
                        'fiCategoryId' => $this->input->post('fiCategoryId',TRUE),
                        'fsTitle' => $this->input->post('fsTitle',TRUE),
                        'fsImage' => $this->input->post('fsImage',TRUE),
                        'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
                        'fsDescription' => $this->input->post('fsDescription',TRUE),
                        // 'fiStatus' => $this->input->post('fiStatus',TRUE),
                        'fdCreateDate' => date("d-m-Y"),
                        // 'fdUpdateDate' => $this->input->post('fdUpdateDate',TRUE),
                        ); 

                            // Upload Image

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

                             // End

                            // Upload Image

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

                             // End
             
                    $this->db->insert('facilities',$data);
                }
            }

        //****************** Not End Ajex Code Insert and Update(3) below ******************

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Facilities_model->total_rows($q,$menu_id,$categoryid);
        $facilities = $this->Facilities_model->get_limit_data($config['per_page'], $start, $q,$menu_id,$categoryid);

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

        $data = array(
            'facilities_data' => $facilities,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
            'menu_id'=>$menu_id,
            'category_id'=> $categoryid,
        );


        $pageData = array(
            'header'=>array('pageTitle'=>'Facility Informations'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('facilities/facilities_list',$pageData);
    }


    public function update($id,$isDirectCall = false) 
    {

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



        $fiMenuId = $_REQUEST['fiMenuId'];

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

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('facilities/update_action'),
                'id' => set_value('id', $row->id),
                'fiMenuId' => $fiMenuId,
                '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('menu')->get();
            $data['menuLists'] = $query->result_array();

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

        loadView('facilities/facilities_list',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('facilities/index/'.$fiMenuId));
        }
    }
    
    public function update_action() 
    {
      
         // print_r($_REQUEST); die();
       
            $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;
            }
            
         // print_r($_REQUEST); die();

            $this->category_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
            redirect(site_url('facilities/menus/'.$this->input->post('fiMenuId',TRUE).'/?categoryid='.$_REQUEST['id']));

         
    }





    //****************** Ajex Code Insert and Update(2) ******************
    public function getFacilitatesPopup()
    {
        $this->load->model('Facilities_model');

            $facilitates_id = "";
            $fiMenuId = "";
            $fiCategoryId = "";
            $fsTitle = "";
            $fsImage = "";
            $fsBannerImage = "";
            $fsDescription = "";

            if ($_REQUEST['facilitates_id'] > 0) {
                $query = $this->db->select('*')->from('facilities')->where('facilities.id', $_REQUEST['facilitates_id'])->get();
                $facilitiesList = $query->result_array();

                // print_r($facilitiesList); die();

                $facilitates_id = $facilitiesList[0]['id'];
                $fiMenuId = $facilitiesList[0]['fiMenuId'];
                $fiCategoryId = $facilitiesList[0]['fiCategoryId'];
                $fsTitle = $facilitiesList[0]['fsTitle'];
                $fsImage = $facilitiesList[0]['fsImage'];
                $fsBannerImage = $facilitiesList[0]['fsBannerImage'];
                $fsDescription = $facilitiesList[0]['fsDescription'];
            }
            
            $query = $this->db->select('*')->where('id',$fiMenuId)->get('menu');
            $menuName = $query->result_array();

    ?>
    <!-- Popup model -->

        <form class="form" id="#facilitiesForm" method="post" enctype="multipart/form-data">
           <div class="modal-header">
                <input type="hidden" name="id" value="<?php echo $_REQUEST['facilitates_id'] ?>" >
                <input type="hidden" name="fiMenuId" value="<?php echo $_REQUEST['menu_id']; ?>" />
                <input type="hidden" name="fiCategoryId" value="<?php echo $_REQUEST['category_id'] ?>" />
               <h5 class="modal-title" id="exampleModalLabel">Facility Informations</h5>
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                 <span aria-hidden="true">&times;</span>
               </button>
             </div>
             <div class="modal-body">
                <div class="panel-container show">
                    <div class="panel-content p-0">
                            <div class="panel-content">
                                <div class="form-row">
                                    <div class="col-md-12 mb-3">
                                        <label class="form-label" for="validationCustom01">Title<span class="text-danger">*</span> </label>
                                        <input type="text" class="form-control" id="validationCustom01" name="fsTitle" id="fsTitle" placeholder="Enter title like TAXI" value="<?php echo $fsTitle; ?>" required>
                                        <div class="valid-feedback">
                                            Looks good!
                                        </div>
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label" for="validationCustom01">Icon (250x250)<span class="text-danger">*</span> </label>
                                       <?php if($fsImage!=""): ?>
                                           <img style="background: #CCC; padding: 1px; max-width: auto;  margin-bottom:  10px; margin-left: 10px;  object-fit: cover; object-position: center; height: 60px;" class="alignment_speech" target="_blank" src="<?php echo base_url().$fsImage; ?>"></img> 
                                        <?php endif; ?>
                                        <input style="padding-top: 5px;" accept=".jpeg,.png,.jpg" cols="12" rows="5" type="file" class="form-control form-control-file" name="fsImage" id="fsImage" placeholder="Description" value="<?php echo $fsImage; ?>" ></input>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label" for="validationCustom01">Banner Image (1000x500)<span class="text-danger">*</span> </label>
                                       <?php if($fsBannerImage!=""): ?>
                                           <img style="background: #CCC; padding: 1px; max-width: auto;  margin-bottom:  10px; margin-left: 10px;  object-fit: cover; object-position: center; height: 60px;" class="alignment_speech" target="_blank" src="<?php echo base_url().$fsBannerImage; ?>"></img> 
                                        <?php endif; ?>
                                        <input style="padding-top: 5px;" accept=".jpeg,.png,.jpg" cols="12" rows="5" type="file" class="form-control form-control-file" name="fsBannerImage" id="fsBannerImage" placeholder="Description" value="<?php echo $fsBannerImage; ?>" ></input>
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="col-md-12">
                                        <label class="form-label" for="validationCustom01">Description<span class="text-danger">*</span> </label>
                                        <textarea name="fsDescription" id='editor' >
                                            <?php echo html_entity_decode($fsDescription) ?>    
                                        </textarea>
                                        <div class="valid-feedback">
                                            Looks good!
                                        </div>
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
             </div>
             <div class="modal-footer">
               <!-- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> -->
               <button type="submit" name="submit" class="btn btn-primary">Submit</button>
             </div>
        </form>
    
    <!-- Popup model End -->

    <?php
    //****************** Not End Ajex Code Insert and Update(2) vivew pass ajex funcation ******************
    }





    
    public function delete($id) 
    {
        $row = $this->Facilities_model->get_by_id($id);

        if ($row) {
            $this->Facilities_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('facilities/menus/'.$row->fiMenuId.'?categoryid='.$row->fiCategoryId));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('facilities/menus/'.$row->fiMenuId.'?categoryid='.$row->fiCategoryId));
        }
    }

    // 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('fsDescription', 'fsdescription', 'trim|required');

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

}

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