<?php  

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



class Rightend extends CI_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('Folder', 'folder_model');
        $this->load->model('FrontRightModel', 'frontright_model');    
    }
    

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

    //floder right
    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
        $action=$this->input->post('action');
        $personid=$this->input->post('personid');
        $currentlevel=$this->input->post('currentlevel');
        // $inputadminname=$this->input->post('inputadminnameedit');
        $level1id=$this->input->post('level1id');
        $level2id=$this->input->post('level2id');
        $level3id=$this->input->post('level3id');
        $level4id=$this->input->post('level4id');

        // echo "action";
        // echo $action;
        // echo "personid";
        // echo $personid;
        // echo "currentlevel";
        // echo $currentlevel;
        // echo "level1id";
        // echo $level1id;    

        if($action=="add"){


        //insert first level id
             $data= array(

            'personid'=>$personid,
            'currentlevel'=>$currentlevel,
            'level1id'=>$level1id,
            // 'inputadminname'=>$inputadminname,
            'level2id'=>$level2id,
            'level3id'=>$level3id,
            'level4id'=>$level4id
        );
         //for own level record, it will skip level 0
         //other record will be save
         if($currentlevel!=0){
           $result=$this->frontright_model->updateFrontRight($data);
         }       
         

         //get subdirectory data
         if($currentlevel==0){
            $data['record']=$this->folder_model->getallfolderdata();
            foreach($data['record'] as $row){

                //change NULL to id
                $replaceid=false;
                $level1=$row->level1;
                $level2=$row->level2;
                $level3=$row->level3;
                $level4=$row->level4;
                
                if($replaceid==false && $level1==0){
                     $level1=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level1==NULL){
                     $level1=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level2==NULL){
                     $level2=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level3==NULL){
                     $level3=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level4==NULL){
                     $level4=$row->id;
                     $replaceid=true;
                }
                //end change NULL to id

            //      $data= array(

            //     'personid'=>$personid,
            //     'currentlevel'=>$currentlevel,
            //     'level1id'=>$row->level1,
            //     // 'inputadminname'=>$inputadminname,
            //     'level2id'=>$row->level2,
            //     'level3id'=>$row->level3,
            //     'level4id'=>$row->level4
            // );
            //     $result=$this->frontright_model->updateFrontRight($data);

                 $data= array(

                'personid'=>$personid,
                'currentlevel'=>$currentlevel,
                'level1id'=>$level1,
                // 'inputadminname'=>$inputadminname,
                'level2id'=>$level2,
                'level3id'=>$level3,
                'level4id'=>$level4
            );
                $result=$this->frontright_model->updateFrontRight($data);


            }//foreach($data['record'] as $row){
         }//if($currentlevel==1){

        if($currentlevel==1){
            $data['record']=$this->folder_model->getalllevel1datainclude($level1id);
            foreach($data['record'] as $row){

                 //change NULL to id
                $replaceid=false;
                $level1=$row->level1;
                $level2=$row->level2;
                $level3=$row->level3;
                $level4=$row->level4;
                
                
                if($replaceid==false && $level1==NULL){
                     $level1=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level2==NULL){
                     $level2=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level3==NULL){
                     $level3=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level4==NULL){
                     $level4=$row->id;
                     $replaceid=true;
                }
                //end change NULL to id

                 $data= array(

                'personid'=>$personid,
                'currentlevel'=>$currentlevel,
                'level1id'=>$level1,
                // 'inputadminname'=>$inputadminname,
                'level2id'=>$level2,
                'level3id'=>$level3,
                'level4id'=>$level4
            );
                $result=$this->frontright_model->updateFrontRight($data);
            }//foreach($data['record'] as $row){
         }//if($currentlevel==2){


        if($currentlevel==2){
            $data['record']=$this->folder_model->getalllevel2datainclude($level2id);
            foreach($data['record'] as $row){

                //change NULL to id
                $replaceid=false;
                $level1=$row->level1;
                $level2=$row->level2;
                $level3=$row->level3;
                $level4=$row->level4;
                
                
                if($replaceid==false && $level1==NULL){
                     $level1=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level2==NULL){
                     $level2=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level3==NULL){
                     $level3=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level4==NULL){
                     $level4=$row->id;
                     $replaceid=true;
                }
                //end change NULL to id

                 $data= array(

                'personid'=>$personid,
                'currentlevel'=>$currentlevel,
                'level1id'=>$level1,
                // 'inputadminname'=>$inputadminname,
                'level2id'=>$level2,
                'level3id'=>$level3,
                'level4id'=>$level4
            );
                $result=$this->frontright_model->updateFrontRight($data);
            }//foreach($data['record'] as $row){
         }//if($currentlevel==2){
        

         if($currentlevel==3){
            // echo "getalllevel3datainclude";
            $data['record']=$this->folder_model->getalllevel3datainclude($level3id);
            foreach($data['record'] as $row){
                // echo "personid";
                $replaceid=false;
                $level1=$row->level1;
                $level2=$row->level2;
                $level3=$row->level3;
                $level4=$row->level4;
                
                
                if($replaceid==false && $level1==NULL){
                     $level1=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level2==NULL){
                     $level2=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level3==NULL){
                     $level3=$row->id;
                     $replaceid=true;
                }
                if($replaceid==false && $level4==NULL){
                     $level4=$row->id;
                     $replaceid=true;
                }
                

                 $data= array(

                'personid'=>$personid,
                'currentlevel'=>$currentlevel,
                'level1id'=>$level1,
                // 'inputadminname'=>$inputadminname,
                'level2id'=>$level2,
                'level3id'=>$level3,
                'level4id'=>$level4
            );
                $result=$this->frontright_model->updateFrontRight($data);
            }//foreach($data['record'] as $row){
         }//if($currentlevel==3){


         if($currentlevel==4){
            // echo "getalllevel3datainclude";

            //not use getalllevel4datainclude
            // $data['record']=$this->folder_model->getalllevel4datainclude($level4id);
            // foreach($data['record'] as $row){

            //      //change NULL to id
            //     $replaceid=false;
            //     $level1=$row->level1;
            //     $level2=$row->level2;
            //     $level3=$row->level3;
            //     $level4=$row->level4;
                
                
            //     if($replaceid==false && $level1==NULL){
            //          $level1=$row->id;
            //          $replaceid=true;
            //     }
            //     if($replaceid==false && $level2==NULL){
            //          $level2=$row->id;
            //          $replaceid=true;
            //     }
            //     if($replaceid==false && $level3==NULL){
            //          $level3=$row->id;
            //          $replaceid=true;
            //     }
            //     if($replaceid==false && $level4==NULL){
            //          $level4=$row->id;
            //          $replaceid=true;
            //     }
            //     //end change NULL to id


            //     // echo "personid";
            //      $data= array(

            //     'personid'=>$personid,
            //     'currentlevel'=>$currentlevel,
            //     'level1id'=>$level1,
            //     // 'inputadminname'=>$inputadminname,
            //     'level2id'=>$level2,
            //     'level3id'=>$level3,
            //     'level4id'=>$level4
            // );
            //     $result=$this->frontright_model->updateFrontRight($data);
            // }//foreach($data['record'] as $row){
         }//if($currentlevel==4){   


        }//if($action=="add"){
        
        if($action=="remove"){    
             $data= array(

            'personid'=>$personid,
            'currentlevel'=>$currentlevel,
            'level1id'=>$level1id,
            // 'inputadminname'=>$inputadminname,
            'level2id'=>$level2id,
            'level3id'=>$level3id,
            'level4id'=>$level4id
        );

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

            // $data['record']=$this->folder_model->getfolderdata($perpage, $page);
        // $data['base_url'] =$config["base_url"];
     
         //end get subdirectory data


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

        // $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 test(){
        $code="SUCCESS";
        $msg="記錄成功更改";  
    }
    //end floder right



    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
            $inputflodername=$this->input->post('inputflodername');


            $data= array(
                'inputflodername'=>$inputflodername,

            );

            $code="";
            // if($this->frontenduser_model->admin_exists($inputadminname)==false){
            //     $code="FAILURE";
            //     $msg="已有管理員姓名".$inputadminname;
            // }
            // else
            if($this->folder_model->folder_exists(strtolower($inputflodername))==false){
                $code="FAILURE";
                $msg="已有分類名稱".$inputflodername;
            }
            else{
                $result=$this->folder_model->insertFolder($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 addmore(){
        $inputflodernamelevel=$this->input->post('inputflodernamelevel');
        $addlevelid=$this->input->post('addlevelid');
        $addlevel=$this->input->post('addlevel');
        $inputboxvalue=$this->input->post('inputboxvalue');
        $inputcheckboxvalue=$this->input->post('inputcheckboxvalue');
        

        $msg="";
        foreach ($inputboxvalue as $key => $values){
            
        $data= array(
            'inputflodername'=>$values,
            'addlevelid'=>$addlevelid,
            'addlevel'=>$addlevel,
        );

            if($addlevel==1){
                if($this->folder_model->folder_levelOneexists(strtolower($values))==false){
                    $code="FAILURE";
                    $msg=$msg."已有分類名稱".$values."<br>";
                }
                else{
                    $result=$this->folder_model->insertLevelOneFolder($values,$addlevel);
                    if($result==false){
                        $code="FAILURE";
                        $msg=$msg."分類不能加入"."<br>";    
                    }
                    else{
                        $code="SUCCESS";
                        $msg=$msg."分類成功加入"."<br>";    
                    }
                }   
            }//if($addlevel==1){

            if($addlevel==2){ 
                if($this->folder_model->folder_levelTwoexists(strtolower($values),$addlevel,$addlevelid)==false){
                    $code="FAILURE";
                    $msg=$msg."已有分類名稱".$values."<br>";
                }
                else{
                    $result=$this->folder_model->insertLevelTwoFolder($values,$addlevel,$addlevelid);
                    if($result==false){
                        $code="FAILURE";
                        $msg=$msg."分類不能加入"."<br>";    
                    }
                    else{
                        $code="SUCCESS";
                        $msg=$msg."分類成功加入"."<br>";    
                    }
                }
            }//if($addlevel==2){   

            if($addlevel==3){
                foreach ($inputcheckboxvalue as $secondkey => $secondvalues){
                    if($this->folder_model->folder_levelThreeexists(strtolower($values),$addlevel,$addlevelid,$secondvalues)==false){
                        $code="FAILURE";
                        $msg=$msg."已有分類名稱".$values."<br>";
                    }
                    else{
                        $result=$this->folder_model->insertLevelThreeFolder($values,$addlevel,$addlevelid,$secondvalues);
                        if($result==false){
                            $code="FAILURE";
                            $msg=$msg."分類不能加入"."<br>";    
                        }
                        else{
                            $code="SUCCESS";
                            $msg=$msg."分類成功加入"."<br>";    
                        }
                    }

                }//foreach ($inputcheckboxvalue as $secondkey => $secondvalues){
            }//if($addlevel==3){    
                
            if($addlevel==4){
                foreach ($inputcheckboxvalue as $thirdkey => $thirdvalues){
                    if($this->folder_model->folder_levelFourexists(strtolower($values),$addlevel,$addlevelid,$thirdvalues)==false){
                        $code="FAILURE";
                        $msg=$msg."已有分類名稱".$values."<br>";
                    }
                    elseif($this->folder_model->checklevelthreeid($thirdvalues)){
                        $code="FAILURE";
                        $msg=$msg."所選不是第三分類".$values."<br>";
                    }
                    else{
                        $result=$this->folder_model->insertLevelFourFolder($values,$addlevel,$addlevelid,$thirdvalues);
                        if($result==false){
                            $code="FAILURE";
                            $msg=$msg."分類不能加入"."<br>";    
                        }
                        else{
                            $code="SUCCESS";
                            $msg=$msg."分類成功加入"."<br>";    
                        }
                    }

                }//foreach ($inputcheckboxvalue as $secondkey => $secondvalues){
            }//if($addlevel==4){
            if($addlevel==24){
                //add 12months and 5 weeks
                foreach ($inputcheckboxvalue as $secondkey => $secondvalues){
                    // if($this->folder_model->folder_levelTwoMonthWeekexists(strtolower($values),$addlevel,$addlevelid,$secondvalues)==false){
                    //     $code="FAILURE";
                    //     $msg=$msg."已有分類名稱".$values."<br>";
                    // }
                    // else
                    if($this->folder_model->checkleveltwoid($secondvalues)){
                        $code="FAILURE";
                        $msg=$msg."所選不是第二分類".$values."<br>";
                    }
                    else{
                        $result=$this->folder_model->insertLevelMonthWeekFolder($values,$addlevel,$addlevelid,$secondvalues);
                        if($result==false){
                            $code="FAILURE";
                            $msg=$msg."分類不能加入"."<br>";    
                        }
                        else{
                            $code="SUCCESS";
                            $msg=$msg."分類成功加入"."<br>";    
                        }
                    }

                }//foreach ($inputcheckboxvalue as $secondkey => $secondvalues){
                //end add 12months ans 5 weeks
            }//if($addlevel==24){

        // $code="";
        // // if($this->frontenduser_model->admin_exists($inputadminname)==false){
        // //     $code="FAILURE";
        // //     $msg="已有管理員姓名".$inputadminname;
        // // }
        // // else
        // if($this->folder_model->folder_exists(strtolower($inputflodernamelevel))==false){
        //     $code="FAILURE";
        //     $msg=$msg."已有分類名稱".$inputflodernamelevel."<br>";
        // }
        // else{
        //     $result=$this->folder_model->insertLevelFolder($data);
        //     if($result==false){
        //         $code="FAILURE";
        //         $msg=$msg."分類不能加入"."<br>";    
        //     }
        //     else{
        //         $code="SUCCESS";
        //         $msg=$msg."分類成功加入"."<br>";    
        //     }
        // }

        }// foreach ($inputflodernamelevel as $key => $values){
        

         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 {
