<?php

class Roles_model extends CI_Model {

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

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

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

    public function get_rol_by_id($id) {
        $this->db->select($this->table . '.id, ' . $this->table . '.rol_nombre');
        $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 check_rol_is_unique_in_sistema_create($rol_nombre) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('rol_nombre', $rol_nombre);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->row_array();
    }

    public function check_rol_is_unique_in_sistema_update($rol_nombre, $id) {
        $this->db->select('status');
        $this->db->from($this->table);
        $this->db->where('id <>', $id);
        $this->db->where('rol_nombre', $rol_nombre);
        $this->db->limit(1);
        $consulta = $this->db->get();
        return $consulta->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;
    }

}
