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

class System extends CI_Controller {

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

            
            if (!$this->session->userdata('logged_in'))
            { 
                redirect('/login');
            }
    
            
            $this->load->model('FrontendUser', 'frontenduser_model');
           

            $this->load->model('Folder', 'folder_model');
           
            $this->load->model('FrontRightModel', 'frontright_model');
            $this->load->model('FileModel', 'file_model');
            $this->load->model('LoginModel', 'login_model');
            $this->load->model('BackendUser', 'backenduser_model');
            $this->load->helper('form');
            
        }

        public function index()
	{

        $userid = $this->session->userdata('userid');




        // $user_data = $this->session->userdata('USER_ID');
        // echo "adminpage";
        // if(empty($userid)){
        //     // $this->load->view('test1');
        //     redirect('/login', 'refresh');
        // }
        if($this->login_model->checking()==false){
            // redirect('/login', 'refresh');
            $this->load->view('login');
        }
        $this->load->helper('url');
        //testing
        // $currentURL = current_url(); //for simple URL
        // $params = $_SERVER['QUERY_STRING']; //for parameters
        // $fullURL = $currentURL . '?' . $params; //full URL with parameter
        //end testing


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

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

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

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

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

        // $this->load->view('welcome_message');
        $data = array(
                'title' => 'My Title',
                'heading' => 'My Heading',
                'message' => 'My Message'
            );        
        $data['total_rows']=$config["total_rows"];
        $data['page']=$page;
        $data['perpage']=$perpage;
        $data['num_links'] = $config["num_links"];
        $data['record']=$this->frontenduser_model->getAdmindata($perpage, $page);
        $data['base_url'] =$config["base_url"];

        //set user data
        $data['userid']=$this->session->userdata('userid');
        $data['fullName']=$this->session->userdata('fullName');
        $data['usertype']=$this->session->userdata('usertype');
        $data['sample1']='123';
        $data['heading'] = "Heading1";

        //end set user data


        
        //get directory data
         $data['directoryList']=$this->folder_model->getfolderdata(50000, 0);
        $count=1;
         foreach($data['directoryList'] as $row){
            $data['level1'][$count]=$this->folder_model->getlevel1data($row->id);
            $data['level1Record'][$count]=$this->folder_model->getflodercount($row->id);
            $countLevel2=1;
            foreach($data['level1'][$count] as $secondrow){
                $data['level2'][$count][$countLevel2]=$this->folder_model->getlevel2data($secondrow->id);
                $countLevel3=1;
                foreach($data['level2'][$count][$countLevel2] as $thirdrow){
                    $data['level3'][$count][$countLevel2][$countLevel3]=$this->folder_model->getlevel3data($thirdrow->id);
                    $countLevel3=$countLevel3+1;
                }
                $countLevel2=$countLevel2+1;
            }
            $count++;

        }



        //end get directory data 

        //get user data
        if($data['usertype']=="frontenduser"){
            $data['record']=$this->frontenduser_model->getAdmindateByid($userid);
        }
        if($data['usertype']=="backenduser"){
            $data['record']=$this->backenduser_model->getAdmindateByid($userid);
        }

        //end get user data


        //get Front User Right
        foreach($data['record'] as $row){
            $data['frontuser'][$row->id]['level0']=$this->frontright_model->getLevel0Right($row->id);
            $data['frontuser'][$row->id]['level1']=$this->frontright_model->getLevel1Right($row->id);
            $data['frontuser'][$row->id]['level2']=$this->frontright_model->getLevel2Right($row->id);
            $data['frontuser'][$row->id]['level3']=$this->frontright_model->getLevel3Right($row->id);
            $data['frontuser'][$row->id]['level4']=$this->frontright_model->getLevel4Right($row->id);

            $data['frontuser'][$row->id]['level0NoRight']=$this->frontright_model->getLevel0NoRight($row->id);
            $data['frontuser'][$row->id]['level1NoRight']=$this->frontright_model->getLevel1NoRight($row->id);
            $data['frontuser'][$row->id]['level2NoRight']=$this->frontright_model->getLevel2NoRight($row->id);
            $data['frontuser'][$row->id]['level3NoRight']=$this->frontright_model->getLevel3NoRight($row->id);
            $data['frontuser'][$row->id]['level4NoRight']=$this->frontright_model->getLevel4NoRight($row->id);
        }//foreach($data['record'] as $row){
        //end get Front User Right

        //check folder locked or not
        //  $count=1;
        // foreach ($data['record'] as $row) {
        //     $countLevel2=1;
        //     foreach($data['directoryList'] as $row1){
        //         //level1 
        //         if(
        //                                 (in_array($row->id, $data['frontuser'][$row->id]['level0'])==true && in_array($row1->id, $data['frontuser'][$row->id]['level1NoRight'])==false)

        //                                 ||

        //                                 (in_array($row1->id, $data['frontuser'][$row->id]['level1'])==true && in_array($row1->id, $data['frontuser'][$row->id]['level1NoRight'])==false)

        //         ){
        //             $data['frontlock'][$row->id][$row1->id]['level1lock']=false;

        //        }
        //        else{
        //            $data['frontlock'][$row->id][$row1->id]['level1lock']=true;     
        //        }    
        //        foreach($data['level1'][$count] as $row2){
        //             //level2
        //                if(
        //                                 (in_array($row1->id, $data['frontuser'][$row->id]['level1'])==true && in_array($row2->id, $data['frontuser'][$row->id]['level2NoRight'])==false)

        //                                 ||

        //                                 (in_array($row2->id, $data['frontuser'][$row->id]['level2'])==true && in_array($row2->id, $data['frontuser'][$row->id]['level2NoRight'])==false)

        //                 ){
        //                   $data['level2lock'][$row->id][$row1->id][$row2->id]=false;   

        //                }
        //                else{
        //                  $data['level2lock'][$row->id][$row1->id][$row2->id]=true; 
        //                }
        //             //end level2
        //             $countLevel2=1;
        //             foreach ($data['level2'][$count][$countLevel2] as $row3) {
        //                 //level3
        //                     if(
        //                                 (in_array($row2->id, $data['frontuser'][$row->id]['level2'])==true && in_array($row3->id, $data['frontuser'][$row->id]['level3NoRight'])==false)

        //                                 ||

        //                                 (in_array($row3->id, $data['frontuser'][$row->id]['level3'])==true && in_array($row3->id, $data['frontuser'][$row->id]['level3NoRight'])==false)

        //                 ){
        //                   $data['level3lock'][$row->id][$row1->id][$row2->id][$row3->id]=false;   

        //                }
        //                else{
        //                  $data['level3lock'][$row->id][$row1->id][$row2->id][$row3->id]=true; 
        //                }
        //                 //end level3 
        //                 $countLevel3=1;
        //                 foreach( $data['level3'][$count][$countLevel2][$countLevel3] as $row4){
                            
        //                 } 
        //                 $countLevel3=$countLevel3+1;    
        //             }//foreach ($data['level2'][$count][$countLevel2] as $row3) {
        //             $countLevel2=$countLevel2+1;   
        //        }//foreach($data['level1'] as $row2){ 
        //        //end level1 
                
        //         $count++;
        //     }//foreach($directoryList as $row1)
           
        // }//foreach ($data['record'] as $row) {       
        foreach ($data['record'] as $row) {        
        $count=1;
         foreach($data['directoryList'] as $row1){
            //level1 
                if(
                                        (in_array($row->id, $data['frontuser'][$row->id]['level0'])==true && in_array($row1->id, $data['frontuser'][$row->id]['level1NoRight'])==false)

                                        ||

                                        (in_array($row1->id, $data['frontuser'][$row->id]['level1'])==true && in_array($row1->id, $data['frontuser'][$row->id]['level1NoRight'])==false)

                ){
                    $data['frontlock'][$row->id][$row1->id]['level1lock']=false;

               }
               else{
                   $data['frontlock'][$row->id][$row1->id]['level1lock']=true;     
               }      
            //end level1

            $countLevel2=1;
            foreach($data['level1'][$count] as $row2){
                 //level2
                       if(
                                        (in_array($row1->id, $data['frontuser'][$row->id]['level1'])==true && in_array($row2->id, $data['frontuser'][$row->id]['level2NoRight'])==false)

                                        ||

                                        (in_array($row2->id, $data['frontuser'][$row->id]['level2'])==true && in_array($row2->id, $data['frontuser'][$row->id]['level2NoRight'])==false)

                        ){
                          $data['level2lock'][$row->id][$row1->id][$row2->id]=false;   

                       }
                       else{
                         $data['level2lock'][$row->id][$row1->id][$row2->id]=true; 
                       }
                //end level2
                $countLevel3=1;
                foreach($data['level2'][$count][$countLevel2] as $row3){
                    //level3
                            if(
                                        (in_array($row2->id, $data['frontuser'][$row->id]['level2'])==true && in_array($row3->id, $data['frontuser'][$row->id]['level3NoRight'])==false)

                                        ||

                                        (in_array($row3->id, $data['frontuser'][$row->id]['level3'])==true && in_array($row3->id, $data['frontuser'][$row->id]['level3NoRight'])==false)

                        ){
                          $data['level3lock'][$row->id][$row1->id][$row2->id][$row3->id]=false;   

                       }
                       else{
                         $data['level3lock'][$row->id][$row1->id][$row2->id][$row3->id]=true; 
                       }
                        //end level3 
                    foreach($data['level3'][$count][$countLevel2][$countLevel3] as $row4){
                        //level4
                            if(
                                        (in_array($row3->id, $data['frontuser'][$row->id]['level3'])==true && in_array($row4->id, $data['frontuser'][$row->id]['level4NoRight'])==false)

                                        ||

                                        (in_array($row4->id, $data['frontuser'][$row->id]['level4'])==true && in_array($row4->id, $data['frontuser'][$row->id]['level4NoRight'])==false)

                        ){
                          $data['level4lock'][$row->id][$row1->id][$row2->id][$row3->id][$row4->id]=false;   

                       }
                       else{
                         $data['level4lock'][$row->id][$row1->id][$row2->id][$row3->id][$row4->id]=true; 
                       }
                        //end level4 
                    }    
                    $countLevel3=$countLevel3+1;
                }
                $countLevel2=$countLevel2+1;
            }
            $count++;

        }   

        }//foreach ($data['record'] as $row) {        


        //end check folder locked or not


        //current month time lock
            date_default_timezone_set('Asia/Hong_Kong');
            $now = new \DateTime('now');
            $month = $now->format('m');
            $day = $now->format('d');

            $lockDirLevel3=array();
            array_push($lockDirLevel3,'Jan');
            array_push($lockDirLevel3,'Feb');
            array_push($lockDirLevel3,'Mar');
            array_push($lockDirLevel3,'Apr');
            array_push($lockDirLevel3,'May');
            array_push($lockDirLevel3,'Jun');
            array_push($lockDirLevel3,'Jul');
            array_push($lockDirLevel3,'Aug');
            array_push($lockDirLevel3,'Sep');
            array_push($lockDirLevel3,'Oct');
            array_push($lockDirLevel3,'Nov');
            array_push($lockDirLevel3,'Dec');
            $data['lockDirLevel3']=$lockDirLevel3;

            $data['month']=$month;
            $data['day']=$day;
            $data['currentavaMonth']="";
            $data['nextavaMonth']="";
            if($month=='01'){
                $data['currentavaMonth']='Jan';
                if($day>=20){
                    $data['nextavaMonth']='Feb';
                }
            }
            if($month=='02'){
                $data['currentavaMonth']='Feb';
                if($day>=20){
                    $data['nextavaMonth']='Mar';
                }
            }
            if($month=='03'){
                $data['currentavaMonth']='Mar';
                if($day>=20){
                    $data['nextavaMonth']='Apr';
                }
            }
            if($month=='04'){
                $data['currentavaMonth']='Apr';
                if($day>=20){
                    $data['nextavaMonth']='May';
                }
            }
            if($month=='05'){
                $data['currentavaMonth']='May';
                if($day>=20){
                    $data['nextavaMonth']='Jun';
                }
            }
            if($month=='06'){
                $data['currentavaMonth']='Jun';
                if($day>=20){
                    $data['nextavaMonth']='Jul';
                }
            }
            if($month=='07'){
                $data['currentavaMonth']='Jul';
                if($day>=20){
                    $data['nextavaMonth']='Aug';
                }
            }
            if($month=='08'){
                $data['currentavaMonth']='Aug';
                if($day>=20){
                    $data['nextavaMonth']='Sep';
                }
            }
            if($month=='09'){
                $data['currentavaMonth']='Sep';
                if($day>=20){
                    $data['nextavaMonth']='Oct';
                }
            }
            if($month=='10'){
                $data['currentavaMonth']='Oct';
                if($day>=20){
                    $data['nextavaMonth']='Nov';
                }
            }
            if($month=='11'){
                $data['currentavaMonth']='Nov';
                if($day>=20){
                    $data['nextavaMonth']='Dec';
                }
            }
            if($month=='12'){
                $data['currentavaMonth']='Dec';
                if($day>=20){
                    $data['nextavaMonth']='Jan';
                }
            }
        //end current month time lock


        //load file directory
        $this->load->helper('url');
        $level1data=0;
        $level2data=NULL;
        $level3data=NULL;
        $level4data=NULL;
        $currentLevel=0;
        if(!empty($this->uri->segment(3))){
            $level1data = $this->uri->segment(3);  
            $currentLevel=1;
        }
        if(!empty($this->uri->segment(4))){
            $level2data = $this->uri->segment(4);  
            $currentLevel=2;
        }
        if(!empty($this->uri->segment(5))){
            $level3data = $this->uri->segment(5);  
            $currentLevel=3;
        }
        if(!empty($this->uri->segment(6))){
            $level4data = $this->uri->segment(6);  
            $currentLevel=4;
        }


        $data['level1data'] =$level1data;
        $data['level2data'] =$level2data;  
        $data['level3data'] =$level3data;  
        $data['level4data'] =$level4data;  
        $data['currentLevel']=$currentLevel;

        //level name
        $data['level1name'] =$this->folder_model->getFolderName($level1data);
        $data['level2name'] =$this->folder_model->getFolderName($level2data); 
        $data['level3name'] =$this->folder_model->getFolderName($level3data); 
        $data['level4name'] =$this->folder_model->getFolderName($level4data); 

        //end level name


        $data['directlist']="";
        if($currentLevel==0){
            $data['showdirectoryList']=$this->folder_model->getfolderdata(50000, 0);
             $data['directlist']="";
        }
        if($currentLevel==1){

            //check if folder is locked
            if($data['frontlock'][$data['userid']][$level1data]['level1lock']==true && $data['usertype']=="frontenduser"){
                $this->load->view('/withoutright',$data);
            }
            //end check if folder is locked
            $data['showdirectoryList']=$this->folder_model->getlevel1data($level1data);
             $data['directlist']=$level1data."/";
            $data['showfileList']=$this->file_model->getlevel1file($level1data);
        }
        if($currentLevel==2){
            //check if older is locked
            if($data['level2lock'][$data['userid']][$level1data][$level2data]==true && $data['usertype']=="frontenduser"){
                $this->load->view('/withoutright',$data);
            }
            //check if older is locked
            $data['showdirectoryList']=$this->folder_model->getlevel2data($level2data);
            $data['directlist']=$level1data."/".$level2data."/";
            $data['showfileList']=$this->file_model->getlevel2file($level2data);
        }
        if($currentLevel==3){
            //check  if older is locked
            if($data['level3lock'][$data['userid']][$level1data][$level2data][$level3data]==true && $data['usertype']=="frontenduser"){
                $this->load->view('/withoutright',$data);
            }
            //month lock
             //level3 month lock
                              if(in_array($data['level3name'],$lockDirLevel3) && $data['usertype']=="frontenduser"){
                                //check month lock
                                if($data['level3lock'][$data['userid']][$level1data][$level2data][$level3data]==false){
                                  if($data['level3name'] !=$data['currentavaMonth'] && $data['level3name'] !=$data['nextavaMonth']){
                                   $this->load->view('/withoutright',$data);
                                  }
                                }
                                //end check month lock
                              }//if(in_array($row->name,$lockDirLevel3)){
                              //end level3 month lock
            //end month lock
            //end check  if older is locked
            $data['showdirectoryList']=$this->folder_model->getlevel3data($level3data);
            $data['directlist']=$level1data."/".$level2data."/".$level3data."/";
            $data['showfileList']=$this->file_model->getlevel3file($level3data);
        }
        if($currentLevel==4){
            //check folder is locked
            if($data['level4lock'][$data['userid']][$level1data][$level2data][$level3data][$level4data]==true && $data['usertype']=="frontenduser"){
                $this->load->view('/withoutright',$data);
            }
            //month lock
             //level3 month lock
                              if(in_array($data['level3name'],$lockDirLevel3) && $data['usertype']=="frontenduser"){
                                //check month lock
                                if($data['level4lock'][$data['userid']][$level1data][$level2data][$level3data][$level4data]==false){
                                  if($data['level3name'] !=$data['currentavaMonth'] && $data['level3name'] !=$data['nextavaMonth']){
                                   $this->load->view('/withoutright',$data);
                                  }
                                }
                                //end check month lock
                              }//if(in_array($row->name,$lockDirLevel3)){
                              //end level3 month lock
            //end month lock

            //end check folder is locked
            $data['showdirectoryList']=$this->folder_model->getlevel4data($level4data);
            $data['directlist']=$level1data."/".$level2data."/".$level3data."/".$level4data."/";
            $data['showfileList']=$this->file_model->getlevel4file($level4data);
        }
        
        //end load file directory

        


        //load default setting 
        $data['view_info'] = $this->get_default_setting();
        //end load default setting


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

        public function dir()
        {

        }

        

         public function get_default_setting()
        {
        $this->db->where('setting_user_id',$this->session->userdata('userid'));
        return $this->db->get('default_setting')->row_array();
        }
}