<?php

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

class Visits_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('visits_model');
        $this->load->model('accounting_model');
        $this->load->model('business_model');
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }

    public function get_visits() {
        //get the posted values
        
        $data = $this->visits_model->get_visits();
        //print_r($data); exit;
        echo(json_encode($data));
        
    }
        
    
    public function getVisitbyid($qtype=0){
        $visitid=$this->input->post('id_customer_visit', TRUE);
        if($qtype==0){
            $data['services'] = $this->visits_model->getvisitservices($visitid);
            $data['staffs']=$this->visits_model->getvisitstaffs($visitid);
            $data['visits'] = $this->visits_model->getopenvisitbyid($visitid);
            $data['advances'] = $this->visits_model->getopenvisitadvancebyid($visitid);
        } else {
            $data = $this->visits_model->getvisitbyid($visitid);
        }
        echo(json_encode($data));
    }
    
    function getvisitdetails(){
        
        $visit_id = htmlspecialchars($this->input->post('visit_id', TRUE));
        $data['services'] = $this->visits_model->getServicesByVisitId($visit_id);
        echo json_encode($data);
        
    }
    
     public function getVisitbyCid(){
        $data = $this->visits_model->getvisitbycid();
        echo(json_encode($data));
    }
    
    public function getPassedVisitbyCid(){
        $data = $this->visits_model->getpassedvisitbycid();
        echo(json_encode($data));        
    }
    
    
    public function updatevisit(){
        exit();
        $result=$this->visits_model->update_visit();
        echo "success|".$result;
    }
    
    public function getlast4visits($customerid){
        
        $data=$this->visits_model->getlast4visits($customerid);
        echo(json_encode($data));
    }
 
    
    public function getstaffclient(){
        $staffid=$this->input->post('visitid', TRUE);
        $data=$this->visits_model->getstaffclient($staffid);
        echo(json_encode($data));
    }
 
    public function add_visit_advance() {
    
        $data=$this->visits_model->update_advance();
        echo(json_encode($data));
        
    }
    public function remove_advance() {
    
        $data=$this->visits_model->remove_advance();
        echo($data);
        
    }
    
    public function create_booking(){
        if(AVAILABILITY==TRUE){
            //check if staff is available
            $tableData1 = stripcslashes($this->input->post('tabledata', TRUE));               
            $tableData1 = json_decode($tableData1,TRUE);
            foreach($tableData1 as $row){
                //print_r($row);
                $staff_id = $row['staffid'];
                $id_package_type=$this->input->post('package_type_id');

                $this->db->select('*');
                $this->db->join('business_services', 'business_services.id_business_services=package_services.service_id');
                $this->db->where('package_category_id', $row['category_id']);
                $this->db->where('package_services_active', "Yes");
                $this->db->where('package_option_number', 0);
                $this->db->where('business_id', $this->session->userdata('businessid'));
                $service_ids=$this->db->get('package_services');            

                $start = date('Y-m-d\TH:i:s', strtotime($row['day'].' '.$row['hour']));
                $st = explode("T",$start);
                $start_time = $st[1];
                $start_time = substr($start_time,0,5);
                $newrunningtime = $start_time;
                $x=0;        
                foreach($service_ids->result() as $service){
                    $this_duration = explode(":",$service->service_duration);
                    $this_hour=$this_duration[0];
                    $this_minutes=$this_duration[1];

                    $this_start_time = $newrunningtime;

                    $running_time = explode(":", $newrunningtime);
                    $running_hour = $running_time[0];
                    $running_minutes= $running_time[1];

                    $h=$running_hour+$this_hour;
                    $m= intval($running_minutes) + intval($this_minutes);
                    $newrunningtime = sprintf('%02d', $h).":".sprintf('%02d',$m);

                    $visit = $this->visits_model->check_staff_availability($staff_id, $this_start_time, $newrunningtime, $st[0]);

                    if(empty($visit) == false && $visit=='failed'){
                        echo "info|Selected staff is busy in this time slot";
                        exit();
                    }

                    $workstation = $this->visits_model->check_workstation_availability($staff_id, $this_start_time, $newrunningtime, $st[0]);

                     if(empty($workstation) == false && $workstation == 'failed'){
                        echo "info|No Workstation is available in this time slot";
                        exit();
                    }

                    $x++;
                }
            }
        }
        //exit();
        
        $data=$this->visits_model->create_booking();
        
        
         //Dispatch Requests
        if(AUTODISPATCH == true){
            $this->load->model('dispatch_model');
            $visits = explode('|', $data);
            if($visits[0]=='success'){
            foreach($visits as $visitid)
                if($visitid !='success'){
                    $dispatch_requests = $this->dispatch_model->add_dispatch_request($visitid);
                }
            }
        }
        
        echo($data);
        
    }
    
    public function create_dynamic_booking(){
        $data=$this->visits_model->create_dynamic_booking();
        echo($data);
        
    }
    
    public function check_visit(){
        $visitid=$this->input->post('visit_id');
        $data=$this->visits_model->getopenvisitbyid($visitid);
        echo(json_encode($data));
    }
    
    public function open_visits() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $data['visits']=$this->visits_model->open_visit_list();


            $this->load->view('includes/header', $data);
            $this->load->view('open_visits');
            $this->load->view('includes/footer');
        }
        
    }
    public function reminder_message_update(){
        $data = $this->visits_model->reminder_message_update();
        if($data){
            echo "success|".$data;
            exit;
        }
    }
    public function add_visit_remarks(){
        $visitid=$this->input->post('visit_id');
        $visitremarks=$this->input->post('visitremarks');
        
        $data = $this->visits_model->add_visit_remarks($visitid, $visitremarks);
        if($data){
            echo "success|".$data;
            exit;
        }
        
    }
     public function add_visit_discount(){
        $visitid=$this->input->post('visit_id');
        $visitdiscount=$this->input->post('visitdiscount');
        $discountreason=$this->input->post('discount_reason');
        
        $data = $this->visits_model->add_visit_discount($visitid, $visitdiscount, $discountreason);
        if($data){
            echo "success|".$data;
            exit;
        }
        
    }
    
    public function check_package_service_count(){
        
        $package_type_id=$this->input->post("package_type_id", TRUE);
        $dates = $this->input->post("dates", TRUE);
        $bookedcount=[];
        
        $this->load->model('packages_model');
        
        $package_categories = $this->packages_model->get_package_active_categories($package_type_id);
       
        $x=0;
        foreach($package_categories as $package_category){
            
            $package_services = $this->packages_model->get_package_active_service($package_category['id_package_category']);
            foreach($package_services as $package_service){
                array_push($bookedcount, $this->visits_model->get_visit_service_count($package_service['service_id'], $dates[$x]));   
            }
            
            $x++;
        }
        
        echo(json_encode($bookedcount));        
        
    }

    public function add_extra(){

        $visit_id = $this->input->post('visit_id'); 
        $visit_extra = $this->input->post('visit_extra'); 


        $this->db->where('id_customer_visits = ', $visit_id);
        $this->db->set('visit_extra', $visit_extra);
        $this->db->update('customer_visits');

        echo('Extra info added');

    }
    
}