<?php

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

class Login_model extends CI_Model
{
    public function __construct()
    {

        parent::__construct();

        $this->load->model('System_admin_model');
    }

    public function validate(){
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        $result = $this->System_admin_model->validate($username,$password);
        if (!$result){
            return false;
        }

        $userInfo = [
            'userid' => $result['id'],
            'username' => $result['username'],
            'lastname' => $result['last_name'],
            "firstname" => $result['first_name'],
            'validated' => true
        ];

        $this->load->model('System_log_model');

        $this->systemLog($result['id']);
        $this->session->set_userdata($userInfo);
        return true;

    }

    public function systemLog($userId){
        $this->System_admin_model->systemLog($userId);
        $this->System_log_model->in($userId);
    }
}
