<?php

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

class Feedback_controller extends CI_Controller {

    public function __construct() {

        parent::__construct();

        // Your own constructor code

        $this->load->model('invoice_model');
        $this->load->model('feedback_model');

        if ($this->session->userdata('role') == '') {

         //   redirect('logout');
        }
    }
    
    public function index($status='none', $date=null) {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $today = date('Y-m-d');

            if(null !== $date){
               $today=$date;
               $data['date']=$today;
            }

            //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));

           $data['status']= urldecode($status); 

            $data['invoices']=$this->invoice_model->getdayinvoices($today, 'desc', $data['status'],null);
            $data['config']=$this->feedback_model->get_config();
            
            $this->load->view('includes/header',$data);
            $this->load->view('feedback/feedback_view');
            $this->load->view('includes/footer');
        }
    }
    
    public function feedback_form($invoice_id) {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
       
        //$today=null, $order='asc', $feedback=null, $invoice_id=null, $type=null, $sh=false, $status='valid'
            $data['invoices']=$this->invoice_model->getdayinvoices(null, 'desc', null, $invoice_id, null, false, 'valid');
          //  print_r($data['invoices']); exit();
            
            $data['config']=$this->feedback_model->get_config();
            
            $this->load->view('includes/header',$data);
            $this->load->view('feedback/feedback_form');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function short_feedback_form($invoice_id) {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
           $this->load->view('login_view');
        } else {
       
            $data['invoices']=$this->invoice_model->getdayinvoices(null, 'desc', null, $invoice_id);

            $data['config']=$this->feedback_model->get_config();
            
            $this->load->view('includes/header',$data);
            $this->load->view('feedback/short_feedback_form');
            $this->load->view('includes/footer');
        }
        
    }
    
    
    public function insert_feedback() {
        //$today = date('Y-m-d');
        //$tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
      
       
       $data=array(
           'customer_id'=>$this->input->post('customerid'),
           'invoice_id'=>$this->input->post('invoiceid'),
           'visit_id'=>$this->input->post('visitid'),
           'business_id'=>$this->session->userdata('businessid'),
           'created_on'=> date('Y-m-d H:i:s'),
           'qos'=>$this->input->post('txtqos'),
           'atmosphere'=>$this->input->post('txtatmosphere'),
           'experience'=>$this->input->post('txtexperience'),
           'valueformoney'=>$this->input->post('txtvfm'),
           'recommendable'=>'',
           'comment'=>$this->input->post('customer_comment'),
           'feedback_type'=>$this->input->post('feedback_type')
       );
       
    //   var_dump($data); exit();
        
       $feedbackid = $this->feedback_model->insert_feedback($data);
       
       $servicedetailid=$this->input->post('servicedetailid');
       
       $index=0;
       foreach($servicedetailid as $sid){
           $exp="expectation_".$sid;
           $info = "information_".$sid;
          
           $data=array(
               'invoice_detail_id'=>$sid,
               'feedback_id'=>$feedbackid,
               'service_id'=>$this->input->post('serviceid')[$index],
               'service_type'=>$this->input->post('servicetype')[$index],
               'service_category'=>$this->input->post('servicetype')[$index],
               'service_name'=>$this->input->post('servicename')[$index],
               'staff'=>$this->input->post('staff')[$index],
               'staff_id'=>$this->input->post('staffid')[$index],
               
               'expectation'=> (isset($this->input->post($exp)[$index])) ? $this->input->post($exp)[0] : 'Yes',
               
               'skill'=>  (isset($this->input->post('txtskill')[$index])) ? $this->input->post('txtskill')[$index] : 0,
               'knowledge'=> (isset($this->input->post('txtknowledge')[$index])) ? $this->input->post('txtknowledge')[$index] : 0,
               'hospitality'=> (isset($this->input->post('txthospitality')[$index])) ? $this->input->post('txtknowledge')[$index] : 0,
               
               'information_provided'=>(isset($this->input->post($info)[$index])) ? $this->input->post($info)[0] : 'Yes',
               'satisfaction'=> (isset($this->input->post('txtsatisfaction')[$index])) ? $this->input->post('txtsatisfaction')[$index] : 0,
               'comments'=>(isset($this->input->post('visit_comment')[$index])) ? $this->input->post('visit_comment')[$index] : ''
           );
           
           $this->feedback_model->insert_details($data);
           
           $index++;
       }
       
       $this->invoice_model->update_feedback($this->input->post('invoiceid'),'entered');
       
       redirect(base_url().'feedback/none/'.date('Y-m-d').'/service');
        
    }
    
    
    
     public function feedbackdash() {
         $data['chart']='chart';
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            
            $month=$this->input->post('selectmonth');
            $year=$this->input->post('selectyear');
            
            if($month==null||$month==0){
                $month=date('m');
            }
            if($year==null||$year==0){
                $year=date('Y');
            }
         
            $data['month']=$month;
            $data['year']=$year;
            $data['staff_stats']=$this->feedback_model->get_month_staff($month,$year);

            $data['category_stats']= $this->feedback_model->get_month_category($month,$year);

            $data['salon_stats']= $this->feedback_model->get_salon_stats($month,$year);
            $data['comments']= $this->feedback_model->get_comments($month,$year);
            
            $this->load->view('includes/header',$data);
            $this->load->view('feedback/feedback_dash');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function feedback_listing() {
         $data['chart']='chart';
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            
            $month=$this->input->post('selectmonth');
            $year=$this->input->post('selectyear');
            
            if($month==null||$month==0){
                $month=date('m');
            }
            if($year==null||$year==0){
                $year=date('Y');
            }
           
            
            $data['month']=$month;
            $data['year']=$year;
            
            $data['config']=$this->feedback_model->get_config();

            $data['feedback_listing']= $this->feedback_model->get_feedback_listing($month,$year);
            $this->load->view('includes/header',$data);
            $this->load->view('feedback/feedback_listing');
            $this->load->view('includes/footer');
        }
        
    }
    
    
}

