<?php

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

class Login_Model extends MY_Model {

    function __construct() {
        parent::__construct();
    }

    public function getUser($username, $password, $userID = null, $employeeId = null) {
        $this->db->select('U.id,U.username,U.display, U.group,U.branch,U.team, U.onapp,U.eposStyle,U.beep,U.beepLabel,E.first_name ,E.last_name');
        $this->db->from('user U');
        $this->db->join('employee E', 'U.employee=E.id', 'left');
        if ($userID && $employeeId) {
            $this->db->where(array('U.id' => $userID, 'U.username' => $username, 'U.password' => md5($password), 'E.id' => $employeeId));
        } else {
            $this->db->where(array('U.username' => $username, 'U.password' => md5($password)));
        }
        $this->db->where(array('U.branch' => $this->session->userdata('loginBranch'), 'U.team' => $this->session->userdata('loginTeam')));
        $thisUser = $this->db->get()->result();
        return $thisUser;
    }

    public function getEngineer($username, $password) {
        return $this->get_single('user', array('username' => $username, 'password' => md5($password)))->row();
    }

    public function eposUserLogs($data, $id = null) {
        $this->_table_name = 'userlogtime';
        $this->_primary_key = 'id';
        $data['branch'] = $this->session->userdata('branch');
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        return $this->save($data, $id);
    }
}
