<?php

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

class bookings_controller extends CI_Controller {

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

    public function duplicate_booking($numofcopies=null, $booking_id=null){

        if($booking_id==null){
            $booking_id=$this->input->post('booking_id');
        }
        if($numofcopies==null){
            $numofcopies=$this->input->post('numofcopies');
        }

        $sql = "SELECT * FROM bookings 
                JOIN booking_visits on booking_visits.booking_id=bookings.id_bookings 
                WHERE id_bookings = ".$booking_id." 
                GROUP BY visit_id";

        $booking_data = $this->db->query($sql);    
        if($booking_data->num_rows() > 0){        
            for($x=0; $x < $numofcopies; $x++){
                foreach($booking_data->result() as $booking){

                    //print_r('booking '.$booking->booking_date.' '.$x);
                    //insert new booking here
                    $data=array(
                        'booking_date'=>$booking->booking_date,
                        'customer_id'=>$booking->customer_id,
                        'business_id'=>$booking->business_id,
                        'package_id'=>$booking->package_id,
                        'discount'=>$booking->discount
                    );
                    $this->db->insert('bookings', $data);
                    $id_booking = $this->db->insert_id();
                    ////
                    $visit_sql = "SELECT * FROM customer_visits 
                    WHERE id_customer_visits = ".$booking->visit_id.";";                
                    

                    $visit_data = $this->db->query($visit_sql);
                    if($visit_data->num_rows() > 0){
                        foreach($visit_data->result() as $visit){
                           // print_r('visit');
                            //insert new visits here 
                            $data=array(
                                'business_id'=>$visit->business_id,
                                'customer_id'=>$visit->customer_id,
                                'visit_color'=>$visit->visit_color,
                                'customer_visit_date'=>$visit->customer_visit_date,
                                'visit_status'=>$visit->visit_status,
                                'reminder_sms'=>$visit->reminder_sms,
                                'reminder_call'=>$visit->reminder_call,
                                'reminder_email'=>$visit->reminder_email,
                                'reminder_stricttime'=>$visit->reminder_stricttime,
                                'visit_color_type'=>$visit->visit_color_type,
                                'advance'=>$visit->advance,
                                'advance_amount'=>$visit->advance_amount,
                                'advance_mode'=>$visit->advance_mode,
                                'advance_date'=>$visit->advance_date,
                                'advance_inst'=>$visit->advance_inst,
                                'advance_comment'=>$visit->advance_comment,
                                'cancelreason'=>$visit->cancelreason,
                                'created_by'=>$visit->created_by,
                                'canceled_by'=>$visit->canceled_by,
                                'inservice'=>$visit->inservice,
                                'inservice_time'=>$visit->inservice_time,
                                'referring_staff'=>$visit->referring_staff,
                                'invoice_seq'=>$visit->invoice_seq,
                                'discount'=>$visit->discount,
                                'discount_reason'=>$visit->discount_reason,
                                'program_enrolled_service_id'=>$visit->program_enrolled_service_id,
                                'group_id'=>$visit->group_id,
                                'group_visit_id'=>$visit->group_visit_id,
                                'created_on'=>$visit->created_on,
                                'customer_event_date'=>$visit->customer_event_date
                            );
                            $this->db->insert('customer_visits', $data);
                            $id_customer_visits=$this->db->insert_id();
                            //then insert booking_visit here
                            $data=array(
                                'booking_id'=>$id_booking,
                                'visit_id'=>$id_customer_visits,
                                'category_id'=>$booking->category_id
                            );
                            $this->db->insert('booking_visits', $data);
                            ///
                            $visit_services_sql="SELECT * 
                            FROM visit_services 
                            WHERE customer_visit_id=".$visit->id_customer_visits.";";

                            $visit_services_data=$this->db->query($visit_services_sql);
                            if($visit_services_data->num_rows() > 0){
                                foreach($visit_services_data->result() as $visit_service){
                                    //insert new visit_services here
                                    //print_r('visit_service');
                                    $data=array(
                                        'business_id'=>$visit_service->business_id,
                                        'customer_visit_id'=> $id_customer_visits,
                                        'service_id'=>$visit_service->service_id,
                                        'service_name'=>$visit_service->service_name,
                                        'visit_service_start'=>$visit_service->visit_service_start,
                                        'visit_service_end'=>$visit_service->visit_service_end,
                                        'update_date'=>$visit_service->update_date,
                                        'service_flag'=>$visit_service->service_flag,
                                        'id_service_category'=>$visit_service->id_service_category,
                                        'loyalty_service'=>$visit_service->loyalty_service,
                                        'promo'=>$visit_service->promo,
                                        'membership'=>$visit_service->membership,
                                        'visit_service_status'=>$visit_service->visit_service_status,
                                        'id_service_type'=>$visit_service->id_service_type,
                                        's_type'=>$visit_service->s_type,
                                        's_category'=>$visit_service->s_category,
                                        's_rate'=>$visit_service->s_rate,
                                        'checkin'=>$visit_service->checkin,
                                        'crosssell'=>$visit_service->crosssell
                                    );
                                    $this->db->insert('visit_services', $data);
                                    $id_visit_services = $this->db->insert_id();
                                    ///

                                    $visit_staff_sql="SELECT * FROM visit_service_staffs
                                    WHERE visit_service_id = ".$visit_service->id_visit_services.";";
                                    
                                    $visit_staff_data=$this->db->query($visit_staff_sql);
                                    if($visit_staff_data->num_rows() > 0){
                                        foreach($visit_staff_data->result() as $visit_staff){
                                            //insert visit_service_staff here
                                            //print_r('visit_staff');
                                            $data= array(
                                                'business_id'=>$visit_staff->business_id,
                                                'customer_visit_id'=>$id_customer_visits,
                                                'visit_service_id'=>$id_visit_services,
                                                'staff_id'=>$visit_staff->staff_id,
                                                'staff_name'=>$visit_staff->staff_name,
                                                'requested'=>$visit_staff->requested,
                                                'block_other'=>$visit_staff->block_other,
                                                'additional_staff'=>$visit_staff->additional_staff,
                                                'visit_service_staff_status'=>$visit_staff->visit_service_staff_status
                                            );

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

                                }
                            }

                        }
                    }
                }
            }
        }

        echo 'success';
    }


}