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

class BackendUser extends CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function getBannerAll()
    {
        
        $this->db->from('adminUsers');
        // $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');
        $query = $this->db->get('adminUsers');
        return $query->num_rows();
    }   


    function getAdmindata($perpage,$page){
        $this->db->select('id,login, fullName, update_time');
        $offset=$page* $perpage;
        $this->db->limit($perpage, $offset);
        $query = $this->db->get('adminUsers');
        return $query->result();
    }

    function getAdmindateByid($id){
        $this->db->select('id,login, fullName, update_time');
        $this->db->where('id', $id);
        $query = $this->db->get('adminUsers');
        return $query->result();
    }


    function insertAdmin($data){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,
        $data = array(
            'login' => $data['inputloginname'] ,
            'fullName' => $data['inputadminname'] ,
            'pwd' => $data['inputPassword'] ,
         );
         $this->db->set('update_time', 'NOW()', FALSE);
         $query=$this->db->insert('adminUsers', $data);

         //update a secret key for new added admin user
         $id = $this->db->insert_id();
         $this->updatesecrete($id);
         //end update a secret key for new added admin user


         return $query;
    }
    function updateAdmin($input){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,
        
        if(empty($input['inputPassword'])){
            $data = array(
                'login' => $input['inputloginname'] ,
                'fullName' => $input['inputadminname'] 
                
             );
        }
        else{
            $data = array(
                'login' => $input['inputloginname'] ,
                'fullName' => $input['inputadminname'] ,
                'pwd' => $input['inputPassword'] 
             );
        }
        
         $this->db->set('update_time', 'NOW()', FALSE);

         $this->db->where('id', $input['id']);
         $query=$this->db->update('adminUsers', $data);
         return $query;
    }



    function deleteAdmin($id){
        $this->db->where('id', $id);
        $query=$this->db->delete('adminUsers');
        return $query;
    }

    function login_exists($loginname){
        // $this->db->from('adminUsers');
        $this->db->where('LOWER(login)', $loginname);
        $query = $this->db->get('adminUsers');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function login_existsbyid($loginname,$id){
        // $this->db->from('adminUsers');
        $this->db->where('LOWER(login)', $loginname);
        $this->db->where('id!=', $id);
        $query = $this->db->get('adminUsers');
        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('adminUsers');
        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('adminUsers');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }


    //process login
    function check(){
        // $userName =  $this->input->post('userName');
        // $password =  md5(trim($this->input->post('password')));

        // $this->db->where('login',$userName);
        // $this->db->where('pwd',$password);
        // // $this->db->where('user_active','yes');
        // return $this->db->get('adminUsers')->row_array();

        //new admin check
        // $userName =  $this->input->post('userName');
        // $password =  sha1(trim($this->input->post('password')));
        // $this->db->where('username',$userName);
        // $this->db->where('password',$password);    
        // return $this->db->get('mas_admin')->row_array();

        $userName =  $this->input->post('userName');
        $password =  sha1(trim($this->input->post('password')));
        $this->db->where('username',$userName);
        $this->db->where('password',$password);    
        $this->db->where('deleted',0); //added condition at 2020-08-25
        return $this->db->get('bemployee')->row_array();


        //end new admin check
        //first check mas_admin table
        //second check mas_rbac_role_user


    }


    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('adminUsers');
        $query = $this->db->get();
        return $query;
    }//function processLogin(){

    function updatesecrete($id){

         $data = array(
                'secret' => md5(uniqid()),
             );
        
         $this->db->where('id', $id);
         $query=$this->db->update('bemployee', $data);
         return $query;
    }


    function getsecret($id){


        
         $this->db->where('id', $id);
         $query=$this->db->get('bemployee');
         $row=$query->row();

         return  $row->secret;;
    }
    //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;
        
    }
}