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

class FrontRightModel extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    //new front right model
    function updateFrontRight($dataoriginal){

            if(empty($dataoriginal['level1id'])){
                    $dataoriginal['level1id']=NULL;
            }
            if(empty($dataoriginal['level2id'])){
                    $dataoriginal['level2id']=NULL;
            }
            if(empty($dataoriginal['level3id'])){
                    $dataoriginal['level3id']=NULL;
            }
            if(empty($dataoriginal['level4id'])){
                    $dataoriginal['level4id']=NULL;
            }

             $data = array(
            'frontpersonid' => $dataoriginal['personid'] ,
            'level1' => $dataoriginal['level1id'] ,
            'level2' => $dataoriginal['level2id'] ,
            'level3' => $dataoriginal['level3id'] ,
            'level4' => $dataoriginal['level4id'] ,
            
         );
         $query="";
         if($this->checkfrontrightexit($dataoriginal)==true){
                // echo "true";
                $this->db->set('update_time', 'NOW()', FALSE);
                $query=$this->db->insert('tblFrontRight', $data);
         }
         else{ //else update

            $this->db->where('frontpersonid', $dataoriginal['personid']);
            if($dataoriginal['currentlevel']==0){
                //may have issue
                $this->db->where('level1', $dataoriginal['level1id']);
            }
            if($dataoriginal['currentlevel']==1){
                $this->db->where('level1', $dataoriginal['level1id']);
            }
            if($dataoriginal['currentlevel']==2){
                $this->db->where('level2', $dataoriginal['level2id']);
            }
            if($dataoriginal['currentlevel']==3){
                $this->db->where('level3', $dataoriginal['level3id']);
            }
            if($dataoriginal['currentlevel']==4){
                $this->db->where('level4', $dataoriginal['level4id']);
            }
        
         $data = array(
            'inheritAccess' => 1 ,
            
         );
        $query=$this->db->update('tblFrontRight', $data);

             // echo "false";
         //          $data = array(
         //    'frontpersonid' => $dataoriginal['personid'] ,
         //    'level1' => $dataoriginal['level1id'] ,
         //    'level2' => $dataoriginal['level2id'] ,
         //    'level3' => $dataoriginal['level3id'] ,
         //    'level4' => $dataoriginal['level4id'] ,
            
         // );
        
         //        $this->db->set('update_time', 'NOW()', FALSE);

         //        $this->db->where('frontpersonid', $dataoriginal['personid']);

         //        //get id
         //        $query=$this->db->update('tblFrontRight', $data);
         } //end else update
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $query=$this->db->insert('tblFrontRight', $data);
         return $query;

    } //function updateFrontRight($data){ 

    function removeFrontRight($dataoriginal){
         if(empty($dataoriginal['level1id'])){
                    $dataoriginal['level1id']=NULL;
            }
            if(empty($dataoriginal['level2id'])){
                    $dataoriginal['level2id']=NULL;
            }
            if(empty($dataoriginal['level3id'])){
                    $dataoriginal['level3id']=NULL;
            }
            if(empty($dataoriginal['level4id'])){
                    $dataoriginal['level4id']=NULL;
            }
            $this->db->where('frontpersonid', $dataoriginal['personid']);
            if($dataoriginal['currentlevel']==0){
                //may have issue
                $this->db->where('level1',$dataoriginal['level1id']);
            }
            if($dataoriginal['currentlevel']==1){
                $this->db->where('level1', $dataoriginal['level1id']);
            }
            if($dataoriginal['currentlevel']==2){
                $this->db->where('level2', $dataoriginal['level2id']);
            }
            if($dataoriginal['currentlevel']==3){
                $this->db->where('level3', $dataoriginal['level3id']);
            }
            if($dataoriginal['currentlevel']==4){
                $this->db->where('level4', $dataoriginal['level4id']);
            }
        
         $data = array(
            'inheritAccess' => 0 ,
            
         );
        $query=$this->db->update('tblFrontRight', $data);
        return $query;
    }//function removeFrontRight($data){

    function checkfrontrightexit($data){
            if(empty($data['level1id'])){
                    $data['level1id']=NULL;
            }
            if(empty($data['level2id'])){
                    $data['level2id']=NULL;
            }
            if(empty($data['level3id'])){
                    $data['level3id']=NULL;
            }
            if(empty($data['level4id'])){
                    $data['level4id']=NULL;
            }

        $this->db->where('frontpersonid', $data['personid']);
        $this->db->where('level1',$data['level1id']);
        $this->db->where('level2 ',$data['level2id']);
        $this->db->where('level3 ',$data['level3id']);
        $this->db->where('level4 ',$data['level4id']);
        $query = $this->db->get('tblFrontRight');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }

    function getLevel0Right($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',1);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result()  as $row)
        {
            array_push($rightArray,$row->level1);
        }
        return $rightArray;
    }//function getLevel0Right($id){

    function getLevel1Right($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',1);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2',NULL);
        $this->db->where('level3',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level1);
        }
        return $rightArray;
    }//function getLevel0Right($id){

    function getLevel2Right($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',1);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level2);
        }
        return $rightArray;
    }//function getLevel0Right($id){
    
    function getLevel3Right($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',1);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3!=',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level3);
        }
        return $rightArray;
    }//function getLevel0Right($id){

    function getLevel4Right($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',1);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3!=',NULL);
        $this->db->where('level4!=',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level4);
        }
        return $rightArray;
    }//function getLevel0Right($id){

    //getNotRight
      function getLevel0NoRight($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',0);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level1);
        }
        return $rightArray;
    }//function getLevel0NoRight($id){  

     function getLevel1NoRight($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',0);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2',NULL);
        $this->db->where('level3',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level1);
        }
        return $rightArray;
    }//function getLevel1NoRight($id){

    function getLevel2NoRight($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',0);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level2);
        }
        return $rightArray;
    }//function getLevel1NoRight($id){

    function getLevel3NoRight($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',0);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3!=',NULL);
        $this->db->where('level4',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level3);
        }
        return $rightArray;
    }//function getLevel3NoRight($id){

    function getLevel4NoRight($id){
        $rightArray=array();
        $this->db->where('frontpersonid',$id);
        $this->db->where('inheritAccess',0);
        $this->db->where('level1!=',NULL);
        $this->db->where('level2!=',NULL);
        $this->db->where('level3!=',NULL);
        $this->db->where('level4!=',NULL);
        $query = $this->db->get('tblFrontRight');
        foreach ($query->result() as $row)
        {
            array_push($rightArray,$row->level4);
        }
        return $rightArray;
    }//function getLevel4NoRight($id){

    //end getNotRight


    function count_all_folders($id){
        $this->db->select('id');
        $this->db->where('frontpersonid', $id);
        $this->db->where('inheritAccess', 1);
        $query = $this->db->get('tblFrontRight');
        return $query->num_rows();    
    }


    //end new front right model


    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 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 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 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->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;
        
    }
}