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

class Auth_model extends CI_Model {
    private $tblUsers;
    function __construct(){
        parent::__construct();
        $this->tblUsers = $this->config->item('tbl_users');
    }

    function doLogin($params){
        if(!isset($params['username']) || $params['username'] == ''){
            return returnResponse('01', 'Username wajib di isi');
        }

        if(!isset($params['password']) || $params['password'] == ''){
            return returnResponse('01', 'Password wajib di isi');
        }

        $username = trim($params['username']);
        $password = sha1(trim($params['password']));

        $this->db->select('user_id, username, nama_lengkap, tgl_update, user_type');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get($this->tblUsers);
        if($query->num_rows() > 0){
            $sessData = array('user_id' => $query->row()->user_id,
                              'username' => $query->row()->username,
                              'nama_lengkap' => $query->row()->nama_lengkap,
                              'tgl_update' => $query->row()->tgl_update,
                              'user_type' => $query->row()->user_type,
                              'is_logged_in' => true,
                              'time_logged_in' => date('Y-m-d H:i:s')
                              );

            $this->session->set_userdata($sessData);
            return returnResponse('00', 'Login sukses...');
        }else{
            return returnResponse('01', 'Username atau password yang masukan salah');
        }
    }
}
