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

class Userlog_model extends CI_Model {
    protected $_table='user_log';

    public function __construct(){
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    /**
     * Hàm lưu log vào/ra hệ thống
     *
     * @param $action
     * @param $account_input
     * @param $result
     * @param null $ip
     * @param string $type
     * @return mixed
     */
    public function add($action, $account_input, $result, $ip=null, $type='BE'){
        $dataInsert = array(
            'action' => $action,
            'time_login' => date('Y-m-d H:i:s'),
            'account_input' => $account_input,
            'ip_address' => $ip,
            'user_agent' => $this->input->user_agent(),
            'type' => $type,
            'result' => $result
        );
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    /**
     * Hàm update thời gian sử dụng hệ thống
     *
     * @param $id
     * @param $time_login
     */
    public function update($id, $time_login){
        $time_logout = date('Y-m-d H:i:s');
        $duration = (int)(strtotime($time_logout) - strtotime($time_login));
        $dataUpdate = array(
            'time_logout' => $time_logout,
            'duration' => $duration
        );
        $this->db->where('id', $id);
        $this->db->update($this->_table, $dataUpdate);
    }

    /**
     * Hàm xóa thông tin log vào/ra
     *
     * @param $id
     */
    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->_table);
    }

    /**
     * Hàm lấy thông tin chi tiết log vào/ra
     *
     * @param $id
     * @return mixed
     */
    public function getById($id){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table.'.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Hàm lấy tổng số bản ghi log vào/ra
     *
     * @param null $dtFrom
     * @param null $dtTo
     * @param null $filterByIP
     * @param null $filterByAccount
     * @param null $filterByResult
     * @return int
     */
    public function countAll($dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByResult=null){
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);

        if($dtFrom != null){$this->db->where('DATE(time_login) >=', $dtFrom);}
        if($dtTo != null){$this->db->where('DATE(time_login) <=', $dtTo);}
        if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
        if($filterByAccount != null){$this->db->like('account_input', $filterByAccount);}
        if($filterByResult != null){$this->db->where('UPPER(result)', $filterByResult);}

        $query = $this->db->get();
        $result = $query->result_array();
        if($result){
            return $result[0]['totalResults'];
        }else{
            return 0;
        }
    }

    /**
     * Hàm lấy dữ liệu phân trang log vào/ra
     *
     * @param $record
     * @param $start
     * @param null $dtFrom
     * @param null $dtTo
     * @param null $filterByIP
     * @param null $filterByAccount
     * @param null $filterByResult
     *
     * @return mixed
     */
    public function getPagination($record, $start, $dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByResult=null){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);

        if($dtFrom != null){$this->db->where('DATE(time_login) >=', $dtFrom);}
        if($dtTo != null){$this->db->where('DATE(time_login) <=', $dtTo);}
        if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
        if($filterByAccount != null){$this->db->like('account_input', $filterByAccount);}
        if($filterByResult != null){$this->db->where('UPPER(result)', $filterByResult);}

        $this->db->order_by($this->_table.'.id', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */