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

class Pdffile 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->model('Section', 'section_model');
            $this->menu = $this->section_model->buildTopMenuItem();
            $this->breadcrumbs = [];
            $this->load->model('ContactDetails', 'contactdetails_model');
            $this->contactus = $this->contactdetails_model->getContactus();

            $this->load->model('BackendUser', 'backenduser_model');
            $this->load->library(array('form_validation','session'));
            $this->load->helper(array('url','html','form'));
            $this->load->model('FileModel', 'file_model');
        }

        public function successpage(){
                // $this->load->view('successpage');
                // echo "1";
                redirect('/adminmain', 'refresh');
        }
               
        function logout(){
                $this->session->sess_destroy();
                redirect('/test');
        }


        public function index()
	{


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

        
        if(empty($userid)){
            // $this->load->view('test1');
            redirect('/login', 'refresh');
        }
        // $this->load->helper('url');
        //$this->load->view('welcome_message');

        $this->load->helper('url');
        $level1data="";
        if(!empty($this->uri->segment(3))){
            $level1data = $this->uri->segment(3);  
        }
        // echo $level1data;

        //May be hide the file data as below.
        
        $data['showfileList']=$this->file_model->getFiledataByid($level1data);
        
        $data['fileid']= $level1data;
        // $this->load->view('/test2',$data);
        // $this->load->view('pdfview',$data);
        $this->load->view('pdfviewer',$data);
        // $this->load->view('viewer',$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 signin(){
                // echo "sigin";
                // $this->successpage();
                $userName= trim($this->input->post('userName'));
                $password= trim($this->input->post('password'));
                // echo $userName;
                // echo $password;
                $query = $this->backenduser_model->processLogin($userName,$password);
                // $query2 = $query->result();
                // echo "login";
                // echo  $query2[0]->login;
                // echo "num row";
                // echo $query->num_rows();
                // $this->form_validation->set_rules('userName', '名稱', 'required|callback_validateUser[' . $query->num_rows() . ']');
                // $this->form_validation->set_rules('password', '密碼', 'required');
                
                // $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
                // $this->form_validation->set_message('validateUser', '錯誤名稱或密碼');
                // $this->form_validation->set_message('required', '請輸入 %s');
                
                //echo query result
              
                //end echo query result

                
                if (empty($userName)|| empty($password) || ($query->num_rows()!=1)) {
                        $data['msg']="登入名稱或密碼錯誤";
                        $this->load->view('testview',$data);
                        
                        // echo "testview";
                }else{
                        if($query){
                                $query = $query->result();
                                $user = array(
                                'USER_ID' => $query[0]->id,
                                'USER_NAME' => $query[0]->login,
                                'FULL_NAME' => $query[0]->fullName
                                
                                );
                        
                        $this->session->set_userdata($user);
                        // redirect('index.php/successpage');
                                $this->successpage();
                                // echo "success";
                        }
                }

        }//public function signin(){
        public function test2()
	{
        //$this->load->view('welcome_message');
        $this->load->view('test1');
        // $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'
        //     ));
        }
}
