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

class Adminmain extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
         var $menu;
         var $contactus;
        public function __construct()
        {
            parent::__construct();
            $this->load->library(array('session'));
            // $this->load->model('Section', 'section_model');
            // $this->menu = $this->section_model->buildTopMenuItem();
            // $this->breadcrumbs = [];
            // $this->load->model('ContactDetails', 'contactdetails_model');
            // $this->contactus = $this->contactdetails_model->getContactus();

           $this->load->model('Brshopmodel', 'shop_model');
            $this->load->model('BrBookingModel', 'booking_model');

            if (!$this->session->userdata('logged_in'))
            { 
                redirect('/at-cms/login');
            }
    
            $this->load->model('Brinvoicemodel', 'invoice_model');
            $this->load->helper('form');
            
        }

        public function index()
	{

        $userid = $this->session->userdata('userid');
        $secret = $this->session->userdata('secret');
        $usertype = $this->session->userdata('usertype');
        // $user_data = $this->session->userdata('USER_ID');

        if($usertype!="backenduser"){
            // $this->load->view('test1');
                redirect('/at-cms/login', 'refresh');
        }
        if(empty($userid)){
                redirect('/at-cms/login', 'refresh');
        }
        if(empty($secret)){
                redirect('/at-cms/login', 'refresh');
        }
        $this->db->where('id',$userid);
        // $this->db->where('secret',$secret);
        // if($this->db->get('mas_admin')->row_array()==false){
        if($this->db->get('bemployee')->row_array()==false){
                redirect('/at-cms/login', 'refresh');
        }


        $this->load->helper('url');
        //testing
        // $currentURL = current_url(); //for simple URL
        // $params = $_SERVER['QUERY_STRING']; //for parameters
        // $fullURL = $currentURL . '?' . $params; //full URL with parameter
        //end testing


        //for pagination    
         // or you can autoload it in config

// print base_url($this->uri->uri_string());
        // $config["base_url"] = base_url() . "adminmain/index/";
        $config["base_url"] =  base_url() ."adminmain/index";
        // $config["total_rows"] = $this->Countries->record_count();
        
        // $config["total_rows"] = $this->backenduser_model->record_count();
        $config["total_rows"] =0;
        $config["uri_segment"] = 3;
        $config["per_page"] = 10;
        $config["page"] = 0;

        // $page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;

        $this->load->helper('url');
        if(!empty($this->uri->segment(3))){
            $page = $this->uri->segment(3);  
        }
        else{
            $page = $config["page"];
        }
        // get 11
        if(!empty($this->uri->segment(4))){
            $perpage = $this->uri->segment(4);  
        }
        else{
            $perpage = $config["per_page"];
        }
        // $perpage = $this->uri->segment(4);

        $choice = $config["total_rows"] / $perpage;
        // $config["num_links"] = round($choice);
        $config["num_links"] = ceil($choice);
        
        //end for pagination

        // $this->load->view('welcome_message');
        $data = array(
                'title' => 'My Title',
                'heading' => 'My Heading',
                'message' => 'My Message'
            );        
        $data['total_rows']=$config["total_rows"];
        $data['page']=$page;
        $data['perpage']=$perpage;
        $data['num_links'] = $config["num_links"];
        // $data['record']=$this->backenduser_model->getAdmindata($perpage, $page);
        $data['record']=0;
        $data['base_url'] =$config["base_url"];
        $data['fullName'] = $this->session->userdata('fullName');            
        $data['shop']=$this->shop_model->getAllShop();
         $data['currentshop']= $this->session->userdata('currentshop');    
        // $this->load->view('/admin/index',$data);
        $data['selectedmenu']='dashboard';


             // get today booking data
        // rold id 7=boss
        // rold id 6=admin
        // rold id 5=consultant/manager  ->can change shop but only view other shop booking
        // rold id 4=consultant          ->can change shop but only view other shop booking
        // rold id 3=therapist/manager  ->cannot change shop and only view their booking
        // role id 2=therapist          ->cannot change shop and only view their booking

          if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['todaybookingsNum']=$this->booking_model->countTodayBooking($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['todaybookingsNum']=$this->booking_model->countTodayBookingById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
        }
        else{
             //for error setting user role
             $data['todaybookingsNum']=$this->booking_model->countTodayBooking($this->session->userdata('currentshop'));
        }

        // end get today booking data

        // get today not confirm booking data
        // rold id 7=boss
        // rold id 6=admin
        // rold id 5=consultant/manager  ->can change shop but only view other shop booking
        // rold id 4=consultant          ->can change shop but only view other shop booking
        // rold id 3=therapist/manager  ->cannot change shop and only view their booking
        // role id 2=therapist          ->cannot change shop and only view their booking

        if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['todayNotConfirmbookingsNum']=$this->booking_model->countTodayNotConfirmBooking($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['todayNotConfirmbookingsNum']=$this->booking_model->countTodayNotConfirmBookingById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
        }
        else{
             //for error setting user role
             $data['todayNotConfirmbookingsNum']=$this->booking_model->countTodayNotConfirmBooking($this->session->userdata('currentshop'));
        }

        // end get today not confirm booking data


         //count One Month Invoice
         if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['totalOneMonthInvoice']=$this->invoice_model->countOneMonthInvoice($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['totalOneMonthInvoice']=$this->invoice_model->countOneMonthInvoiceById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
        }
        else{
             //for error setting user role
             $data['totalOneMonthInvoice']=$this->invoice_model->countOneMonthInvoice($this->session->userdata('currentshop'));
        }
    

        //end count One Month Invoice

        //count One Month Payment
         if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['totalOneMonthPayment']=$this->invoice_model->countOneMonthPayment($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['totalOneMonthPayment']=$this->invoice_model->countOneMonthPaymentById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
        }
        else{
             //for error setting user role
             $data['totalOneMonthPayment']=$this->invoice_model->countOneMonthPayment($this->session->userdata('currentshop'));
        }
    

        //end count One Month Payment


         //count new customer ordering %
         if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['totalCustomerOrderPrecent']=$this->invoice_model->countCustomerOrderPrecent($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['totalCustomerOrderPrecent']=$this->invoice_model->countCustomerOrderPrecentById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
            //not do
        }
        else{
             //for error setting user role
             $data['totalCustomerOrderPrecent']=$this->invoice_model->countCustomerOrderPrecent($this->session->userdata('currentshop'));
        }

        //end count new customer ordering % 


         //count new customer invoice amount
         if($this->session->userdata('roleId')==7 || $this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==5 || $this->session->userdata('roleId')==4){

             $data['totalNewCustomerInvoiceAmount']=$this->invoice_model->countNewCustomerInvoiceAmount($this->session->userdata('currentshop'));
        }
        elseif($this->session->userdata('roleId')==3 || $this->session->userdata('roleId')==2){

            $data['totalNewCustomerInvoiceAmount']=$this->invoice_model->countNewCustomerInvoiceAmountById($this->session->userdata('currentshop'),$this->session->userdata('userid'));
            //not do
        }
        else{
             //for error setting user role
             $data['totalNewCustomerInvoiceAmount']=$this->invoice_model->countNewCustomerInvoiceAmount($this->session->userdata('currentshop'));
        }

        //end count new customer invoice amount




        $this->load->view('/admin/dashboard',$data);
        // $contactusdata = $this->contactdetails_model->getContactus();
        //     $this->breadcrumbs[] = array (
        //         'label' => '聯絡我們',
        //         'link' => ''
        //     );
        //     $this->twig->display('main', array(
        //         'path' => array(
        //             'vendor' => $this->config->item('vendor_path'),
        //             'script' => $this->config->item('script_path'),
        //             'css' => $this->config->item('css_path'),
        //             'image' => $this->config->item('image_path')
        //         ),
        //         'breadcrumbs' => $this->breadcrumbs,
        //         'menus' => $this->menu,
        //         'contactusdata'=>$contactusdata,
        //         'session' => 'contact',
        //         'contactus' => $this->contactus,
        //         'page' => 'index'
        //     ));
        }
        public function dispay(){
            echo "1";
            $this->load->helper('url');
        $page = $this->uri->segment(3);  // get 11
        $perpage = $this->uri->segment(4);
        echo "page";
        echo $page;
        echo "perpage";
        echo $perpage;
        }

        public function changeshop(){
             $this->load->library('session');

            $shopid=$this->input->post('shopid');
            // echo $searchingword;

            //clearing $searchingword
            $shopid=trim($shopid);
            //end clearing $searchingword

            $this->session->set_userdata('currentshop', $shopid);    
            // $this->index();   
            $inputcontroller=$this->input->post('inputcontroller');
            // echo $inputcontroller;
            $this->load->helper('url');
            // redirect('at-cms/'.$inputcontroller.'/index'); 
            redirect('at-cms/'.$inputcontroller); 
            // redirect('at-cms/bremployeeadmin'); 
        }
}