<?php

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

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->database();
        $this->load->library('form_validation');
        // Your own constructor code
        $this->load->model('user_model');
        $this->load->model('scheduler_model');
        $this->load->model('business_model');
        $this->load->model('dashboard_model');
        $this->load->model('product_model');
        $this->load->model('service_model');
        $this->load->model('coaching_model');
        $this->load->model('staff_model');

    }

    public function index() {
         
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            
            $this->load->view('login_view');
        } else {
            
                $this->dashboard();
            
        }
    }

    
    function not_found(){
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            $this->load->view('login_view');
        } else {
            
            $data['nav'] = 'dashboard';
            
            $data['user_role'] = $this->session->userdata('role');
            
            
            $this->load->view('includes/header', $data);
            $this->load->view('not_found');
            $this->load->view('includes/footer');
        }
        
    }
    
    
    
    function switch_b(){
        if ($this->session->userdata('b_switch') && $this->session->userdata('b_switch') == 'Yes') {
            $b_list = $this->business_model->get_all_businesses();
            
            echo(json_encode($b_list)); 
            
        }elseif ($this->session->userdata('role')  == 'Super User') {
            $b_list = $this->business_model->get_all_businesses();
            
            echo(json_encode($b_list)); 
            
        }
    }
    
    function business_switch(){
        
        $this->session->unset_userdata('business_id');
        $this->session->unset_userdata('business_name');
        
        $newdata=array(
            'businessid' => $this->input->post('id_business'),
            'business' => $this->input->post('business_name'),
            'ho' => $this->input->post('business_ho')
        );
        
        $this->session->set_userdata($newdata);
        
       $this->dashboard();
    }
    
    function check_business_sales($month, $year) {
        
    }

    public function dashboard() {
        
        $data['chart']='chart';
        $data['nodatatable']='nodatatable';
        
        
        $this->load->library('user_agent');
        
        $agent=null;
        $browser='';
        $platform='';
        if ($this->agent->is_browser())
        {
                
                $browser=$this->agent->browser();
                $platform = $this->agent->platform();
        }
        elseif ($this->agent->is_mobile())
        {
                $agent = $this->agent->mobile();
        }
        else
        {
                $agent = 'Unidentified User Agent';
        }

//        echo $agent.'</br>';
//        echo ($agent || trim($platform)=='Android' || trim($platform)=='iphone') && $this->session->userdata('role') !== 'Admin';
//        echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)
//        exit();
        
//        echo trim($browser); exit();
        if ($this->session->userdata('role') == '' && $this->session->userdata('username') != 'attendance') {
            $this->load->view('login_view');
        } 
//        else if (($agent || trim($platform)=='Android' || trim($platform)=='iphone') && $this->session->userdata('role') !== 'Admin' && $this->session->userdata('role') !== 'Super User' && $this->session->userdata('role') !== 'Feedback'){
//             $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">You do not have permission to use the system on your cell phone! '.$platform.'</div>');
//             $this->load->view('login_view');
//        }
        else if (($agent || trim($platform)=='Android' || trim($platform)=='iphone') && $this->session->userdata('role') == 'Reception' || $this->session->userdata('role') == 'Program Reception' && $this->session->userdata('role') !== 'Feedback'){
             $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">You do not have permission to use the system on your cell phone! '.$platform.'</div>');
             $this->load->view('login_view');
        }     
        else if (trim($browser) =='Firefox'){
             $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">The system works best with Chrome! Your browser is '.$browser.'</div>');
             $this->load->view('login_view');             
        } else {
            //programsdashboard
            $data['nav'] = 'dashboard';
            if ($this->session->userdata('role') == 'HR') {
                redirect(base_url('staff_list'));
            } else if ($this->session->userdata('role') == 'Store Manager') {
                redirect(base_url('business_brands'));
            } else if ($this->session->userdata('role') == 'Store Operator') {
                redirect(base_url('business_brands'));
            } else if ($this->session->userdata('role') == 'Call Center Op' || $this->session->userdata('role') == 'Reception' || $this->session->userdata('role') == 'Program Reception') {
                redirect(base_url('scheduler'));
                //$this->scheduler();
            } else if ($this->session->userdata('role') == 'Board') {
                redirect(base_url('scheduler_view_only'));
                //$this->scheduler();
            } else if ($this->session->userdata('role') == 'Trainer') {
                // redirect(base_url('programsdashboard')); 
                $program_group_id =0;
                if (null !== $this->input->post('id_program_group')){
                    $program_group_id =  $this->input->post('id_program_group');
                } 
                $staff_id=0; 

                $this->db->select('id_staff');
                $this->db->where('id_users', $this->session->userdata('userid'));
                $this->db->join('staff', 'staff_cell = user_mobile');
                $query = $this->db->get('users');
                $staff=$query->row(); 
                 if($staff){
                    $staff_id = $staff->id_staff;
                 }

                if (null !== $this->input->post('staff_id')){
                    $staff_id =  $this->input->post('staff_id');
                } 
                $today = date('Y-m-d');
                if(null != $this->input->post('mDate') && $this->input->post('mDate')!=''){
                    $today = $this->input->post('mDate') ;
                } 
                $data['nav'] = 'Programs';
                $data['subnav'] = 'CoachingMarkStudentAttendance';
                $data['business'] = $this->business_model->getbusinessdetails();
                $data['program_groups'] = $this->coaching_model->get_program_groups();
                $data['program_group_attendance_report'] = $this->coaching_model->get_program_groups_attendance_report($program_group_id,$staff_id,$today);
                $data['program_group_id'] =  $program_group_id; 
                $data['program_staff_id'] =  $staff_id; 
    
                $data['staff'] = $this->staff_model->get_active_scheduler_staff($this->session->userdata('businessid') ); 
                // $data['group']=$this->coaching_model->get_group($program_group_id);
                $data['today']=$today;
                $this->load->view('includes/header', $data);
                $this->load->view('coaching/program_mark_student_attendance_view');
                $this->load->view('includes/footer');

            } else if ($this->session->userdata('role') == 'POS') {
                redirect(base_url('pos_view'));
                //$this->scheduler();
            } else if ($this->session->userdata('role') == 'Floor Staff') {
                redirect(base_url('scheduler'));
                //$this->scheduler();
            }else if ($this->session->userdata('role') == 'PeriodBooking' || $this->session->userdata('role') == 'PBAdmin') {
                redirect(base_url('ho_view'));
                //$this->scheduler();
            } else if($this->session->userdata('role') == 'Feedback'){
                $today= date('Y-m-d');
                redirect(base_url().'feedback/none/'.$today.'/service');
            } else if($this->session->userdata('role') == 'Sh-Users'){
                redirect(base_url().'sh_dashboard');
            } else if($this->session->userdata('role') == 'pds'){
                redirect(base_url().'pds');
            } else if($this->session->userdata('username') == 'attendance'){ 
                redirect(base_url().'attendance_qr');
            } else if($this->session->userdata('role') == 'Manual Attendance'){
                redirect(base_url().'attendance');
            } else if($this->session->userdata('role') == 'Attendance'){ 
                $this->db->select('qr_enabled');
                $this->db->where('id_business', $this->session->userdata('businessid'));
                $q = $this->db->get('business');
                $bus=$q->row();
                
                if($bus->qr_enabled == 'Yes'){                    
                    redirect(base_url().'attendance_qr');
                }else{
                    redirect(base_url().'attendance');
                }

            } else if($this->session->userdata('role') == 'selfservice'){
                redirect(base_url().'menu');
            } else if($this->session->userdata('role') == 'Staff Schedule'){
                
                $today= date('Y-m-d');
                $userid=0;
                $this->db->select('id_staff');
                $this->db->where('id_users', $this->session->userdata('userid'));
                $this->db->join('staff', 'staff_cell = user_mobile');
                $query = $this->db->get('users');
                $staff=$query->row();
                //print_r($userid);exit();
                redirect(base_url().'coach_schedule/'.$today.'/'.$staff->id_staff);
            } else if($this->session->userdata('role') == 'ResPOS'){
                redirect(base_url().'restaurant_pos');        
            } else {

                // business_sale data inserting
                $month = date('M', strtotime('first day of previous month'));
                $year = date('Y', strtotime('first day of previous month'));

                $result = $this->dashboard_model->get_business_sale_year_month($month, $year);

                if ($result) {
                    
                } else {
                    $start = date('Y-m-d', strtotime('first day of previous month'));
                    $end = date('Y-m-d', strtotime('last day of previous month'));
                    $result = $this->dashboard_model->get_this_month_year_sale($start, $end);
                    $data = array(
                        'business_id' => $this->session->userdata('businessid'),
                        'month' => date('M', strtotime($start)),
                        'year' => date('Y', strtotime($start)),
                        'total_sale' => empty($result->total_sale) ? 0 : $result->total_sale
                    );
                    $result = $this->dashboard_model->add_this_month_year_sale($data);
                }

                // business_sale chart drawing
                $years = $this->dashboard_model->get_business_sale_years();

                $html = "";

                foreach ($years as $y) {

                    $months = array(
                        $y->year => array(
                            'Jan' => 0, 'Feb' => 0, 'Mar' => 0, 'Apr' => 0,
                            'May' => 0, 'Jun' => 0, 'Jul' => 0, 'Aug' => 0,
                            'Sep' => 0, 'Oct' => 0, 'Nov' => 0, 'Dec' => 0
                        )
                    );

                    $sales = $this->dashboard_model->get_business_sales($y->year);

                    $html .= "{ name: '$y->year', ";
                    $html .= "data: [";

                    foreach ($sales as $sale) {
                        foreach ($months[$y->year] as $key => $value) {
                            if ($sale->month == $key) {
                                $months[$y->year][$key] = $sale->total_sale;
                            }
                        }
                    }

                    foreach ($months[$y->year] as $m) {
                        $html .= $m . ",";
                    }

                    $html .= "] ";

                    $html .= "},";
                }

                $data['sales'] = rtrim($html, ',');
                
               //get work week
               $workweek = $this->dashboard_model->get_workweek();
               
               
               $data['days'] =[];
               $x=0;
               foreach($workweek as $ww){
                   
                   $data['days'][$x] = $ww['day'];        
                   $data['Customers'][$x] = (float)$ww['Customers'];
                   $data['Services'][$x] = (float)$ww['Services'];
                   $data['Revenue'][$x] = (float)$ww['Revenue'];
                   $data['Balance'][$x] = (float)$ww['Balance'];
                   $data['Advance'][$x] = (float)$ww['Advance'];
                   $x++;
                   
                }
                
               //get work month
               $workweek = $this->dashboard_model->get_workmonth();
               
               
               $data['mdays'] =[];
               $x=0;
               foreach($workweek as $ww){
                   
                   $data['mdays'][$x] = $ww['day'];        
                   $data['mCustomers'][$x] = (float)$ww['Customers'];
                   $data['mServices'][$x] = (float)$ww['Services'];
                   $data['mRevenue'][$x] = (float)$ww['Revenue'];
                   $data['mBalance'][$x] = (float)$ww['Balance'];
                   $data['mAdvance'][$x] = (float)$ww['Advance'];
                   $x++;
                   
                }
                
                ///get other charts data directtly
                //get todays sale
                $data['TodaySale'] = $this->dashboard_model->get_todaysale();      
                $TodayRetained = $this->dashboard_model->get_todayretained(); 
                if(sizeof($data['TodaySale'] )>0){
                    $data['TodayRetained'] = $TodayRetained[0]['Total'];
                    $data['Today'] = $data['TodaySale'][0]['Total'];
                    $data['TodayAdvAdj'] = $data['TodaySale'][0]['adv_adj'];
                }else{
                    $data['TodayRetained'] = 0;
                    $data['Today'] =0;
                    $data['TodayAdvAdj'] = 0;
                }
               
                
                
                $data['Yesterday'] = $this->dashboard_model->get_yesterdaysale();
                $todaysale=0; $yesterdaysale=0;$perc=0;
                if(isset($data['TodaySale'][0])){
                    $todaysale=$data['TodaySale'][0]['Total'];
                }
                if(isset($data['Yesterday'][0])){
                    $yesterdaysale=$data['Yesterday'][0]['Total'];
                }
                $diffsale=$todaysale-$yesterdaysale;
                if ($yesterdaysale > 0) {
                   $data['perc'] =  round((100 * $diffsale) / $yesterdaysale,2);
                } else {
                   $data['perc'] = 100;
                }
                
                //get month sale data
                $data['invoice']  = $this->dashboard_model->m_invoice();
                $data['recovery']  = $this->dashboard_model->m_recovery();
                $data['retained']  = $this->dashboard_model->m_retained();
                $data['advance']  = $this->dashboard_model->m_advance();
                $data['voucher']  = $this->dashboard_model->m_voucher();
                $data['Month']  = ((sizeof($data['invoice']) > 0) ? $data['invoice'][0]['Total'] : 0) + ((sizeof($data['recovery']) > 0) ? $data['recovery'][0]['Total']:0) + ((sizeof($data['advance']) > 0) ? $data['advance'][0]['Total'] : 0)  + ((sizeof($data['voucher']) > 0) ? $data['voucher'][0]['Total']:0);
                //get year sale data
                $data['yinvoice']  = $this->dashboard_model->y_invoice();
                $data['yrecovery']  = $this->dashboard_model->y_recovery();
                $data['yretained']  = $this->dashboard_model->y_retained();
                $data['yadvance']  = $this->dashboard_model->y_advance();
                $data['yvoucher']  = $this->dashboard_model->y_voucher();
               // $data['Year']  = $this->dashboard_model->get_year();
                //$data['Year']  = $data['yinvoice'][0]['Total']+ $data['yrecovery'][0]['Total'] +$data['yadvance'][0]['Total']+$data['yvoucher'][0]['Total'];
                $data['Year']  = ((sizeof($data['yinvoice']) > 0) ? $data['yinvoice'][0]['Total']:0) + ((sizeof($data['yrecovery']) > 0 )? $data['yrecovery'][0]['Total']:0) + ((sizeof($data['yadvance']) > 0) ? $data['yadvance'][0]['Total']:0) + ((sizeof($data['yvoucher'])>0) ? $data['yvoucher'][0]['Total']:0);
                
                //get monthly sale for spark chart
                $monthlysales = $this->dashboard_model->get_monthly();
                $data['monthlysale']=[];
                foreach($monthlysales as $m){
                    array_push($data['monthlysale'], $m['Total']);                    
                }
               
                //get daily sales for spark chart
                $dailysales = $this->dashboard_model->get_daily();
                $data['dailysale']=[];
                foreach($dailysales as $d){
                    array_push($data['dailysale'], $d['Total']);                    
                }
                
                ///get monthly commissions
                
                $monthlycommissions = $this->dashboard_model->get_month_commission();
                $data['monthlycommissions']=[];
                $data['month']=[];
                foreach($monthlycommissions as $mc){
                    array_push($data['monthlycommissions'], $mc['Commission']);
                    array_push($data['month'], $mc['Month']);                    
                }
                
                //get staff details for table
                $data['top4staff']  = $this->dashboard_model->top_4_staff();
                
                
                //get customer list
                $data['top4clients']  = $this->dashboard_model->top_4_clients();
                
                //get uninvoiced
                $data['uninvoiced']  = $this->dashboard_model->uninvoiced();
                
                
                //get outofstock
               // $data['out_of_stock_count'] = $this->product_model->out_of_stock_count();
                
                //get expiring
               // $data['expiring'] = $this->product_model->expiring_products_count();
                
               // var_dump($data['monthlycommissions']);exit();
                //////
                
                $this->load->view('includes/header', $data);
                $this->load->view('dashboard');
                $this->load->view('includes/footer');
            }
        }
    }


    public function reception() {
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            redirect('logout');
        } else {

            $this->load->view('includes/header');
            $this->load->view('reception');
            $this->load->view('includes/footer');
        }
    }

    public function scheduler($defaulDate = "") {
        //exit();
        $data['calendar']='calendar';
        $data['nodatatable']='nodatatable';
//        if($this->session->userdata('role') == 'Call Center Op'){
//             $data['menu'] = 'hidden';
//        }
        
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            $this->load->view('login_view');
            
        } else {

            if ($this->session->userdata('role') == 'HR') {
                redirect(base_url('staff_list'));
            }

            $data['nav'] = 'scheduler';
            
            $data['scheduler_style'] = $this->business_model->getbusinessdetails();
            
            STICKY_VISITS==false ?  $data['STICKY_VISITS'] = 0 : $data['STICKY_VISITS']= 1;
          
            if(null != $this->input->post('stickyvisits')){ 
                if($this->input->post('stickyvisits')==0){
                    $data['STICKY_VISITS']=0;
                } else {
                    $data['STICKY_VISITS']=1;
                }
            }
            
            
            
            $limit=STAFF_LIMIT;
            
            $this->load->model('customer_model');
            $data['city_areas'] = $this->customer_model->get_city_areas();
            
            if(null!==$this->input->post('off_set')){
                $off_set=$this->input->post('off_set');
            }else {
                $off_set = 0;
            }
            
            //Check if page count is mentioned
            $pages=0;
            if(isset($data['scheduler_style'][0]['page1'])){
                if(null !== $data['scheduler_style'][0]['page1']  && $data['scheduler_style'][0]['page1'] > 0 && $off_set == 0){
                    $limit=$data['scheduler_style'][0]['page1'];
                    $pages=2;
                }            
            }            
            
            
            //Adding shifts to scheduler
            if(null!==$data['scheduler_style'][0]['shifts'] && $data['scheduler_style'][0]['shifts']==='Y'){
                $data['time'] = $this->business_model->get_shift_timing();
                $staff_count = $this->scheduler_model->staff_shift_count($data['time']->id_shifts);
                //$data['pages']= ceil($staff_count->staff_count/$limit);
                $data['pages']= $pages > 0 ? $pages : ceil($staff_count->staff_count/$limit); 
                $data['limit']= $limit; 
                $data['off_set']= $off_set; 
                $data['staff_count']=$staff_count->staff_count; 
                $data['staff_list'] = $this->scheduler_model->staff_shift_list($data['time']->id_shifts, $off_set, $limit);
            } else {
                $data['time'] = $this->business_model->get_business_timing();
                $staff_count = $this->scheduler_model->staff_count();
                $data['pages']= $pages > 0 ? $pages : ceil($staff_count->staff_count/$limit); 
                //$data['pages']= ceil($staff_count->staff_count/$limit); 
                $data['limit']= $limit; 
                $data['off_set']= $off_set;
                $data['staff_count']=$staff_count->staff_count; 
                $data['staff_list'] = $this->scheduler_model->staff_list($off_set, $limit);
            }
            
            /////
            $data['user_role'] = $this->session->userdata('role');
            
            $resourceList = "";

            foreach ($data['staff_list'] as $staff) {

                $resourceList .= '{id: ' . $staff->id_staff . ', ';

                $img_path = 'assets/images/staff/';
                if(null == $staff->staff_image){
                    $staff_image = $img_path . "nu.png";
                }else{
                    $staff_image = file_exists('assets/images/staff/' . $staff->staff_image) ? $img_path . $staff->staff_image : $img_path . "nu.png";
                }

                $resourceList .= 'image: "' . $staff_image . '",';
                //$resourceList .= 'title: "' . $staff->staff_fullname .' \n'. strtoupper(substr($staff->staff_designation,0,5)) .' \n'.$staff->floor.'",';
                $resourceList .= 'title: "' . $staff->staff_fullname.'",';
                $resourceList .= 'staff_shared: "' . $staff->staff_shared . '",';
                $resourceList .= 'days_off: "' . $staff->week_day_off . '"},';
            }


            $events = "";

            ////New Try ////
            // if($this->input->post('defaultDate')==null){
            //     $defaulDate=date('Y-m-d');
            // }else{
            //     $defaulDate=$this->input->post('defaultDate');
            // }
            // $start = $defaulDate;
            // $end = date('Y-m-d', strtotime($start . '+1 day'));
           
            // $previous = $this->session->userdata('previous');
            // $today = date('Y-m-d');

            // if ($this->session->userdata('role') !== "Admin" && $this->session->userdata('role') !== "Super User" && $this->session->userdata('role') !== "Sh-Users") {
            //     if ($previous === "N") {
            //         if ($today <= $start) {
            //             $start = $start;
            //         } else {
            //             $start = $today;
            //         }
            //     }
            // }


            // $sh=false;
            // if ($this->session->userdata('role') == "Sh-Users") {
            //     $sh=true;
            // }
            // if($this->input->post('stickyvisits')==true){
            //     $openvisits = $this->scheduler_model->getOpenVisitsByDate($start, $end, $sh, true);
            // }else{
            //     $openvisits = $this->scheduler_model->getOpenVisitsByDate($start, $end, $sh);
            // }
             

            // $event['block_events'] = $this->scheduler_model->get_staff_blocked_events($start, $end);
            // $event['visit_events'] = $openvisits;
            

            // $event['visit_last_color'] = $this->scheduler_model->getLastVisitColor($start, $end);
            // $data['events'] = rtrim(json_encode($event),',');    

            $events ='';
            $data['events'] = rtrim(json_encode($events),',');    
            
            //////

            $data['resources'] = rtrim($resourceList, ',');
          
               
           
            
            if(null!==$this->session->flashdata('defaultDate')){
                $data['defaultDate'] = $this->session->flashdata('defaultDate');
                $data['visitlist'] = $this->scheduler_model->day_visit_list($this->session->flashdata('defaultDate'));
            }else{
                $data['defaultDate'] = $this->input->post('defaultDate');
                $data['visitlist'] = $this->scheduler_model->day_visit_list($this->input->post('defaultDate'));
            }   

            $data['taxes']=$this->business_model->getbusinesstaxes('service');
            $data['servicetax']=0;
            foreach ($data['taxes'] as $dtax){
                $data['servicetax']=$data['servicetax']+ $dtax['tax_percentage'];
            }
            
            $data['taxesdata']= json_encode($data['taxes']);
            
            ///get service types
            $data['service_types']= $this->service_model->get_services_types();
            $data['service_categories']= $this->service_model->getservice_categories();
           
            $data['additional_staff'] = $this->scheduler_model->staff_list();
            
            $this->load->model('discount_model');
            $data['discount_reasons'] = $this->discount_model->getdiscountreasons();
            
            

            $this->load->view('includes/header', $data);
            $this->load->view('scheduler_view');
            $this->load->view('includes/footer');
        }
    }

    function business_timing() {

        //storemanager,hr,reception...user serial
        checkroles(0, 0, 0);

        $data['nav'] = 'my_business';
        $data['subnav'] = 'business_timing';

        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            redirect('logout');
        } else {

            $data['time'] = $this->business_model->get_business_timing();

            $this->load->view('includes/header', $data);
            $this->load->view('setting/business_timing');
            $this->load->view('includes/footer');
        }
    }

    function update_business_timing() {

        $start = htmlentities(trim($this->input->post('start', TRUE)));
        $end = htmlentities(trim($this->input->post('end', TRUE)));

        $data = array(
            'business_opening_time' => $start,
            'business_closing_time' => $end
        );

        $update = $this->business_model->update_business_timing($data);

        if ($update) {
            echo 'success';
        }
    }

    public function time_block_reason() {

        //storemanager,hr,reception...user serial
        checkroles(0, 0, 0);

        $data['nav'] = 'my_business';
        $data['subnav'] = 'time_block_reason';
        $data['timebockreason'] = $this->business_model->get_time_block_reason();
        $this->load->view('includes/header', $data);
        $this->load->view('setting/time_block_reason_view');
        $this->load->view('includes/footer');
    }

    public function add_blockevent() {

        $data = array(
            'business_id' => $this->session->userdata('businessid'),
            'block_event_name' => $this->input->post('blockeventname'),
            'block_event_duration' => $this->input->post('duration'),
            'block_event_desc' => $this->input->post('description')
        );

        $insert = $this->business_model->add_blockevents($data);

        if ($insert) {
            echo 'success';
        }
    }

    public function edit_blockevent() {

        $data = array(
            'block_event_name' => $this->input->post('blockeventname'),
            'block_event_duration' => $this->input->post('duration'),
            'block_event_desc' => $this->input->post('description')
        );

        $update = $this->business_model->edit_blockevents($data);

        if ($update) {
            echo 'success';
        }
    }

    public function get_edit_blockevent() {
        $data = $this->business_model->get_edit_blockevent();
       
        echo (json_encode($data)); 
        die;
    }

    public function scheduler_view_only($defaulDate = "") {
        
        $data['calendar']='calendar';
        $data['nodatatable']='nodatatable';
        
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
           $this->load->view('login_view');
        } else {
            $data['menu'] = 'hidden';
            if ($this->session->userdata('role') == 'Store Manager') {
                redirect(base_url('servicetypes'));
            }
            if ($this->session->userdata('role') == 'HR') {
                redirect(base_url('staff_list'));
            }

            $data['nav'] = 'scheduler';

            $data['time'] = $this->business_model->get_business_timing();

            $data['scheduler_style'] = $this->business_model->getbusinessdetails();
            $data['user_role'] = $this->session->userdata('role');

            $data['staff_list'] = $this->scheduler_model->staff_list();
            $resourceList = "";

            foreach ($data['staff_list'] as $staff) {

                $resourceList .= '{id: ' . $staff->id_staff . ', ';

                $img_path = 'assets/images/staff/';
                $staff_image = file_exists('assets/images/staff/' . $staff->staff_image) ? $img_path . $staff->staff_image : $img_path . "no-image.png";

                $resourceList .= 'title: "' . $staff->staff_fullname .' '.$staff->staff_designation. '"},';
            }


            $events = "";


            $data['resources'] = rtrim($resourceList, ',');
            $data['events'] = rtrim($events, ',');

            $data['defaultDate'] = $defaulDate;

            $data['STICKY_VISITS'] = 0;

            //echo $defaulDate; exit;
            $this->load->view('includes/header', $data);
            $this->load->view('scheduler_view_only');
            $this->load->view('includes/footer');        
        }
    }
    
   
    function period_booking(){
        if ($this->session->userdata('role') == '' || $this->session->userdata('role') == 'Sh-Users') {
            $this->load->view('login_view');
        } else {
            $data['menu'] = 'hidden';
            $data['nav'] = 'scheduler';
            $data['scheduler_style'] = $this->business_model->getbusinessdetails();
            $data['user_role'] = $this->session->userdata('role');
            $data['staff_list'] = $this->scheduler_model->staff_list();
            
            
            $this->load->view('includes/header', $data);
            $this->load->view('period_booking');
            $this->load->view('includes/footer');    
        }
    }

    
    
   function update_csrf(){
        $csrf['csrf_name'] = $this->security->get_csrf_token_name();
        $csrf['csrf_hash'] = $this->security->get_csrf_hash();
        echo(json_encode($csrf));
   }
    
}
