<?php  

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



class Portableform4aend extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->library('session');
        // $this->load->model('Brcustomermodel', 'customer_model');
        $this->load->model('BrPortableform4amodel', 'portableform4a_model');
        // $this->load->model('BrPortableFormModel', 'portableform_model');
        // $this->load->model('Brshopmodel', 'shop_model');
        // $this->load->model('BrservicesuserModel', 'servicesuser_model');

    }
    //added portable function
    public function add(){
       
            // $request= new Request();
            // $admin=$request->getBody();

            // 'inputadminname':$('#inputadminname').val(),
            //     'inputloginname':$('#inputloginname').val(),
            //     'inputPassword'
            // $name=$this->input->post('name');
            
            //add security trim input-todo
            //hash password for security
            $currentFormId=$this->input->post('currentFormId');
            $question=$this->input->post('question');
            $signaturePad=$this->input->post('signaturePad');
            // $chi_name=$this->input->post('chi_name');
            // $eng_firstname=$this->input->post('eng_firstname');
            // $eng_lastname=$this->input->post('eng_lastname');
            $employeename_text=$this->input->post('employeename_text');
            $employsignaturePad=$this->input->post('employsignaturePad');

            

            $data= array(
                'currentFormId'=>$currentFormId,
                'question'=>$question,
                // 'chi_name'=>$chi_name,
                // 'eng_firstname'=>$eng_firstname,
                // 'inputadminname'=>$inputadminname,
                // 'eng_lastname'=>$eng_lastname,
                'signaturePad'=>$signaturePad,
                'employeename_text'=>$employeename_text,
                'employsignaturePad'=>$employsignaturePad,


            );

            // $code="";
            // if($this->frontenduser_model->admin_exists($inputadminname)==false){
            //     $code="FAILURE";
            //     $msg="已有管理員姓名".$inputadminname;
            // }
            // else
            // if($this->customer_model->customercode_exists(strtolower($code))==false){
            //     $entercode=$code;
            //     $code="FAILURE";
            //     $msg="已有客戶編號".$entercode;
            // }

            // $headcode=$this->shop_model->getShopcodeHead($first_registered_tenant_no);


            if($this->portableform4a_model->form_exists($currentFormId)==false){
                // $entercode=$code;
                // $code="FAILURE";
                // $msg="新增失敗。已有激光療程同意書";

                $newFormId=$this->portableform4a_model->getFormIdByCustId($currentFormId);

                 $data= array(
                'currentFormId'=>$newFormId,
                'currentUserId'=>$currentFormId,
                'question'=>$question,
                // 'chi_name'=>$chi_name,
                // 'eng_firstname'=>$eng_firstname,
                // 'inputadminname'=>$inputadminname,
                // 'eng_lastname'=>$eng_lastname,
                'signaturePad'=>$signaturePad,
                'employeename_text'=>$employeename_text,
                'employsignaturePad'=>$employsignaturePad,


            );


                $result=$this->portableform4a_model->updateForm($data);


                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能加入";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功加入"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }


            }
            // elseif(!empty($inputcode) && strpos($inputcode, $headcode) !== 0){
            //     $code="FAILURE";
            //     $msg="新增失敗。客戶編號的前綴應為 ".$headcode."。";
            // }
            // elseif(!empty($inputcode) && $inputcode ==$headcode."0000"){
            //     $code="FAILURE";
            //     $msg="新增失敗。請填寫客戶編號.現在客戶編號是".$headcode."0000"."。";
            // }
            // elseif($this->customer_model->customercode_exists($inputcode)==false){
            //     $code="FAILURE";
            //     $msg="新增失敗。已有客戶編號".$inputcode;
            // }
            else{
                $result=$this->portableform4a_model->insertForm($data);
                
                
                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能加入";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功加入"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }
            }

             
            

             header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
            $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
            echo json_encode($myArray);
            // echo "posted";
        
    }

    public function updateform(){
        //if form exist update 
        //else insert form
       
            // $request= new Request();
            // $admin=$request->getBody();

            // 'inputadminname':$('#inputadminname').val(),
            //     'inputloginname':$('#inputloginname').val(),
            //     'inputPassword'
            // $name=$this->input->post('name');
            
            //add security trim input-todo
            //hash password for security
            $currentFormId=$this->input->post('currentFormId');
            $currentUserId=$this->input->post('currentUserId');
            $question=$this->input->post('question');
            $signaturePad=$this->input->post('signaturePad');
            // $chi_name=$this->input->post('chi_name');
            // $eng_firstname=$this->input->post('eng_firstname');
            // $eng_lastname=$this->input->post('eng_lastname');
            $employeename_text=$this->input->post('employeename_text');
            $employsignaturePad=$this->input->post('employsignaturePad');
           
            

            

            // $code="";
            // if($this->frontenduser_model->admin_exists($inputadminname)==false){
            //     $code="FAILURE";
            //     $msg="已有管理員姓名".$inputadminname;
            // }
            // else
            // if($this->customer_model->customercode_exists(strtolower($code))==false){
            //     $entercode=$code;
            //     $code="FAILURE";
            //     $msg="已有客戶編號".$entercode;
            // }

            // $headcode=$this->shop_model->getShopcodeHead($first_registered_tenant_no);


            if($this->portableform4a_model->form_exists($currentUserId)==false){
                // $entercode=$code;
                // $code="FAILURE";
                // $msg="新增失敗。已有Ther Matrix 同意書";
                //update form 


                $data= array(
                'currentFormId'=>$currentFormId,
                'currentUserId'=>$currentUserId,
                'question'=>$question,
                // 'chi_name'=>$chi_name,
                // 'eng_firstname'=>$eng_firstname,
                // 'inputadminname'=>$inputadminname,
                // 'eng_lastname'=>$eng_lastname,
                'signaturePad'=>$signaturePad,
                'employeename_text'=>$employeename_text,
                'employsignaturePad'=>$employsignaturePad,


            );


                $result=$this->portableform4a_model->updateForm($data);


                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能加入";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功加入"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }
                //end update form


            }
            // elseif(!empty($inputcode) && strpos($inputcode, $headcode) !== 0){
            //     $code="FAILURE";
            //     $msg="新增失敗。客戶編號的前綴應為 ".$headcode."。";
            // }
            // elseif(!empty($inputcode) && $inputcode ==$headcode."0000"){
            //     $code="FAILURE";
            //     $msg="新增失敗。請填寫客戶編號.現在客戶編號是".$headcode."0000"."。";
            // }
            // elseif($this->customer_model->customercode_exists($inputcode)==false){
            //     $code="FAILURE";
            //     $msg="新增失敗。已有客戶編號".$inputcode;
            // }
            else{

                $data= array(
                'currentFormId'=>$currentUserId,
                'question'=>$question,
                // 'chi_name'=>$chi_name,
                // 'eng_firstname'=>$eng_firstname,
                // 'inputadminname'=>$inputadminname,
                // 'eng_lastname'=>$eng_lastname,
                'signaturePad'=>$signaturePad,
                'employeename_text'=>$employeename_text,
                'employsignaturePad'=>$employsignaturePad,


            );


                $result=$this->portableform4a_model->insertForm($data);
                
                
                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能加入";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功加入"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }
            }

             
            

             header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
            $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
            echo json_encode($myArray);
            // echo "posted";
        

    }
    public function deleteform(){
        $currentFormId=$this->input->post('currentFormId');
        $currentUserId=$this->input->post('currentUserId');
        $question=$this->input->post('question');
        $signaturePad=$this->input->post('signaturePad');


        if($this->portableform4a_model->form_exists($currentUserId)==false){
                 $data= array(
                        'currentFormId'=>$currentFormId,
                        'currentUserId'=>$currentUserId,
                        'question'=>$question,
                        // 'chi_name'=>$chi_name,
                        // 'eng_firstname'=>$eng_firstname,
                        // 'inputadminname'=>$inputadminname,
                        // 'eng_lastname'=>$eng_lastname,
                        'signaturePad'=>$signaturePad,


                    );


                $result=$this->portableform4a_model->deleteForm($data);
                
                if($result==true){
                    $code="SUCCESS";
                    $msg="相關記錄成功删徐";
                }
                else{
                    $code="FAILURE";
                    $msg="相關記錄不能删徐";
                }

        }
        else{
              $code="SUCCESS";
              $msg="相關記錄成功删徐";
        }
       
        // $msg=$inputdeletecheckboxvalue;
        header('Content-Type: application/json');
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
        echo json_encode($myArray);
    }    

    //end added portable function

    public function selectform(){
        $formSelection=$this->input->post('formSelection');
        $currentFormId=$this->input->post('currentFormId');

        //return url of form selection
        
        //clear all form selection
        $this->portableform_model->setNeedForm1a(0);
        $this->portableform_model->setSubmittedForm1a(0);
        $this->portableform_model->setNeedForm2a(0);
        $this->portableform_model->setSubmittedForm2a(0);
        $this->portableform_model->setNeedForm2b(0);
        $this->portableform_model->setSubmittedForm2b(0);
        $this->portableform_model->setNeedForm3a(0);
        $this->portableform_model->setSubmittedForm3a(0);
        //end clear all form selection


        $returnUrl="";
        if(empty($formSelection)){
            $returnUrl='/portable/brcustomeradmin/success';
        }//if(empty($formSelection)){

        foreach ($formSelection as $selectedForm) {
            if($selectedForm==1){
               if(empty($returnUrl)){
                    $returnUrl='/portable/form1a/add';
                     
               }
               $this->portableform_model->setNeedForm1a(1);
            }//if($selectedForm==1){
            if($selectedForm==2){
               if(empty($returnUrl)){
                    $returnUrl='/portable/form2a/add';
                    
               }
                $this->portableform_model->setNeedForm2a(1);
                $this->portableform_model->setNeedForm2b(1);
            }//if($selectedForm==2){
            if($selectedForm==3){
               if(empty($returnUrl)){
                    $returnUrl='/portable/form3a/add';
                     
               }
               $this->portableform_model->setNeedForm3a(1);
            }//if($selectedForm==3){
               
        }   





        //end return url of form selection



        if(empty($currentFormId)){
                    $code="FAILURE";
                    $msg="同意書不能選擇";     
        }
        else{
                    $code="SUCCESS";
                    $msg=""; 
                    //for add user it will add a secret key to database
                    
                    //end 

        }

        header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg,'returnUrl'=>$returnUrl];
        echo json_encode($myArray);
            // echo "posted";

    }//public function selectform(){



    //end added function

    public function get($id=Null){
        $partnerlist=$this->frontenduser_model->getBannerAll();
        echo  '1';
        print_r($partnerlist);


        
    }


    
    public function update(){
            $id=$this->input->post('id');
            $inputcode=$this->input->post('code');
            $display_name=$this->input->post('display_name');
            // $chi_name=$this->input->post('chi_name');
            // $eng_firstname=$this->input->post('eng_firstname');
            // $eng_lastname=$this->input->post('eng_lastname');
            $gender=$this->input->post('gender');
            $dob=$this->input->post('dob');
            $mobile=$this->input->post('mobile');
            $district_id=$this->input->post('district_id');
            $area_id=$this->input->post('area_id');
            $zone=$this->input->post('zone');
            $address=$this->input->post('address');
            $join_date=$this->input->post('join_date');
            $shop_id=$this->input->post('shop_id');
            $skin_improve=$this->input->post('skin_improve');
            $referred_mobile=$this->input->post('referred_mobile');
            $remark=$this->input->post('remark');
            $oridistrict_id=$this->input->post('oridistrict_id');
            $oriarea_id=$this->input->post('oriarea_id');
            $orizone=$this->input->post('orizone');
            $first_registered_tenant_no=$this->input->post('first_registered_tenant_no');
            $concurrent_registered_tenant_no=$this->input->post('concurrent_registered_tenant_no');
            $level=$this->input->post('level');
            $isTemp=$this->input->post('isTemp');
            $channelSelectID=$this->input->post('channelSelectID');
            $body_improve=$this->input->post('body_improve');
            $bodyoptionother_text=$this->input->post('bodyoptionother_text');
            $face_improve=$this->input->post('face_improve');
            $faceoptionother_text=$this->input->post('faceoptionother_text');
    
            // $need_purchase_service=$this->input->post('need_purchase_service');
            // $service_id=$this->input->post('service_id');
            // $purchased_date=$this->input->post('purchased_date');
            // $purchased_qty=$this->input->post('purchased_qty');
            // $available_qty=$this->input->post('available_qty');
            // $remark2=$this->input->post('remark2');

            if(!empty($district_id)){
                $inputDistrict_id=$district_id;
            }
            else{
                $inputDistrict_id=$oridistrict_id;
            }
            if(!empty($area_id)){
                $inputArea_id=$area_id;
            }
            else{
                $inputArea_id=$oriarea_id;
            }
            if(!empty($zone)){
                $inputZone=$zone;
            }
            else{
                $inputZone=$orizone;
            }

            $data= array(
                'id'=>$id,
                'code'=>$inputcode,
                'display_name'=>$display_name,
                // 'chi_name'=>$chi_name,
                // 'eng_firstname'=>$eng_firstname,
                // 'inputadminname'=>$inputadminname,
                // 'eng_lastname'=>$eng_lastname,
                'gender'=>$gender,
                'dob'=>$dob,
                'mobile'=>$mobile,
                'district_id'=>$inputDistrict_id,
                'area_id'=>$inputArea_id,
                'zone'=>$inputZone,
                'address'=>$address,
                'join_date'=>$join_date,
                'shop_id'=>$shop_id,
                'skin_improve'=>$skin_improve,
                'referred_mobile'=>$referred_mobile,
                'remark'=>$remark,
                'oridistrict_id'=>$oridistrict_id,
                'oriarea_id'=>$oriarea_id,
                'orizone'=>$orizone,
                'first_registered_tenant_no'=>$first_registered_tenant_no,
                'concurrent_registered_tenant_no'=>$concurrent_registered_tenant_no,
                'level'=>$level,
                'isTemp'=>$isTemp,
                'channelSelectID'=>$channelSelectID,
                'body_improve'=>$body_improve,
                'bodyoptionother_text'=>$bodyoptionother_text,
                'face_improve'=>$face_improve,
                'faceoptionother_text'=>$faceoptionother_text,
                // 'need_purchase_service'=>$need_purchase_service,
                // 'service_id'=>$service_id,
                // 'purchased_date'=>$purchased_date,
                // 'purchased_qty'=>$purchased_qty,
                // 'available_qty'=>$available_qty,
                // 'remark2'=>$remark2,
  
            );

            // $code="";
            // if($this->frontenduser_model->admin_exists($inputadminname)==false){
            //     $code="FAILURE";
            //     $msg="已有管理員姓名".$inputadminname;
            // }
            // else
            // if($this->customer_model->customercode_existsbyid($code,$id)==false){
            //     $entercode=$code;
            //     $code="FAILURE";
            //     $msg="已有客戶編號".$entercode;
            // }
            $headcode=$this->shop_model->getShopcodeHead($first_registered_tenant_no);


            if($this->customer_model->customermobile_existsbyid($mobile,$id)==false){
                // $entercode=$code;
                $code="FAILURE";
                $msg="更改失敗。已有客戶電話號碼".$mobile;
            }
            elseif(!empty($inputcode) && strpos($inputcode, $headcode) !== 0){
                $code="FAILURE";
                $msg="更改失敗。客戶編號的前綴應為 ".$headcode."。";
            }
            elseif(!empty($inputcode) && $inputcode ==$headcode."0000"){
                $code="FAILURE";
                $msg="更改失敗。請填寫客戶編號.現在客戶編號是".$headcode."0000"."。";
            }
            elseif($this->customer_model->customercode_existsbyid($inputcode,$id)==false){
                $code="FAILURE";
                $msg="新增失敗。已有客戶編號".$inputcode;
            }
            else{
                $result=$this->customer_model->updateCustomer($data);
                if($result==false){
                    $code="FAILURE";
                    $msg="記錄不能更改";     
                }
                else{
                    $code="SUCCESS";
                    $msg="記錄成功更改"; 
                    //for add user it will add a secret key to database
                    
                    //end 

                }
            }

             
            

             header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
            $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
            echo json_encode($myArray);
            // echo "posted";
    }
    public function status(){
        $action=$this->input->post('action');
            $personid=$this->input->post('personid');
            if($action=="add"){


        //insert first level id
            $data= array(

            'id'=>$personid,
            'status'=>1
            // 'level1id'=>$level1id,
            // // 'inputadminname'=>$inputadminname,
            // 'level2id'=>$level2id,
            // 'level3id'=>$level3id,
            // 'level4id'=>$level4id
        );

            $result=$this->customer_model->updateStatus($data);
        }
            if($action=="remove"){    
             $data= array(

             'id'=>$personid,
            'status'=>0
        );

         $result=$this->customer_model->updateStatus($data);
        }//if($action=="remove"){

            if($result==false){
                $code="FAILURE";
                $msg="記錄不能更改";     
            }
            else{
                $code="SUCCESS";
                $msg="記錄成功更改";    
            }

            header('Content-Type: application/json');
            // $code="SUCCESS";
            // $code="FAILURE";
            // $msg="已有相關記錄";
            // $myArray = ['id'=>12, 'name'=>'Topi'];
            $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
            echo json_encode($myArray);
            // echo "posted";

    }
    public function getPreferPic(){
            $cust_id=$this->input->post('cust_id');
            $data=$this->customer_model->getPreferPic($cust_id);
             header('Content-Type: application/json');
            echo json_encode($data);

    }//public function getPreferPic(){
    public function edit(){
       
        // $request= new Request();
        // $admin=$request->getBody();

        // 'inputadminname':$('#inputadminname').val(),
        //     'inputloginname':$('#inputloginname').val(),
        //     'inputPassword'
        // $name=$this->input->post('name');
        
        //add security trim input-todo
        //hash password for security
        $inputcompanynameedit=$this->input->post('inputcompanynameedit');
        $inputfullnameedit=$this->input->post('inputfullnameedit');
        $inputteledit=$this->input->post('inputteledit');
        // $inputadminname=$this->input->post('inputadminnameedit');
        $inputloginname=$this->input->post('inputloginnameedit');
        $inputPassword=$this->input->post('inputPasswordedit');
        $id=$this->input->post('inputadminid');

        $data= array(

            'inputcompanynameedit'=>$inputcompanynameedit,
            'inputfullnameedit'=>$inputfullnameedit,
            'inputteledit'=>$inputteledit,
            // 'inputadminname'=>$inputadminname,
            'inputloginname'=>$inputloginname,
            'inputPassword'=>$inputPassword,
            'id'=>$id
        );

        $code="";
        // if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
        //     $code="FAILURE";
        //     $msg="已有管理員姓名".$inputadminname;
        // }
        // else
        if($this->frontenduser_model->login_existsbyid(strtolower($inputloginname),$id)==false){
            $code="FAILURE";
            $msg="已有登入名稱".$inputloginname;
        }
        else{
            $result=$this->frontenduser_model->updateAdmin($data);
            if($result==false){
                $code="FAILURE";
                $msg="記錄不能更改";     
            }
            else{
                $code="SUCCESS";
                $msg="記錄成功更改";    
            }

            
        }

         
        

         header('Content-Type: application/json');
        // $code="SUCCESS";
        // $code="FAILURE";
        // $msg="已有相關記錄";
        // $myArray = ['id'=>12, 'name'=>'Topi'];
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
        echo json_encode($myArray);
        // echo "posted";
    
    }

    public function delete(){
        $inputdeletecheckboxvalue=$this->input->post('inputdeletecheckboxvalue');
        $result=true;
        foreach ($inputdeletecheckboxvalue as $key => $values){
            if($this->frontenduser_model->deleteAdmin($values)==false){
                $result=false; 
            }
           
            
        }//foreach ($inputdeletecheckboxvalue as $key => $values){
    
        if($result==true){
            $code="SUCCESS";
            $msg="相關記錄成功删徐";
        }
        else{
            $code="FAILURE";
            $msg="相關記錄不能删徐";
        }
       
        // $msg=$inputdeletecheckboxvalue;
        header('Content-Type: application/json');
        $myArray = ['recordstatus'=>$code, 'msg'=>$msg];
        echo json_encode($myArray);
    }
    public function getadmindata(){
        $id=$this->input->post('user_id');
        $data=$this->frontenduser_model->getAdmindateByid($id);
        header('Content-Type: application/json');
        echo json_encode($data);
    }

}//class Admins {
