<?php

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

class Selfservice_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('service_model');
        $this->load->model('business_model');
        $this->load->model('selfservice_model');        
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }
    
    public function menu(){
        
        $data['chart']='nochart';
        $data['nodatatable']='nodatatable';
        $data['menu'] = 'hidden';
        
        
        $data['business'] = $this->business_model->get_business_details();
        $data['service_types'] = $this->service_model->get_services_types();
        $data['service_categories'] = json_encode($this->service_model->getservice_categories());
        
        $this->load->view('includes/header1', $data);
        $this->load->view('selfservice/menucard');
        $this->load->view('includes/footer');
        
    }
    
    public function create_booking(){
        
        $customer_id = $this->input->post('customer_id');
        $customer_cell = $this->input->post('customer_cell');
        $datetime = $this->input->post('start');;
//        
//        $this->db->select('date_format(now(), "%Y-%m-%d %H:%i:%s") as mydate');
//        $query = $this->db->get('business');
//        $dt = $query->row();
//        $datetime = $dt->mydate;
        //print_r($datetime); exit();
        
        $business_id = $this->session->userdata('businessid');  

        $services = stripcslashes($this->input->post('ListData', TRUE));
        $services = json_decode($services,TRUE);
        
        if($customer_id>0){
            //$staff = $this->selfservice_model->get_available_staff($business_id);  //get available staff
            $staff=null;
            $staff_id = null; $stafftype='To be Selected';
            if(null != $staff){
                if($staff->visit_service_end != null){                    
                    if($staff->visit_service_end > $datetime){                    
                        $datetime = $staff->visit_service_end;
                    }
                }
                $staff_id = $staff->id_staff;
                $stafftype=$staff->staff_fullname;
            } else {            
                //print_r('get to be<br>');
                $staff = $this->selfservice_model->get_to_be_selected_staff($business_id);             
                $staff_id = $staff->id_staff;
                $stafftype=$staff->staff_fullname;                
                
            }
            //echo $stafftype;exit();
            if($stafftype != null){
                //Create Visit
                ///add visit
                $visit_id=$this->selfservice_model->add_visit($customer_id, $datetime, $business_id, 'Created From Self Service Desk', 0,  'SelfService');

                //add visit services and staff
                $visit_service_id = $this->selfservice_model->add_visit_services($visit_id, $customer_id, $customer_cell, $services, $staff_id, $stafftype, $datetime, $business_id);
                
                if($stafftype != 'TO BE SELECTED'){
                    echo ('success'.'|'.'Please wait '. $stafftype.' will be servicing you today.');
                    exit();
                }else{
                    echo ('success'.'|'.'Thank you! Please wait, someone will be with you shortly.');
                    exit();
                }    


            }else{
                echo ('error'.'|'.'System not set up for self Service, Please add a To be Selected option for Staff');
            }
            exit();


        }else{
            echo ('error'.'|'.'No Customer Details available, Please start again');
        }
    }
}