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

class Brcustomermodel extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('BrservicesModel', 'services_model');
        $this->load->model('BrservicesuserModel', 'servicesUser_model');

        $this->load->model('BrservicesModel', 'services_model');

        //added portable function

        $this->load->model('BrPortableFormModel', 'portableform_model');

        $this->load->model('BrPortableform1amodel', 'protableform1a_model');
             $this->load->model('BrPortableform2amodel', 'protableform2a_model');
             $this->load->model('BrPortableform2bmodel', 'protableform2b_model');
             $this->load->model('BrPortableform3amodel', 'protableform3a_model');
             $this->load->model('BrPortableform4amodel', 'protableform4a_model'); 
        //end added portable function
        date_default_timezone_set('Asia/Hong_Kong');

    }
    
    //added customermodel function
    function getCustomerAll($shopid="",$serachlevel="",$channelSelectID="",$searchmem="")
    {
        
        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }
    function getCustomerAllShopWithSecondShop($shopid="",$secondshopid="",$serachlevel="",$channelSelectID="",$searchmem="")
    { //for sarah to see all shop customer

        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        $this->db->or_where(array('concurrent_registered_tenant_no' => $secondshopid));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();


    }
    
    function getCustomerAllShop(){
        //for add booking model
        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get();
        return $query->result();
        //end for add booking model
    }

    function customercode_exists($code){
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function customermobile_exists($mobile){
        $this->db->where('mobile', $mobile);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function customermobile_existsbyid($mobile,$id){
        $this->db->where('mobile', $mobile);
        $this->db->where('id!=', $id);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function customercode_existsbyid($code,$id){
        $this->db->where('LOWER(code)', strtolower($code));
        $this->db->where('id!=', $id);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        if($query->num_rows() >= 1){
            return false;
        } else {
            return true;
        }
    }
    function getCustomerByPhone($mobile){
        $this->db->where('mobile', $mobile);
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get('brcustomer');
        $id = $query->row()->id;
        return $id;
    }
    function insertCustomer($dataori){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,

        //shop_id use to record different table
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->trans_begin();
        
         //added portable field 2020-09-08
        $id_number="";
         if(!empty($dataori['id_number'])){
            $id_number=$dataori['id_number'];
        }
        $career_name="";
         if(!empty($dataori['career_name'])){
            $career_name=$dataori['career_name'];
        }
        $age="";
         if(!empty($dataori['age'])){
            $age=$dataori['age'];
        }
        $body_height="";
         if(!empty($dataori['body_height'])){
            $body_height=$dataori['body_height'];
        }
        $body_weight="";
         if(!empty($dataori['body_weight'])){
            $body_weight=$dataori['body_weight'];
        }
        $married_status="";
         if(!empty($dataori['married_status'])){
            $married_status=$dataori['married_status'];
        }
        $child_num="";
         if(!empty($dataori['child_num'])){
            $child_num=$dataori['child_num'];
        }

        $diseaseother="";
         if(!empty($dataori['diseaseother'])){
            $diseaseother=$dataori['diseaseother'];
        }

        $sensitivity="";
         if(!empty($dataori['sensitivity'])){
            $sensitivity=$dataori['sensitivity'];
        }

        $surgery="";
         if(!empty($dataori['surgery'])){
            $surgery=$dataori['surgery'];
        }

        $medicine="";
         if(!empty($dataori['medicine'])){
            $medicine=$dataori['medicine'];
        }

        $allergy="";
         if(!empty($dataori['allergy'])){
            $allergy=$dataori['allergy'];
        }

        $otherlighttherapist="";
         if(!empty($dataori['otherlighttherapist'])){
            $otherlighttherapist=$dataori['otherlighttherapist'];
        }

        $otherpayment="";
         if(!empty($dataori['otherpayment'])){
            $otherpayment=$dataori['otherpayment'];
        }


        $disease_data="";
         if(!empty($dataori['disease'])){
            $disease_data="[".implode(',',$dataori['disease'])."]";
        }

         $lifestyle_data="";
         if(!empty($dataori['lifestyle'])){
            $lifestyle_data="[".implode(',',$dataori['lifestyle'])."]";
        }
        $habbit_data="";
         if(!empty($dataori['habbit'])){
            $habbit_data="[".implode(',',$dataori['habbit'])."]";
        }
        $lighttheraphist_data="";
         if(!empty($dataori['lighttheraphist'])){
            $lighttheraphist_data="[".implode(',',$dataori['lighttheraphist'])."]";
        }
        $botoxtherapist_data="";
         if(!empty($dataori['botoxtherapist'])){
            $botoxtherapist_data=$dataori['botoxtherapist'][0];
        }
        $transtherapist_data="";
         if(!empty($dataori['transtherapist'])){
            $transtherapist_data=$dataori['transtherapist'][0];
        }
        $feeding_data="";
         if(!empty($dataori['feeding'])){
            $feeding_data=$dataori['feeding'][0];
        }
        $beautytreat_data="";
         if(!empty($dataori['beautytreat'])){
            $beautytreat_data=$dataori['beautytreat'][0];
        }
        $paymentmethod_data="";
         if(!empty($dataori['paymentmethod'])){
            $paymentmethod_data="[".implode(',',$dataori['paymentmethod'])."]";
        }
        $referrer1="";
         if(!empty($dataori['referrer1'])){
            $referrer1=$dataori['referrer1'];
        } 
        $sensitivity_option="";
         if(!empty($dataori['sensitivity_option'])){
            $sensitivity_option=$dataori['sensitivity_option'];
        }
        $surgery_option="";
         if(!empty($dataori['surgery_option'])){
            $surgery_option=$dataori['surgery_option'];
        }
        $medicine_option="";
         if(!empty($dataori['medicine_option'])){
            $medicine_option=$dataori['medicine_option'];
        }
        $allergy_option="";
         if(!empty($dataori['allergy_option'])){
            $allergy_option=$dataori['allergy_option'];
        }      

        //end added portable field 2020-09-08

        $data = array(
            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'display_name' => $dataori['display_name'] ,
            // 'chi_name' => $dataori['chi_name'] ,
            // 'eng_firstname' => $dataori['eng_firstname'] ,
            // 'eng_lastname' => $dataori['eng_lastname'] ,
            'gender' => $dataori['gender'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'dob' => $dataori['dob'] ,
            'mobile' => $dataori['mobile'] ,
            'join_date' => $dataori['join_date'] ,
            'status' => 1 ,
            'display' => 1 ,
            
            'referred_mobile' => $dataori['referred_mobile'],
            'remark' =>$dataori['remark'] ,
            'first_registered_tenant_no' =>$dataori['first_registered_tenant_no'] ,
            'concurrent_registered_tenant_no' =>$dataori['concurrent_registered_tenant_no'] ,
            'created_by'=>$this->session->userdata('userid'),
            'level'=>$dataori['level'] ,
            'isTemp'=>$dataori['isTemp'] ,
            'channelSelectID'=>$dataori['channelSelectID'] ,
            'prefer_pic'=>$dataori['prefer_pic'] ,
            'bodyoptionother_text'=>$dataori['bodyoptionother_text'] ,
            'faceoptionother_text'=>$dataori['faceoptionother_text'] ,
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

             //added portable field 2020-0908
            'referrer1'=>$referrer1,
            'id_number'=>$id_number ,
            'career_name'=>$career_name ,
            'age'=>$age ,
            'body_height'=>$body_height ,
            'body_weight'=>$body_weight ,
            'married_status'=>$married_status ,
            'child_num'=>$child_num ,
            'disease'=>$disease_data,
            'diseaseother'=>$diseaseother ,
            'sensitivity_option'=>$sensitivity_option ,
            'sensitivity'=>$sensitivity ,
            'surgery_option'=>$surgery_option ,
            'surgery'=>$surgery ,
            'medicine_option'=>$medicine_option ,
            'medicine'=>$medicine ,
            'allergy_option'=>$allergy_option ,
            'allergy'=>$allergy ,
            'lifestyle'=>$lifestyle_data,
            'habbit'=>$habbit_data,
            'lighttherapist'=>$lighttheraphist_data,
            'otherlighttherapist'=>$otherlighttherapist ,
            'botoxtherapist'=>$botoxtherapist_data,
            'transtherapist'=>$transtherapist_data,
            'feeding'=>$feeding_data,
            'beautytreat'=>$beautytreat_data,
            'paymentmethod'=>$paymentmethod_data,
            'otherpayment'=>$otherpayment ,
            
            //end added portable field 2020-0908
            

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         
        
         // $query=$this->db->insert('mas_rbac_users', $data);
         $query=$this->db->insert('brcustomer', $data);


         $id = $this->db->insert_id();

         //for portable form
          $this->portableform_model->setCurrentFormId($id);
         //end for portable form

         //add living area
          $data = array(
            'customer_id' => $id ,
            'district_id' => $dataori['district_id'] ,
            'area_id' => $dataori['area_id'] ,
            'zone' => $dataori['zone'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'address' => $dataori['address'] ,
            'status' => 1 ,
            'display' => 1 ,
            // 'mobile' => $dataori['mobile'] ,
            // 'join_date' => $dataori['join_date'] ,
            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('brcustomer_address', $data);

         //end add living area

         $skin_improve_data="";
         if(!empty($dataori['skin_improve'])){
            $skin_improve_data="[".implode(',',$dataori['skin_improve'])."]";
         }
         //add skin improve
         $data = array(
            'customer_id' => $id ,
            'skin_improve_ids' =>  $skin_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('brcustomer_skin_improve', $data);
         //end add skin improve

         //add body improve
        $body_improve_data="";
         if(!empty($dataori['body_improve'])){
            $body_improve_data="[".implode(',',$dataori['body_improve'])."]";
         }

         $data = array(
            'customer_id' => $id ,
            'body_improve_ids' =>  $body_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('brcustomer_body_improve', $data);
         //end body improve

         //add face improve
        $face_improve_data="";
         if(!empty($dataori['face_improve'])){
            $face_improve_data="[".implode(',',$dataori['face_improve'])."]";
         }

         $data = array(
            'customer_id' => $id ,
            'face_improve_ids' =>  $face_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('brcustomer_face_improve', $data);
         //end face improve

         //add promotion code
         if(!empty($dataori['promotion_code'])){
              $data = array(
            'customer_id' => $id ,
            'promotion_code' =>  $dataori['promotion_code'] ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,          
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         $query=$this->db->insert('brcustomer_promotion_code', $data);

         }//if(!empty($dataori['promotion_code'])){
         //end add promotion code


         // if($dataori['need_purchase_service']==true){


         // //add customer service
         // //find category_id
         // $category_id="";
         // $category_id=$this->services_model->getCategoryid($dataori['service_id']);
         // //end find category_id


         // $data = array(
         //    'category_id' => $category_id ,
         //    'service_id' => $dataori['service_id'] ,
         //    'user_id' => $id ,
         //    'purchased_date' => $dataori['purchased_date'] ,
         //    'purchased_qty' => $dataori['purchased_qty'] ,
         //    'available_qty' => $dataori['available_qty'] ,
         //    // 'fullName' => $dataori['inputadminname'] ,
         //    'status' => 1 ,
         //    'remark' => $dataori['remark2'] ,
         //    'display' => 1 ,
         //    // 'updated_by' => 1 , //session user id
         //    // 'created_by' => 1 , //session user id

         // );
         // date_default_timezone_set('UTC');
         // // $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         // $query=$this->db->insert('mas_service_user_'.strtolower($shopsrow->shortcode), $data);
         // //end add customer service
         // }//if($dataori['need_purchase_service']==1){


         // return $query;
        if ($this->db->trans_status() === FALSE)
        {
                $this->db->trans_rollback();
                // echo "roll back";
                return false;
        }
        else
        {
             // $this->db->trans_rollback();
                $this->db->trans_commit();
                // echo "commit transaction";
                return true;
        }
    }
     function updateCustomer($dataori){

        //different data field of updateCustomer 2020-09-23
        //********


        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,

        //shop_id use to record different table
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));

        
         //added portable field 2020-09-08
        
        $comefrompage="";
         if(!empty($dataori['comefrompage'])){
            $comefrompage=$dataori['comefrompage'];
        }
        $id_number="";
         if(!empty($dataori['id_number'])){
            $id_number=$dataori['id_number'];
        }
        $career_name="";
         if(!empty($dataori['career_name'])){
            $career_name=$dataori['career_name'];
        }
        $age="";
         if(!empty($dataori['age'])){
            $age=$dataori['age'];
        }
        $body_height="";
         if(!empty($dataori['body_height'])){
            $body_height=$dataori['body_height'];
        }
        $body_weight="";
         if(!empty($dataori['body_weight'])){
            $body_weight=$dataori['body_weight'];
        }
        $married_status="";
         if(!empty($dataori['married_status'])){
            $married_status=$dataori['married_status'];
        }
        $child_num="";
         if(!empty($dataori['child_num'])){
            $child_num=$dataori['child_num'];
        }

        $diseaseother="";
         if(!empty($dataori['diseaseother'])){
            $diseaseother=$dataori['diseaseother'];
        }

        $sensitivity="";
         if(!empty($dataori['sensitivity'])){
            $sensitivity=$dataori['sensitivity'];
        }

        $surgery="";
         if(!empty($dataori['surgery'])){
            $surgery=$dataori['surgery'];
        }

        $medicine="";
         if(!empty($dataori['medicine'])){
            $medicine=$dataori['medicine'];
        }

        $allergy="";
         if(!empty($dataori['allergy'])){
            $allergy=$dataori['allergy'];
        }

        $otherlighttherapist="";
         if(!empty($dataori['otherlighttherapist'])){
            $otherlighttherapist=$dataori['otherlighttherapist'];
        }

        $otherpayment="";
         if(!empty($dataori['otherpayment'])){
            $otherpayment=$dataori['otherpayment'];
        }

        $disease_data="";
         if(!empty($dataori['disease'])){
            $disease_data="[".implode(',',$dataori['disease'])."]";
        }

         $lifestyle_data="";
         if(!empty($dataori['lifestyle'])){
            $lifestyle_data="[".implode(',',$dataori['lifestyle'])."]";
        }
        $habbit_data="";
         if(!empty($dataori['habbit'])){
            $habbit_data="[".implode(',',$dataori['habbit'])."]";
        }
        $lighttheraphist_data="";
         if(!empty($dataori['lighttheraphist'])){
            $lighttheraphist_data="[".implode(',',$dataori['lighttheraphist'])."]";
        }
        $botoxtherapist_data="";
         if(!empty($dataori['botoxtherapist'])){
            $botoxtherapist_data=$dataori['botoxtherapist'][0];
        }
        $transtherapist_data="";
         if(!empty($dataori['transtherapist'])){
            $transtherapist_data=$dataori['transtherapist'][0];
        }
        $feeding_data="";
         if(!empty($dataori['feeding'])){
            $feeding_data=$dataori['feeding'][0];
        }
        $beautytreat_data="";
         if(!empty($dataori['beautytreat'])){
            $beautytreat_data=$dataori['beautytreat'][0];
        }
        $paymentmethod_data="";
         if(!empty($dataori['paymentmethod'])){
            $paymentmethod_data="[".implode(',',$dataori['paymentmethod'])."]";
        }
        $referrer1="";
         if(!empty($dataori['referrer1'])){
            $referrer1=$dataori['referrer1'];
        } 
        $sensitivity_option="";
         if(!empty($dataori['sensitivity_option'])){
            $sensitivity_option=$dataori['sensitivity_option'];
        }
        $surgery_option="";
         if(!empty($dataori['surgery_option'])){
            $surgery_option=$dataori['surgery_option'];
        }
        $medicine_option="";
         if(!empty($dataori['medicine_option'])){
            $medicine_option=$dataori['medicine_option'];
        }
        $allergy_option="";
         if(!empty($dataori['allergy_option'])){
            $allergy_option=$dataori['allergy_option'];
        }      

        //end added portable field 2020-09-08

        if(empty($comefrompage)){
            //for adminmain customer update
     
        $data = array(
            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'display_name' => $dataori['display_name'] ,
            // 'chi_name' => $dataori['chi_name'] ,
            // 'eng_firstname' => $dataori['eng_firstname'] ,
            // 'eng_lastname' => $dataori['eng_lastname'] ,
            'gender' => $dataori['gender'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'dob' => $dataori['dob'] ,
            'mobile' => $dataori['mobile'] ,
            'join_date' => $dataori['join_date'] ,
            'status' => 1 ,
            'display' => 1 ,
            'referred_mobile' => $dataori['referred_mobile'],
            'remark' =>$dataori['remark'] ,
            'first_registered_tenant_no'=>$dataori['first_registered_tenant_no'],
                'concurrent_registered_tenant_no'=>$dataori['concurrent_registered_tenant_no'],
                'level'=>$dataori['level'],
                'isTemp'=>$dataori['isTemp'],
                'channelSelectID'=>$dataori['channelSelectID'],
             'updated_by'=>$this->session->userdata('userid'),   
             'bodyoptionother_text'=>$dataori['bodyoptionother_text'],   
             'faceoptionother_text'=>$dataori['faceoptionother_text'],   
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

             //added portable field 2020-0908
            // 'id_number'=>$id_number ,
            // 'career_name'=>$career_name ,
            // 'age'=>$age ,
            // 'body_height'=>$body_height ,
            // 'body_weight'=>$body_weight ,
            // 'married_status'=>$married_status ,
            // 'child_num'=>$child_num ,
            // 'disease'=>$disease_data,
            // 'diseaseother'=>$diseaseother ,
            // 'sensitivity'=>$sensitivity ,
            // 'surgery'=>$surgery ,
            // 'medicine'=>$medicine ,
            // 'allergy'=>$allergy ,
            // 'lifestyle'=>$lifestyle_data,
            // 'habbit'=>$habbit_data,
            // 'lighttherapist'=>$lighttheraphist_data,
            // 'otherlighttherapist'=>$otherlighttherapist ,
            // 'botoxtherapist'=>$botoxtherapist_data,
            // 'transtherapist'=>$transtherapist_data,
            // 'feeding'=>$feeding_data,
            // 'beautytreat'=>$beautytreat_data,
            // 'paymentmethod'=>$paymentmethod_data,
            // 'otherpayment'=>$otherpayment ,
            
            //end added portable field 2020-0908

         );
         //for adminmain customer update
        }//if(empty($comefrompage)){
        if($comefrompage=='portable'){
            //for portable customer update which have more fields
            $data = array(

            'code' => $dataori['code'] ,
            'customer_group_id' => 1 ,
            'display_name' => $dataori['display_name'] ,
            // 'chi_name' => $dataori['chi_name'] ,
            // 'eng_firstname' => $dataori['eng_firstname'] ,
            // 'eng_lastname' => $dataori['eng_lastname'] ,
            'gender' => $dataori['gender'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'dob' => $dataori['dob'] ,
            'mobile' => $dataori['mobile'] ,
            'join_date' => $dataori['join_date'] ,
            'status' => 1 ,
            'display' => 1 ,
            'referred_mobile' => $dataori['referred_mobile'],
            'remark' =>$dataori['remark'] ,
            'first_registered_tenant_no'=>$dataori['first_registered_tenant_no'],
                'concurrent_registered_tenant_no'=>$dataori['concurrent_registered_tenant_no'],
                'level'=>$dataori['level'],
                'isTemp'=>$dataori['isTemp'],
                'channelSelectID'=>$dataori['channelSelectID'],
             'updated_by'=>$this->session->userdata('userid'),   
             'bodyoptionother_text'=>$dataori['bodyoptionother_text'],   
             'faceoptionother_text'=>$dataori['faceoptionother_text'],   
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

             //added portable field 2020-0908
            'referrer1'=>$referrer1,  
            'id_number'=>$id_number ,
            'career_name'=>$career_name ,
            'age'=>$age ,
            'body_height'=>$body_height ,
            'body_weight'=>$body_weight ,
            'married_status'=>$married_status ,
            'child_num'=>$child_num ,
            'disease'=>$disease_data,
            'diseaseother'=>$diseaseother ,
            'sensitivity_option'=>$sensitivity_option ,
            'sensitivity'=>$sensitivity ,
            'surgery_option'=>$surgery_option ,
            'surgery'=>$surgery ,
            'medicine_option'=>$medicine_option ,
            'medicine'=>$medicine ,
            'allergy_option'=>$allergy_option ,
            'allergy'=>$allergy ,
            'lifestyle'=>$lifestyle_data,
            'habbit'=>$habbit_data,
            'lighttherapist'=>$lighttheraphist_data,
            'otherlighttherapist'=>$otherlighttherapist ,
            'botoxtherapist'=>$botoxtherapist_data,
            'transtherapist'=>$transtherapist_data,
            'feeding'=>$feeding_data,
            'beautytreat'=>$beautytreat_data,
            'paymentmethod'=>$paymentmethod_data,
            'otherpayment'=>$otherpayment ,
            
            //end added portable field 2020-0908

         );
        }//if($comefrompage=='portable'){    

         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         
        
         // $query=$this->db->insert('mas_rbac_users', $data);
         $this->db->where('id', $dataori['id']);
         $query=$this->db->update('brcustomer', $data);


        

         //add living area
          $data = array(
            // 'customer_id' => $id ,
            'district_id' => $dataori['district_id'] ,
            'area_id' => $dataori['area_id'] ,
            'zone' => $dataori['zone'] ,
            // 'fullName' => $dataori['inputadminname'] ,
            'address' => $dataori['address'] ,
            'status' => 1 ,
            'display' => 1 ,
            // 'mobile' => $dataori['mobile'] ,
            // 'join_date' => $dataori['join_date'] ,
            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('brcustomer_address', $data);

         //end add living area

         $skin_improve_data="";
         if(!empty($dataori['skin_improve'])){
            $skin_improve_data="[".implode(',',$dataori['skin_improve'])."]";
         }
         //add skin improve
         $data = array(
            // 'customer_id' => $id ,
            'skin_improve_ids' =>  $skin_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('brcustomer_skin_improve', $data);
         //end add skin improve

         // body improve
          $body_improve_data="";
         if(!empty($dataori['body_improve'])){
            $body_improve_data="[".implode(',',$dataori['body_improve'])."]";
         }

         $data = array(
            // 'customer_id' => $id ,
            'body_improve_ids' =>  $body_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('brcustomer_body_improve', $data);
         // end add body improve

         // face improve
          $face_improve_data="";
         if(!empty($dataori['face_improve'])){
            $face_improve_data="[".implode(',',$dataori['face_improve'])."]";
         }

         $data = array(
            // 'customer_id' => $id ,
            'face_improve_ids' =>  $face_improve_data ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,
            'display' => 1 ,            
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
          date_default_timezone_set('UTC');
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('brcustomer_face_improve', $data);
         // end add face improve

         //add promotion code
         if(!empty($dataori['promotion_code'])){
              $data = array(
            // 'customer_id' => $id ,
            'promotion_code' =>  $dataori['promotion_code'] ,
            // 'fullName' => $data['inputadminname'] ,
            'status' => 1 ,          
            // 'updated_by' => 1 , //session user id
            // 'created_by' => 1 , //session user id

         );
         $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         $this->db->where('customer_id', $dataori['id']);
         $query=$this->db->update('brcustomer_promotion_code', $data);

         }//if(!empty($dataori['promotion_code'])){
         //end add promotion code


         // if($dataori['need_purchase_service']==true){


         // //add customer service
         // //find category_id
         // $category_id="";
         // $category_id=$this->services_model->getCategoryid($dataori['service_id']);
         // //end find category_id


         // $data = array(
         //    'category_id' => $category_id ,
         //    'service_id' => $dataori['service_id'] ,
         //    'user_id' => $id ,
         //    'purchased_date' => $dataori['purchased_date'] ,
         //    'purchased_qty' => $dataori['purchased_qty'] ,
         //    'available_qty' => $dataori['available_qty'] ,
         //    // 'fullName' => $dataori['inputadminname'] ,
         //    'status' => 1 ,
         //    'remark' => $dataori['remark2'] ,
         //    'display' => 1 ,
         //    // 'updated_by' => 1 , //session user id
         //    // 'created_by' => 1 , //session user id

         // );
         // date_default_timezone_set('UTC');
         // $this->db->set('update_time', time(), FALSE);
         // $this->db->set('create_time', time(), FALSE);
         // $query=$this->db->insert('mas_service_user_'.strtolower($shopsrow->shortcode), $data);
         // //end add customer service
         // }//if($dataori['need_purchase_service']==1){

         return $query;
    }//end function update

    
    function getCustomer($id){
        $this->session->userdata('currentshop');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);    
        $this->db->where('id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getAllCustomer(){
        $this->db->select("*");
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);  
        $query = $this->db->get();
        return $query->result();
    }

    function getCustomerLivingArea($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_address');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerPromotionCode($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_promotion_code');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerSkinImprove($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_skin_improve');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerBodyImprove($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_body_improve');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function getCustomerFaceImprove($id){
        // $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('brcustomer_face_improve');
        $this->db->where('delete_time', NULL);    
        $this->db->where('customer_id', $id);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret;
    }
    function updateStatus($data){
        
            $data2 = array(
            'status' => $data['status'] ,
         );
        date_default_timezone_set('UTC');
        
        $this->db->set('update_time', time(), FALSE);

        $this->db->where('id', $data['id']);
 
        $query=$this->db->update('brcustomer', $data2);
        return $query;

    }
     function getCustomerService($id){

        $this->db->select('u.*, s.code, s.category_id, s.name, s.work, s.price,s.time,s.no_credit_rebate');
        $this->db->from('brservice_user u, brservices s');
        $this->db->where('u.service_id = s.id');
        $this->db->where('u.user_id',$id);
        $this->db->where('u.delete_time', NULL); 
        // $this->db->where('r.permissions = u.permissions');
        $query = $this->db->get();

        // $this->db->from('mas_service_user');
        // $this->db->where('user_id',$id);
        // $this->db->order_by('service_id', 'ASC');
        // $query = $this->db->get();

        return $query->result();

     }

      function getCustomerIdByMobile($mobile){
        $this->db->select('id');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);    
        $this->db->where('mobile', $mobile);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $id = $query->row()->id;
        return $id;
    }
    function getAllCustomercode(){
        $this->db->select("code");
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL); 
        $query = $this->db->get();
        return $query->result();

    }
    function getLastCustomerId($shopid){
        $shopRow=$this->shop_model->getShopbyid($shopid);
        $shortcode=$shopRow->shortcode;

        $this->db->select("*");
        $this->db->from('brcustomer');
        $this->db->like('code', $shortcode);
        $this->db->where('first_registered_tenant_no', $shopid);
         $this->db->order_by('code', 'DESC');    
        $code = $this->db->get()->row()->code;

         $cust_num = substr($code, -4);
         $cust_num = $cust_num+1;
         if($shortcode=="TST"){
            $cust_num = $cust_num+786;//skip first 786 member
         }
         if($shortcode=="CWB"){
            $cust_num = $cust_num+299;//skip first 786 member
         }

         $printCustnum=str_pad($cust_num, 4, '0', STR_PAD_LEFT);
         $printCust=$shortcode.$printCustnum;

         //customer code list
         $customercodelist=$this->getAllCustomercode();

         while($this->existInList($customercodelist,$printCust)==true){
            $cust_num = $cust_num+1;
            $printCustnum=str_pad($cust_num, 4, '0', STR_PAD_LEFT);
            $printCust=$shortcode.$printCustnum;
         }
         
         //end customer code list

        return $printCust;
    }//function getLastCustomer($shopid){
    function existInList($codelist,$findcode){
        foreach ($codelist as $row) {

            if($row->code==$findcode){
                return true;
            }
        }
        return false;
    }
    function getPreferPic($custid){
        $this->db->select('prefer_pic');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);    
        $this->db->where('id', $custid);    
        // $this->db->order_by('code');
        $query = $this->db->get();
        $prefer_pic = $query->row()->prefer_pic;
        return $prefer_pic;
    }//function getPreferPic($custid){

    //end added customermodel function
    function getShopAll()
    {
        
        $this->db->from('bshop');
        // $this->db->where(array('show' => 1));
        $this->db->where(array('deleted' => 0));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }
    function shopcode_exists($code){
        $this->db->where('LOWER(shortcode)', $code);
        $query = $this->db->get('bshop');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function insertShop($inputdata){
           $data = array(
            'shortcode' => $inputdata['shortcode'] ,
            'code' => $inputdata['code'] ,
            'name' => $inputdata['name'] ,
            'status' => 1 ,
            'display' => 0 ,// 0 stand for display, 1 stand for not display
            'deleted' => 0 , //0 stand for not deleted, 1 stand for deleted
            );
           date_default_timezone_set('UTC');
           $this->db->set('update_time', time(), FALSE);
           $this->db->set('create_time', time(), FALSE);
           $query=$this->db->insert('bshop', $data);

           return $query;
    }
    
    function isNewCustomer($id){
        // $today=date("Y-m-d");
        // $timestamp1 = strtotime($today);

        $dateInTwoWeeks = strtotime('+2 weeks');
        $dateBeforeTwoWeeks = strtotime('-2 weeks');


        $this->db->select('code');
        // $shopsrow=$this->shop_model->getShopbyid();
        $this->db->from('brcustomer');
        $this->db->where('delete_time', NULL);      
        $this->db->where('id', $id);
         // $this->db->where('code','');  //cancel empty code for new customer at 2020-11-11   
        $this->db->group_start();
        $this->db->where('create_time <=',$dateInTwoWeeks);
        $this->db->or_where('create_time >=',$dateBeforeTwoWeeks);
        $this->db->group_end();
        // $this->db->order_by('code');
        $query = $this->db->get();
        $num_row=$query->num_rows();
        // $id = $query->row()->code;
        // return $id;
        if($num_row>0){
            return true;
        }
        else{
            return false;
        }
    }

    function getCustomerAllWithDatatable($shopid="",$serachlevel="",$channelSelectID="",$searchmem="",$postData="")
    {
        

        $response = array();

         ## Read value
         $draw = $postData['draw'];
         $start = $postData['start'];
         $rowperpage = $postData['length']; // Rows display per page
         $columnIndex = $postData['order'][0]['column']; // Column index
         $columnName = $postData['columns'][$columnIndex]['data']; // Column name
         $columnSortOrder = $postData['order'][0]['dir']; // asc or desc
         $searchValue = $postData['search']['value']; // Search value

         ## Search 
         $searchQuery = "";
         if($searchValue != ''){
                $searchGender=$searchValue;
                if($searchValue=="男" || $searchValue=="女"){
                    if($searchValue=="女"){
                        $searchGender="0";
                    }
                    if($searchValue=="男"){
                        $searchGender="1";
                    }
                }
                $searchQuery = " (code like '%".$searchValue."%' or display_name like '%".$searchValue."%' or gender like '%".$searchGender."%' or dob like'%".$searchValue."%' or mobile like'%".$searchValue."%' ) ";
     
            
         }

         ## Total number of records without filtering
      $this->db->select("count(*) as allcount");
      $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
         $query = $this->db->get();
         $records = $query->result();
         $totalRecords = $records[0]->allcount;



         ## Total number of record with filtering
     $this->db->select("count(*) as allcount");
    $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));

        
     if($searchQuery != '')
        $this->db->where($searchQuery);
     $query = $this->db->get();
     $records = $query->result();
     $totalRecordwithFilter = $records[0]->allcount;



        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        if($searchQuery != '')
         $this->db->where($searchQuery);
         $this->db->order_by($columnName, $columnSortOrder);
         $this->db->limit($rowperpage, $start);
        // $this->db->order_by('rank');
        $query = $this->db->get();
        $records = $query->result();

         $data = array();

     foreach($records as $record ){
        $displaygender="";
        if($record->gender==0){
            $displaygender="女";     
        }
        if($record->gender==1){
            $displaygender="男";     
        }

        $displaydetails="";
        if($this->session->userdata('brcustomer_access')==true){
            $displaydetails='<a href="/at-cms/brcustomeradmin/detail/'.$record->id.'">詳情</a>';

        }

        $displayedit="";

         //for manager can see other shop but cannot edit
                        $extraPasscheck=false;
                        if($this->session->userdata('shopid')==$shopid){
                          $extraPasscheck=true;
                        }
                        if($this->session->userdata('shopid')!=$shopid && ($this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==7 )){
                          $extraPasscheck=true;
                        }
                //end for manager can see other shop but cannot edit
        if($this->session->userdata('brcustomer_edit')==true  && $extraPasscheck==true){

             $displayedit='<a href="/at-cms/brcustomeradmin/edit/'.$record->id.'">編輯</a>';
        }

        //display open
        $displayopen="";
         $displayopen=$displayopen.'<input type="checkbox"';

        if($record->status=="1"){
                         $displayopen=$displayopen.' checked';
        }
         $displayopen=$displayopen.' data-toggle="toggle" id="checkstatus'.$record->id.' name="checkstatus'.$record->id.'" class="togglenewclass " data-onstyle="success" data-offstyle="danger" data-clickid="'.$record->id.'" ';
        if($this->session->userdata('brcustomer_edit')==false){

            $displayopen=$displayopen.' disabled="disabled"';
         }

        
        if($this->session->userdata('brcustomer_edit')==true){
             $displayopen=$displayopen.' onchange="checkToggle(this)" ';
    
         }//if($this->session->userdata('brcustomer_edit')==true){
       

           $displayopen=$displayopen.'  >  '; 
           //end display open

        
        $data[] = array( 
           "code"=>$record->code,
           "display_name"=>$record->display_name,
           "gender"=>$displaygender,
           "dob"=>$record->dob,
           "mobile"=>$record->mobile,
           "details"=>$displaydetails,
           "edit"=>$displayedit,
           "open"=>$displayopen
        ); 
     }

     ## Response
     $response = array(
        "draw" => intval($draw),
        "iTotalRecords" => $totalRecords,
        "iTotalDisplayRecords" => $totalRecordwithFilter,
        "aaData" => $data
     );

     return $response; 
    }

    function getCustomerAllShopWithDatatableWithSecondShop($shopid="", $secondshopid="",$serachlevel="",$channelSelectID="",$searchmem="",$postData="")
    {
        $response = array();

         ## Read value
         $draw = $postData['draw'];
         $start = $postData['start'];
         $rowperpage = $postData['length']; // Rows display per page
         $columnIndex = $postData['order'][0]['column']; // Column index
         $columnName = $postData['columns'][$columnIndex]['data']; // Column name
         $columnSortOrder = $postData['order'][0]['dir']; // asc or desc
         $searchValue = $postData['search']['value']; // Search value

         ## Search 
         $searchQuery = "";
         if($searchValue != ''){
                $searchGender=$searchValue;
                if($searchValue=="男" || $searchValue=="女"){
                    if($searchValue=="女"){
                        $searchGender="0";
                    }
                    if($searchValue=="男"){
                        $searchGender="1";
                    }
                }
                $searchQuery = " (code like '%".$searchValue."%' or display_name like '%".$searchValue."%' or gender like '%".$searchGender."%' or dob like'%".$searchValue."%' or mobile like'%".$searchValue."%' ) ";
     
            
         }

         ## Total number of records without filtering
      $this->db->select("count(*) as allcount");
      $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->group_start();
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        $this->db->or_where(array('concurrent_registered_tenant_no' => $secondshopid));
        $this->db->group_end(); 
         $query = $this->db->get();
         $records = $query->result();
         $totalRecords = $records[0]->allcount;



         ## Total number of record with filtering
     $this->db->select("count(*) as allcount");
    $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
         $this->db->group_start();
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        $this->db->or_where(array('concurrent_registered_tenant_no' => $secondshopid));
        $this->db->group_end(); 

        
     if($searchQuery != '')
        $this->db->where($searchQuery);
     $query = $this->db->get();
     $records = $query->result();
     $totalRecordwithFilter = $records[0]->allcount;



        $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
         $this->db->group_start();
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        $this->db->or_where(array('concurrent_registered_tenant_no' => $secondshopid));
        $this->db->group_end(); 
        if($searchQuery != '')
         $this->db->where($searchQuery);
         $this->db->order_by($columnName, $columnSortOrder);
         $this->db->limit($rowperpage, $start);
        // $this->db->order_by('rank');
        $query = $this->db->get();
        $records = $query->result();

         $data = array();

     foreach($records as $record ){
        $displaygender="";
        if($record->gender==0){
            $displaygender="女";     
        }
        if($record->gender==1){
            $displaygender="男";     
        }

        $displaydetails="";
        if($this->session->userdata('brcustomer_access')==true){
            $displaydetails='<a href="/at-cms/brcustomeradmin/detail/'.$record->id.'">詳情</a>';

        }

        $displayedit="";

         //for manager can see other shop but cannot edit
                        $extraPasscheck=false;
                        if($this->session->userdata('shopid')==$shopid){
                          $extraPasscheck=true;
                        }
                        if($this->session->userdata('shopid')!=$shopid && ($this->session->userdata('roleId')==6 || $this->session->userdata('roleId')==7 )){
                          $extraPasscheck=true;
                        }
                //end for manager can see other shop but cannot edit
        if($this->session->userdata('brcustomer_edit')==true  && $extraPasscheck==true){

             $displayedit='<a href="/at-cms/brcustomeradmin/edit/'.$record->id.'">編輯</a>';
        }

        //display open
        $displayopen="";

        
        $displayopen=$displayopen.'<div class="toggle btn ';

        if($record->status=="1"){
                         $displayopen=$displayopen.' btn-success';
        }
        if($record->status!="1"){
                         $displayopen=$displayopen.' btn-danger off';
        }
         $displayopen=$displayopen.' data-toggle="toggle" style="width: 61.0781px; height: 38px;"><input type="checkbox" checked="" data-toggle="toggle" id="checkstatus'.$record->id.' name="checkstatus'.$record->id.'" class="togglenewclass " data-onstyle="success" data-offstyle="danger" data-clickid="'.$record->id.'" ';
        if($this->session->userdata('brcustomer_edit')==false){

            $displayopen=$displayopen.' disabled="disabled"';
         }

        
        if($this->session->userdata('brcustomer_edit')==true){
             $displayopen=$displayopen.' onchange="checkToggle(this)" ';
    
         }//if($this->session->userdata('brcustomer_edit')==true){
       

           $displayopen=$displayopen.'  > <div class="toggle-group"><label class="btn btn-success';
           if($record->status=="1"){
                         $displayopen=$displayopen.' active';
        }
        

            $displayopen=$displayopen.' toggle-on">On</label><label class="btn btn-danger ';

            if($record->status!="1"){
                         $displayopen=$displayopen.' active';
        }
            
           $displayopen=$displayopen.' toggle-off">Off</label><span class="toggle-handle btn btn-default"></span></div></div> ';  
        //end display open

        
        $data[] = array( 
           "code"=>$record->code,
           "display_name"=>$record->display_name,
           "gender"=>$displaygender,
           "dob"=>$record->dob,
           "mobile"=>$record->mobile,
           "details"=>$displaydetails,
           "edit"=>$displayedit,
           "open"=>$displayopen
        ); 
     }

     ## Response
     $response = array(
        "draw" => intval($draw),
        "iTotalRecords" => $totalRecords,
        "iTotalDisplayRecords" => $totalRecordwithFilter,
        "aaData" => $data
     );

     return $response; 
    }
    function getCustomerAllDataListPortable($shopid="",$postData="")
    {
        

        $response = array();

         ## Read value
         $draw = $postData['draw'];
         $start = $postData['start'];
         $rowperpage = $postData['length']; // Rows display per page
         $columnIndex = $postData['order'][0]['column']; // Column index
         $columnName = $postData['columns'][$columnIndex]['data']; // Column name
         $columnSortOrder = $postData['order'][0]['dir']; // asc or desc
         $searchValue = $postData['search']['value']; // Search value

         ## Search 
         $searchQuery = "";
         if($searchValue != ''){
                $searchGender=$searchValue;
                if($searchValue=="男" || $searchValue=="女"){
                    if($searchValue=="女"){
                        $searchGender="0";
                    }
                    if($searchValue=="男"){
                        $searchGender="1";
                    }
                }
                $searchQuery = " (code like '%".$searchValue."%' or display_name like '%".$searchValue."%' or gender like '%".$searchGender."%' or dob like'%".$searchValue."%' or mobile like'%".$searchValue."%' ) ";
     
            
         }

         ## Total number of records without filtering
      $this->db->select("count(*) as allcount");
      $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
         $query = $this->db->get();
         $records = $query->result();
         $totalRecords = $records[0]->allcount;



         ## Total number of record with filtering
     $this->db->select("count(*) as allcount");
    $this->db->from('brcustomer');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        $this->db->where('delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));

        
     if($searchQuery != '')
        $this->db->where($searchQuery);
     $query = $this->db->get();
     $records = $query->result();
     $totalRecordwithFilter = $records[0]->allcount;


        $this->db->select('brcustomer.*,brportableform1a.customer_id as form1a,brportableform2a.customer_id as form2a,brportableform2b.customer_id as form2b,brportableform4a.customer_id as form4a,brportableform3a.customer_id as form3a ');

        $this->db->from('brcustomer');
        $this->db->join('brportableform1a', 'brcustomer.id = brportableform1a.customer_id  ','left');
        $this->db->join('brportableform2a', 'brcustomer.id = brportableform2a.customer_id  ','left');
        $this->db->join('brportableform2b', 'brcustomer.id = brportableform2b.customer_id  ','left');
        $this->db->join('brportableform4a', 'brcustomer.id = brportableform4a.customer_id  ','left');
        $this->db->join('brportableform3a', 'brcustomer.id = brportableform3a.customer_id  ','left');
        // $this->db->where(array('show' => 1));
        // $this->db->where(array('deleted' => 0));
        // $this->db->where('delete_time', NULL);
        $this->db->where('brcustomer.delete_time', NULL); 
        $this->db->where('brportableform1a.delete_time', NULL); 
        $this->db->where('brportableform2a.delete_time', NULL); 
        $this->db->where('brportableform2b.delete_time', NULL); 
        $this->db->where('brportableform4a.delete_time', NULL); 
        $this->db->where('brportableform3a.delete_time', NULL); 
        if(!empty($serachlevel)){
           $this->db->where(array('level' => $serachlevel)); 
        }
        if(!empty($channelSelectID)){
           $this->db->where(array('channelSelectID' => $channelSelectID)); 
        }
        if(!empty($searchmem)){
            if($searchmem==1){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => $searchmem)); 
                $this->db->or_where('code =', "");
                $this->db->group_end(); //th
                
            }
            if($searchmem==2){
                $this->db->group_start(); //this will start grouping
                $this->db->where(array('isTemp' => '0')); 
                $this->db->or_where('code !=', "");
                $this->db->group_end(); //th
            }
        }
        $this->db->where(array('concurrent_registered_tenant_no' => $shopid));
        if($searchQuery != '')
         $this->db->where($searchQuery);
         // $this->db->order_by($columnName, $columnSortOrder);
        $searchColumn="";
        if($columnName!="details" ){
            $searchColumn=$columnName;
            $this->db->order_by($searchColumn, $columnSortOrder);
        }
         $this->db->limit($rowperpage, $start);
        // $this->db->order_by('rank');
        $query = $this->db->get();
        $records = $query->result();

         $data = array();

     foreach($records as $record ){
        $displaygender="";
        if($record->gender==0){
            $displaygender="女";     
        }
        if($record->gender==1){
            $displaygender="男";     
        }

        $displaydetails="";
        if($this->session->userdata('brcustomer_access')==true){
            $displaydetails='<a href="/portable/brcustomeradmin/edit/'.$record->id.'">詳情</a>';

        }

       

        $display_name=$record->display_name;
        if(!empty($record->mobile)){
            $display_name=$display_name.'<br>('.$record->mobile.')';
        }

        
        //$form1adata=$this->protableform1a_model->checkSubmited($record->id);
        $displayform1a="";
        $displayform1a=$displayform1a.'<a href="/portable/form1a/edit/'.$record->id.'"><div style="width:100%;height:100%;overflow:auto;">';
        //if($form1adata==true){
        if($record->form1a!=null){
            $displayform1a=$displayform1a.'<p><b>✓</b></p>';
        }
        //if($form1adata==false){
        if($record->form1a==null){
            $displayform1a=$displayform1a.'<p><b>✕</b></p>';
        }
        $displayform1a=$displayform1a.'</div></a>';

        // $form2adata=$this->protableform2a_model->checkSubmited($record->id);
        $displayform2a="";
        $displayform2a=$displayform2a.'<a href="/portable/form2a/edit/'.$record->id.'"><div style="width:100%;height:100%;overflow:auto;">';
        // if($form2adata==true){
        if($record->form2a!=null){
            $displayform2a=$displayform2a.'<p><b>✓</b></p>';
        }
        // if($form2adata==false){
        if($record->form2a==null){
            $displayform2a=$displayform2a.'<p><b>✕</b></p>';
        }
        $displayform2a=$displayform2a.'</div></a>';


        // $form2bdata=$this->protableform2b_model->checkSubmited($record->id);
        $displayform2b="";
        $displayform2b=$displayform2b.'<a href="/portable/form2b/edit/'.$record->id.'"><div style="width:100%;height:100%;overflow:auto;">';
        // if($form2bdata==true){
        if($record->form2b!=null){
            $displayform2b=$displayform2b.'<p><b>✓</b></p>';
        }
        // if($form2bdata==false){
        if($record->form2b==null){
            $displayform2b=$displayform2b.'<p><b>✕</b></p>';
        }
        $displayform2b=$displayform2b.'</div></a>';


        // $form3adata=$this->protableform3a_model->checkSubmited($record->id);
        $displayform3a="";
        $displayform3a=$displayform3a.'<a href="/portable/form2b/edit/'.$record->id.'"><div style="width:100%;height:100%;overflow:auto;">';
        // if($form3adata==true){
        if($record->form3a!=null){
            $displayform3a=$displayform3a.'<p><b>✓</b></p>';
        }
        // if($form3adata==false){
        if($record->form3a==null){
            $displayform3a=$displayform3a.'<p><b>✕</b></p>';
        }
        $displayform3a=$displayform3a.'</div></a>';

        // $form4adata=$this->protableform4a_model->checkSubmited($record->id);
        $displayform4a="";
        $displayform4a=$displayform4a.'<a href="/portable/form2b/edit/'.$record->id.'"><div style="width:100%;height:100%;overflow:auto;">';
        // if($form4adata==true){
        if($record->form4a!=null){
            $displayform4a=$displayform4a.'<p><b>✓</b></p>';
        }
        // if($form4adata==false){
        if($record->form4a==null){
            $displayform4a=$displayform4a.'<p><b>✕</b></p>';
        }
        $displayform4a=$displayform4a.'</div></a>';

        $data[] = array( 
           "code"=>$record->code,
           "display_name"=>$display_name,
           "gender"=>$displaygender,
           "dob"=>$record->dob,
           "details"=>$displaydetails,
           "form1a"=>$displayform1a,
           "form2a"=>$displayform2a,
           "form2b"=>$displayform2b,
           "form3a"=>$displayform3a,
           "form4a"=>$displayform4a
        ); 
     }

     ## Response
     $response = array(
        "draw" => intval($draw),
        "iTotalRecords" => $totalRecords,
        "iTotalDisplayRecords" => $totalRecordwithFilter,
        "aaData" => $data
     );

     return $response; 
    }
    function getBannerAll()
    {
        
        $this->db->from('tblFolders');
        // $this->db->where(array('show' => 1));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }

    // function getAdmindata(){
    //     $this->db->select('id,login, fullName, update_time');
    //     $query = $this->db->get('adminUsers');
    //     return $query->result();
    // }
    // function record_count(){
    //     $this->db->select('id');
    //     $this->db->where('level1', '0');
    //     $this->db->where('level2', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();
    // }
    // function count_all_folders(){
    //     $this->db->select('id');
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();    
    // }   
    // function getflodercount($id){
    //     $this->db->select('id');
    //     $this->db->where('level1', $id);
    //     // $this->db->where('level2!=', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->num_rows();    
    // }    

    // function getfolderdata($perpage,$page){
    //     $this->db->select('id,name,level1, update_time');
    //     $offset=$page* $perpage;
    //     $this->db->where('level1', 0);
    //     $this->db->limit($perpage, $offset);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }


    // function getlevel1data($id)
    // {
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel2data($id)
    // {
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level2', $id);
    //     $this->db->where('level3', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel3data($id){
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level3', $id);
    //     $this->db->where('level4', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }
    // function getlevel4data($id){
    //     $this->db->select('id,name,level1, update_time');
    //     $this->db->where('level4', $id);
    //     $this->db->where('level5', NULL);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }

    // //get all folder data for rightend
    // function getallfolderdata(){
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }    
    // function getalllevel1datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level1', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // } 
    // function getalllevel2datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level2', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // } 
    // function getalllevel3datainclude($id)
    // {
    //     $this->db->select('id,name,level1,level2,level3,level4, update_time');
    //     $this->db->where('level3', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }    

    // //end get all folder data for rightend

    // function getAdmindateByid($id){
    //     $this->db->select('id,login, companyName,fullName,tel, update_time');
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     return $query->result();
    // }

    
    // function getFolderName($id){
    //     $this->db->select('name');
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     if ($query->num_rows() > 0) {
    //         // return $query->first_row();
    //          $row = $query->row(); 
    //         return $row->name;
    //     }
    //     return "";
    // }

    // function insertFolder($data){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
    //     $data = array(
    //         'name' => $data['inputflodername'] ,
    //         'level1' => '0' ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelFolder($data){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
    //     if($data['addlevel']==1){
    //         $data = array(
    //             'name' => $data['inputflodernamelevel'] ,
    //             'level1' => '0' ,
                
    //          );
    //     }
    //     if($data['addlevel']==2){
    //         $data = array(
    //             'name' => $data['inputflodernamelevel'] ,
    //             'level1' => $data['addlevelid'] ,
                
    //          );
    //     }
        
        
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelOneFolder($name,$level){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => '0' ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
        
    // }
    // function insertLevelTwoFolder($name,$addlevel,$addlevelid){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
            
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelThreeFolder($name,$addlevel,$addlevelid,$secondvalues){
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
    //         'level2' => $secondvalues ,
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }

    // function insertLevelFourFolder($name,$addlevel,$addlevelid,$thirdvalues){
    //     $secondlevelid=$this->findLevelThreeid($thirdvalues);
    //     $data = array(
    //         'name' => $name ,
    //         'level1' => $addlevelid ,
    //         'level2' => $secondlevelid ,
    //         'level3' => $thirdvalues ,
    //      );
    //      $this->db->set('update_time', 'NOW()', FALSE);
    //      $query=$this->db->insert('tblFolders', $data);
    //      return $query;
    // }
    // function insertLevelMonthWeekFolder($values,$addlevel,$addlevelid,$secondvalues){
    //     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
    //     $weekarray =  array("Week 1", "Week 2", "Week 3", "Week 4", "Week 5");
    //     foreach($montharray as $month){
    //         if($this->folder_levelThreeexists(strtolower($month),$addlevel,$addlevelid,$secondvalues)==true){
    //              $data = array(
    //             'name' =>  $month ,
    //             'level1' => $addlevelid ,
    //             'level2' => $secondvalues ,
    //          );
    //          $this->db->set('update_time', 'NOW()', FALSE);
    //          $query=$this->db->insert('tblFolders', $data);
    //          $lastinsertid = $this->db->insert_id();
    //          foreach ($weekarray as $week) {

    //              $data = array(
    //             'name' =>  $week ,
    //             'level1' => $addlevelid ,
    //             'level2' => $secondvalues ,
    //             'level3' => $lastinsertid ,
    //              );
    //             $this->db->set('update_time', 'NOW()', FALSE);
    //              $query=$this->db->insert('tblFolders', $data);
    //          }

    //         }//if(folder_levelThreeexists($month,$addlevel,$addlevelid,$secondvalues)==false){
    //     }//foreach($montharray as $month){
    //     return $query;
    // }

    // function updateAdmin($input){
    //     // 'inputadminname'=>$inputadminname,
    //     //         'inputloginname'=>$inputloginname,
    //     //         'inputPassword'=>$inputPassword,
        
    //     if(empty($input['inputPassword'])){
    //         $data = array(
    //             'login' => $input['inputloginname'] ,
    //             'companyName' => $input['inputcompanynameedit'],
    //             'fullName' => $input['inputfullnameedit'], 
    //             'tel' => $input['inputteledit']  
                
    //          );
    //     }
    //     else{
    //         $data = array(
    //             'login' => $input['inputloginname'] ,
    //             'companyName' => $input['inputcompanynameedit'],
    //             'fullName' => $input['inputfullnameedit'], 
    //             'tel' => $input['inputteledit']  ,
    //             'pwd' => $input['inputPassword'] 
    //          );
    //     }
        
    //      $this->db->set('update_time', 'NOW()', FALSE);

    //      $this->db->where('id', $input['id']);
    //      $query=$this->db->update('tblFolders', $data);
    //      return $query;
    // }

    // function deleteAdmin($id){
    //     $this->db->where('id', $id);
    //     $query = $this->db->get('tblFolders');
    //     $row = $query->row(); 

    //     //check level of delete
    //     $deleteLevel=0;
    //     if($row->level4 ==NULL  && $row->level3!=NULL){
    //         $deleteLevel=4;
    //     }
    //     if($row->level3 ==NULL  && $row->level2!=NULL){
    //         $deleteLevel=3;
    //     }
    //     if($row->level2 ==NULL  && $row->level1!=NULL && $row->level1!=0){
    //         $deleteLevel=2;
    //     }
    //     if( $row->level1==0){
    //         $deleteLevel=1;
    //     }

    //     //end check level of delete
    //     if($deleteLevel==4){
    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==3){

    //         $this->db->where('level3 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level3', $id);
    //              $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==2){
    //         $this->db->where('level2 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level2', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }
    //     if($deleteLevel==1){
    //         $this->db->where('level1 ',$id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('level1', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }

    //         $this->db->where('id', $id);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() != 0){
    //             $this->db->where('id', $id);
    //             $query=$this->db->delete('tblFolders');
    //         }
    //     }

        
    //     return $query;
    // }

    // function folder_exists($foldername){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('LOWER(name)', $foldername);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function folder_levelOneexists($foldername){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', '0');
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function folder_levelTwoexists($foldername,$level,$id){
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function folder_levelThreeexists($foldername,$level,$id,$secondllevelid){
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', $secondllevelid);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function folder_levelFourexists($foldername,$level,$id,$thirdllevelid){
    //     $secondlevelid=$this->findLevelThreeid($thirdllevelid);
    //     $this->db->where('LOWER(name)', $foldername);
    //     $this->db->where('level1', $id);
    //     $this->db->where('level2', $secondlevelid);
    //     $this->db->where('level3', $thirdllevelid);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }

    // }

    // function folder_levelTwoMonthWeekexists($values,$addlevel,$addlevelid,$secondvalues){
    //     $montharray = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec");
    //     foreach($montharray as $month){
    //         $this->db->select("id");
    //         $this->db->where('name', $month);
    //         $this->db->where('level1', $addlevelid);
    //         $this->db->where('level2', NULL);
    //         $query = $this->db->get('tblFolders');
    //         if($query->num_rows() == 1){
    //             return false;
    //             //check week    
                

    //             //end check week

    //         } else {

    //         }
    //     }
        
    //     return true;
    // }

    // function checkleveltwoid($secondlevelid){
    //     $this->db->where('id', $secondlevelid);
    //     $this->db->where('level1 !=',0);
    //     $this->db->where('level2 ', NULL);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function checklevelthreeid($thirdllevelid){
    //     $this->db->where('id', $thirdllevelid);
    //     $this->db->where('level2 !=', NULL);
    //     $this->db->where('level3 ', NULL);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
    // function findLevelThreeid($thirdllevelid){
    //     $this->db->select("level2");
    //     $this->db->where('id', $thirdllevelid);
    //     $this->db->from('tblFolders');
    //     $query = $this->db->get();
    //     // $ret = $query->row();
    //     // return $ret->level2;
    //     if ($query->num_rows() > 0)
    //     {
    //         $row = $query->row(); 
    //         return $row->level2;
    //     }
    //     return NULL;
    // }

    // function login_existsbyid($loginname,$id){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(login)', $loginname);
    //     $this->db->where('id!=', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }
   

    // function admin_exists($loginname){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(fullName)', $loginname);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }

    // function admin_existsbyid($loginname,$id){
    //     // $this->db->from('adminUsers');
    //     $this->db->where('LOWER(fullName)', $loginname);
    //     $this->db->where('id!=', $id);
    //     $query = $this->db->get('tblFolders');
    //     if($query->num_rows() == 1){
    //         return false;
    //     } else {
    //         return true;
    //     }
    // }


    // //process login
    // function check(){
    //     $userName =  $this->input->post('userName');
    //     $password =  $this->input->post('password');

    //     $this->db->where('login',$userName);
    //     $this->db->where('pwd',$password);
    //     // $this->db->where('user_active','yes');
    //     return $this->db->get('tblFolders')->row_array();
    // }


    // function processLogin($userName=NULL,$password){



    //     $this->db->select("id,login,fullName");
    //     $whereCondition = $array = array('login' =>$userName,'pwd'=>$password);
    //     $this->db->where($whereCondition);
    //     $this->db->from('tblFolders');
    //     $query = $this->db->get();
    //     return $query;
    // }//function processLogin(){
    // //end process login

    // function buildTopMenuItem()
    // {
    //     // $menu = array();
        
    //     // $result = $this->getSectionAll();
    //     // foreach ($result as $row)
    //     // {
    //     //     $menu[] = array(
    //     //         'name_zh' => $row->cp_name,
    //     //         'name_cn' => $row->sp_name,
    //     //         'name_en' => $row->ep_name,
    //     //         'link' => $row->link
    //     //     );
    //     // }
    //     // return $menu;
        
    // }
}