<?php

class Usuarios_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->table = 'usuarios';
    }

    public function get_all_usuarios($limit_offset_order_and_filters) {
        $this->db->select('usuarios.id, usuarios.username, usuarios.email, usuarios.status, '
                . ' roles.rol_nombre');
        $this->db->from($this->table);
        $this->db->join('roles', $this->table . '.rol_id = roles.id', 'INNER');
        $this->db->add_filters_order_and_pagination($limit_offset_order_and_filters);
        $result = $this->db->get();
        return $result->result_array();
    }

    public function count_all_usuarios($limit_offset_order_and_filters) {
        $this->db->from($this->table);
        $this->db->add_filters($limit_offset_order_and_filters);
        $count = $this->db->count_all_results();
        return $count;
    }

    public function get_all_usuarios_activos() {
        $this->db->select($this->table . '.id, ' . $this->table . '.username');
        $this->db->from($this->table);
        $this->db->where($this->table . '.status', 1);
        $result = $this->db->get();
        return $result->result_array();
    }

    public function get_all_usuarios_inactivos() {
        $this->db->select('usuarios.id, usuarios.username');
        $this->db->from($this->table);
        $this->db->where($this->table . '.status', -1);
        $result = $this->db->get();
        return $result->result_array();
    }

    public function get_all_usuarios_by_rol_id($rol_id) {
        $this->db->select($this->table . '.id,  ' . $this->table . '.rol_id,  ' . $this->table . '.username,  ' . $this->table . '.status');
        $this->db->from($this->table);
        $this->db->where($this->table . '.rol_id', $rol_id);
        $result = $this->db->get();
        return $result->result_array();
    }

    public function get_usuario_by_id($id) {
        $this->db->select($this->table . '.id,  ' . $this->table . '.rol_id, ' . $this->table . '.username, ' . $this->table . '.email,' . $this->table . '.email_code,  ' . $this->table . '.status');
        $this->db->from($this->table);
        $this->db->where($this->table . '.id', $id);
        $this->db->limit(1);
        $result = $this->db->get();
        return $result->row_array();
    }

    public function get_usuario_by_username($username) {
        $this->db->select($this->table . '.id,  ' . $this->table . '.rol_id,' . $this->table . '.username,  ' . $this->table . '.email,' . $this->table . '.status');
        $this->db->from($this->table);
        $this->db->where(' ' . $this->table . '.username', $username);
        $this->db->limit(1);
        $result = $this->db->get();
        return $result->row_array();
    }

    public function check_username_is_unique_in_sistema_create($username) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('username', $username);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function check_username_in_sistema($username) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('username', $username);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function check_email_is_unique_in_sistema_create($email) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function check_username_is_unique_in_sistema_update($curso_nombre, $id) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('id <>', $id);
        $this->db->where('username', $curso_nombre);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function check_email_is_unique_in_sistema_update($email, $id) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('id <>', $id);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function search_usuario_by_username_and_password($username, $password) {
        $this->db->select($this->table . '.id, ' . $this->table . '.rol_id, ' . $this->table . '.username, ' . $this->table . '.status');
        $this->db->from($this->table);
        $this->db->where($this->table . '.username', $username);
        $this->db->where($this->table . '.password', $password);
        $this->db->limit(1);
        $result = $this->db->get();
        return $result->row_array();
    }

    public function insert($data) {
        if ($this->db->insert($this->table, $data)) {
            $id = $this->db->insert_id();
            if (create_log($this->table, $id, $data)) {
                return $id;
            }
        }
        return FALSE;
    }

    public function update($id, $data) {
        if ($this->db->update($this->table, $data, ['id' => $id])) {
            if (create_log($this->table, $id, $data)) {
                return $id;
            }
        }
        return FALSE;
    }

}
