<?php

defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/RestController.php';

use chriskacerguis\RestServer\RestController;

require APPPATH . 'libraries/Format.php';

class Modulos_acciones_roles extends RestController {

    public function __construct() {
        parent::__construct();
        $this->load->model('modulos_acciones_roles_model');
    }

    public function create_multiple_post() {
        $this->_form_validation_create_multiple();
        $modulos_acciones_ids = $this->post('modulos_acciones_ids');
        $modulos_acciones_roles_ids = array();
        $this->db->trans_start();
        foreach ($modulos_acciones_ids as $modulo_accion_id) {
            $modulo_accion_rol_id = $this->modulos_acciones_roles_model->insert($this->_fill_data_insert($modulo_accion_id));
            if (!$modulo_accion_rol_id) {
                $this->response(['message' => 'Error al guardar los registrod, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
            } else {
                $modulos_acciones_roles_ids[] = ['modulo_accion_id' => $modulo_accion_id, 'modulo_accion_rol_id' => $modulo_accion_rol_id];
            }
        }
        $this->db->trans_complete();
        $this->response(["message" => "Permisos agregados.", 'modulos_acciones_roles_ids' => $modulos_acciones_roles_ids], RestController::HTTP_OK);
    }

    public function inactivate_multiple_post() {
        $this->_form_validation_inactivate_multiple();
        $modulos_acciones_roles_ids = $this->post('modulos_acciones_roles_ids');
        $this->db->trans_start();
        foreach ($modulos_acciones_roles_ids as $modulo_accion_rol_id) {
            if (!$this->modulos_acciones_roles_model->update($modulo_accion_rol_id, $this->_fill_data_inactivate())) {
                $this->response(['message' => 'Error al actualizar los registros, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
            }
        }
        $this->db->trans_complete();
        $this->response(["message" => "Permisos removidos."], RestController::HTTP_OK);
    }

    public function create_post() {
        $this->_form_validation_create();
        if ($id = $this->modulos_acciones_roles_model->insert($this->_fill_data_insert())) {
            $this->response(["message" => "Permiso agregado.", 'id' => $id], RestController::HTTP_OK);
        } else {
            $this->response(['message' => 'Error al guardar el registro, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
        }
    }

    public function inactivate_post() {
        $this->_params_validation_id();
        if ($this->modulos_acciones_roles_model->update($this->get('id'), $this->_fill_data_inactivate())) {
            $this->response(["message" => "Permiso removido."], RestController::HTTP_OK);
        } else {
            $this->response(['message' => 'Error al actualizar el registro, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
        }
    }

    private function _fill_data_insert($modulo_accion_id = NULL) {
        $data = array();
        $data['rol_id'] = $this->post('rol_id');
        $data['modulo_accion_id'] = $modulo_accion_id === NULL ? $this->post('modulo_accion_id') : $modulo_accion_id;
        $data['status'] = 1;
        $data['created_at'] = date("Y-m-d H:i:s");
        return $data;
    }

    private function _fill_data_inactivate() {
        $data = array();
        $data['status'] = -1;
        $data['updated_at'] = date("Y-m-d H:i:s");
        return $data;
    }

    private function _params_validation_id() {
        $this->load->library('params_validation');
        $this->params_validation->set_rules('id', 'ID', 'trim|required|is_natural_no_zero');
        if (!$this->params_validation->run()) {
            $this->response(array('message' => params_validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    private function _form_validation_create() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('rol_id', 'ID rol', 'trim|required|max_length[255]');
        $this->form_validation->set_rules('modulo_accion_id', 'ID módulo acción', 'trim|required|max_length[255]');
        if (!$this->form_validation->run()) {
            $this->response(array('message' => validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    private function _form_validation_create_multiple() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('rol_id', 'ID rol', 'trim|required|max_length[255]');
        $this->form_validation->set_rules('modulos_acciones_ids[]', 'ID módulos acciones', 'trim|required|callback_validate_modulos_acciones_ids');
        if (!$this->form_validation->run()) {
            $this->response(array('message' => validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    private function _form_validation_inactivate_multiple() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('modulos_acciones_roles_ids[]', 'ID módulos acciones roles', 'trim|required|callback_validate_modulos_acciones_roles_ids');
        if (!$this->form_validation->run()) {
            $this->response(array('message' => validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    public function validate_modulos_acciones_ids() {
        $modulos_acciones_ids = $this->post('modulos_acciones_ids');
        if (!is_array($modulos_acciones_ids)) {
            $this->response(array('message' => "Error al enviar los IDS de acciones."), RestController::HTTP_BAD_REQUEST);
        }
        foreach ($modulos_acciones_ids as $modulo_accion_id) {
            if (!is_numeric($modulo_accion_id) || ((int) $modulo_accion_id) < 10000) {
                $this->response(array('message' => "El ID de la acción " . $modulo_accion_id . " no existe."), RestController::HTTP_BAD_REQUEST);
            }
        }
    }

    public function validate_modulos_acciones_roles_ids() {
        $modulos_acciones_roles_ids = $this->post('modulos_acciones_roles_ids');
        if (!is_array($modulos_acciones_roles_ids)) {
            $this->response(array('message' => "Error al enviar los IDS de modulos acciones roles."), RestController::HTTP_BAD_REQUEST);
        }
        foreach ($modulos_acciones_roles_ids as $modulo_accion_rol_id) {
            if (!is_numeric($modulo_accion_rol_id) || ((int) $modulo_accion_rol_id) < 1) {
                $this->response(array('message' => "El ID de las acciones deben ser n&uacute;meros mayores a 0."), RestController::HTTP_BAD_REQUEST);
            }
        }
    }
}
