<?php

class M_login extends CI_Model {

	public $table = 'tbl_user';
	
	function check_user($username, $password) {
		$query = $this->db->get_where($this->table, array('EmployeeCode' => $username, 'EmployeePassword' => $password), 1, 0);
		
		if ($query->num_rows() > 0) {
			return TRUE;
		}
		else {
			return FALSE;
		}	
	}
	
	function user_login($data_login) {
		$this->db->insert('tbl_userlog', $data_login);
		$log_id = $this->db->insert_id();
		return $log_id;	
	}
	
	function user_logout($log_id, $data_login) {
		$this->db->update('tbl_userlog', $data_login, array('UserLogID' => $log_id));
	}
	
	function get_group_id($employee) {
        $this->db->select('GroupID');
		$this->db->from('tbl_user');
		$this->db->where('EmployeeCode', $employee);		
		$query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$group_id = $row->GroupID;
			}    

			return $group_id;
		}
		else {
			return FALSE;
		}	
    }
	
	function get_employee_name($employee) {
        $this->db->select('EmployeeName');
		$this->db->from('hm_employee');
		$this->db->where('EmployeeCode', $employee);		
		$query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$EmployeeName = $row->EmployeeName;
			}    

			return $EmployeeName;
		}
		else {
			return FALSE;
		}	
    }
	
	function get_main_authority($group) {
		$this->db->select('MainAuthority');
		$this->db->from('tbl_groupuser');
		$this->db->where('GroupID', $group); 
        $query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$MainAuthority = $row->MainAuthority;
			}    

			return $MainAuthority;
		}
		else {
			return FALSE;
		}        
	}
	
	function get_code_authority($username) {
		$this->db->select('AuthorityCode');
		$this->db->from('tbl_groupauthority');
		$this->db->where('EmployeeCode', $username); 
        $query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {	
				$code_authority[] = $row->AuthorityCode;
			}	    

			return $code_authority;
		}
		else {
			return FALSE;
		}    
	}
	
	function get_menu_id($group) {
		$this->db->select('MenuID');
		$this->db->from('tbl_groupauthority');
		$this->db->where('GroupID', $group); 
        $query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$menu_id[] = $row->MenuID;
			}    

			return $menu_id;
		}
		else {
			return FALSE;
		}  
		
		
	}
	
	function get_parent_id($menu_id) {
		$this->db->select('ParentID');
		$this->db->from('tbl_menu');
		$this->db->where('MenuID', $menu_id); 
        $query = $this->db->get();    
        
		foreach ($query->result() as $row) {
			$parent_id = $row->ParentID;
		}    

		return $parent_id;
	}
	
	function get_menu_name($menu_id) {
		$this->db->select('MenuName');
		$this->db->from('tbl_menu');
		$this->db->where('MenuID', $menu_id); 
        $query = $this->db->get();    
        
		foreach ($query->result() as $row) {
			$menu_name = $row->MenuName;
		}    

		return $menu_name;
	}
	
	function get_menu_id_by_menu_name($menu_name) {
		$this->db->select('MenuID');
		$this->db->from('tbl_menu');
		$this->db->where('MenuName', $menu_name); 
        $query = $this->db->get();    
        
		foreach ($query->result() as $row) {
			$menu_id = $row->MenuID;
		}    

		return $menu_id;
	}
	
}	
	
	