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

class EmployeeModel extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->model('ShopModel', 'shop_model');
        date_default_timezone_set('Asia/Hong_Kong');
    }

    //added employee
    function getEmployeeIncomeType() {
        $this->db->from('mas_employee_income_type');

        $query = $this->db->get();
        return $query->result();
    }
    function login_exists($loginname){
        // $this->db->from('adminUsers');
        $this->db->where('LOWER(username)', $loginname);
        $query = $this->db->get('mas_admin');
        if($query->num_rows() == 1){
            return false;
        } else {
            return true;
        }
    }
    function insertAdmin($inputdata){
        // 'inputadminname'=>$inputadminname,
        //         'inputloginname'=>$inputloginname,
        //         'inputPassword'=>$inputPassword,
        $data = array(
            'username' => $inputdata['username'] ,
            'password' => $inputdata['password'] ,
            'chi_name' => $inputdata['chi_name'] ,
            'eng_firstname' => $inputdata['eng_firstname'] ,
            // 'fullName' => $inputdata['inputadminname'] ,
            'eng_lastname' => $inputdata['eng_lastname'] ,
            'gender' => $inputdata['gender'] ,
            'status' => 1 ,
            'display' => 1 ,

            // 'eng_lastname' => $data['eng_lastname'] ,
            // 'role_name' => $data['role_name'] ,
            // 'tenant_no' => $data['tenant_no'] ,
            // 'income_type' => $data['income_type'] ,
            // 'income' => $data['income'] ,
            // 'commission' => $data['commission'] ,
            // 'remark' => $data['remark'] ,


         );
        date_default_timezone_set('UTC');
        
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
         // $this->db->set('update_time', 'NOW()', FALSE);
         // $this->db->set('create_time', 'NOW()', FALSE);
         $query=$this->db->insert('mas_admin', $data);

         //insert mas rbac users
         $data = array(
            'employee_group_id' => 2 ,
            'username' => $inputdata['username'] ,
            'password' => $inputdata['password'] ,
            'chi_name' => $inputdata['chi_name'] ,
            'eng_firstname' => $inputdata['eng_firstname'] ,
            // 'fullName' => $inputdata['inputadminname'] ,
            'eng_lastname' => $inputdata['eng_lastname'] ,
            'gender' => $inputdata['gender'] ,
            'status' => 1 ,
            'display' => 1 ,

            // 'eng_lastname' => $inputdata['eng_lastname'] ,
            // 'role_name' => $inputdata['role_name'] ,
            // 'tenant_no' => $inputdata['tenant_no'] ,
            // 'income_type' => $inputdata['income_type'] ,
            // 'income' => $inputdata['income'] ,
            // 'commission' => $inputdata['commission'] ,
            'remark' => $inputdata['remark'] ,


         );
         $this->db->set('update_time', time(), FALSE);
         $this->db->set('create_time', time(), FALSE);
          $shopsrow=$this->shop_model->getShopbyid($inputdata['tenant_no']);   
          $query=$this->db->insert('mas_rbac_users_'.strtolower($shopsrow->shortcode),$data);

         $employeeid=$this->db->insert_id();  
         //end insert mas rbac users   

         //insert mas rbac role user
            $data = array(
            'user_id' => $employeeid ,
            'role_id' => $inputdata['role_name'] ,
        );
            $shopsrow=$this->shop_model->getShopbyid($inputdata['tenant_no']);   
          $query=$this->db->insert('mas_rbac_role_user_'.strtolower($shopsrow->shortcode),$data);
         //end insert  mas rbac role user


          //insert income lookup
           $data = array(
            'employee_id' => $employeeid ,
            'employee_income_type_id' => $inputdata['income_type'],
            'income' => $inputdata['income'] ,
            'commission' => $inputdata['commission'] ,
        );
          $shopsrow=$this->shop_model->getShopbyid($inputdata['tenant_no']);   
          $query=$this->db->insert('mas_employee_income_lookup_'.strtolower($shopsrow->shortcode),$data);

          //end insert income lookup

         return $query;
    }
    function getAllEmpolyee() {
        $this->db->from('mas_admin');

        $query = $this->db->get();
        return $query->result();
    }
    function gettherapist($id){
        $status_array = array(2,3);
        // $this->db->where('employee_group_id', 2);
        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->where_in('employee_group_id', $status_array);
//         $this->db->where('employee_group_id', 2)->group_start()
// ->or_where('employee_group_id',3)
// ->group_end();
        $this->db->where('delete_time', NULL);
        
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));

        $query = $this->db->get();
        return $query->result();
    } 
    function getEmpolyeeName($id) {
   

        $shopsrow=$this->shop_model->getShopbyid($this->session->userdata('currentshop'));
        $this->db->from('mas_rbac_users_'.strtolower($shopsrow->shortcode));
         $this->db->where('id', $id);    
 
        $chi_name=$this->db->get()->row()->chi_name;
        return $chi_name;

    }
    function getUserDetail($id){
         $this->db->from('mas_admin');
         $this->db->where('id', $id);   

         $row=$this->db->get()->row();
         return $row;
    }
    function gettenant($username){
        // $this->db->from('mas_rbac_users_'.'tst');

        $shops=$this->shop_model->getAllShop();
        
        foreach ($shops as $shopsrow) {
               # code...
            $this->db->where('username', $username); 
            $query = $this->db->get('mas_rbac_users_'.strtolower($shopsrow->shortcode));
            if($query->num_rows() >= 1){
                return $shopsrow->id;

            }

        } //foreach ($shops as $shopsrow) {  

    }
    function getUserRole($shopid,$userid){

        $shopsrow=$this->shop_model->getShopbyid($shopid);
        $this->db->from('mas_rbac_role_user_'.strtolower($shopsrow->shortcode));
         $this->db->where('user_id', $userid);    
 
        $role_id=$this->db->get()->row()->role_id;
        return $role_id;
    }

    function getUserRoleName($roleid){

        $shopsrow=$this->shop_model->getShopbyid($shopid);
        $this->db->from('mas_rbac_roles');
         $this->db->where('id', $roleid);    
 
        $role_name=$this->db->get()->row()->name;
        return $role_name;
    }
    function getEmployeeidbyshopcode($shopcode,$username){
        $this->db->from('mas_rbac_users_'.strtolower($shopcode));

        $this->db->where('username', $username);    
 
        $id=$this->db->get()->row()->id;
        return $id;
    }
    //end added employee
    function getAllShop() {
        $this->db->from('mas_tenant');

        $query = $this->db->get();
        return $query->result();
    }
    function getBannerAll()
    {
        
        $this->db->from('frontendUsers');
        // $this->db->where(array('show' => 1));
        // $this->db->order_by('rank');
        $query = $this->db->get();
        return $query->result();
    }


    function checking(){

    	$userid = $this->session->userdata('userid');
    	$username = $this->session->userdata('username');
    	$fullName = $this->session->userdata('fullName');
    	$logged_in = $this->session->userdata('logged_in');
    	$secret = $this->session->userdata('secret');
    	$usertype = $this->session->userdata('usertype');

    	$passLogin=false;
    	if($usertype=="frontenduser"){
    		if($logged_in==false){
    			return false;
    		}
    		if(empty($userid)){
    			return false;
    		}
    		if(empty($secret)){
    			return false;
    		}
    		$this->db->where('id',$userid);
        	$this->db->where('secret',$secret);
        	if($this->db->get('frontendUsers')->row_array()){
        		$passLogin=true;
        		return true;
        	}	
        	else{
        		return false;
        	}

    	}//if($usertype=="frontenduser"){
    	if($usertype=="backenduser"){
    	    if($logged_in==false){
    			return false;
    		}
    		if(empty($userid)){
    			return false;
    		}
    		if(empty($secret)){
    			return false;
    		}
    		$this->db->where('id',$userid);
        	$this->db->where('secret',$secret);
        	if($this->db->get('adminUsers')->row_array()){
        		$passLogin=true;
        		return true;
        	}	
        	else{
        		return false;
        	}
    	
    	}
    }
 }