<?php
require_once("../config/conexion.php");
require_once("../modelos/Rol.php");
$rol = new Rol();

switch ($_GET["op"]) {
    case "combo":
        $datos = $rol->get_rol();
        if (is_array($datos) == true and count($datos) > 0) {
            $html = "";
            foreach ($datos as $row) {
                $html .= "<option value='" . $row['N_rolID'] . "'>" . $row['Sdescrip'] . "</option>";
            }
            echo $html;
        }
        break;

    case "listar":
        $datos = $rol->get_rol();
        $data = [];
        foreach ($datos as $row) {
            $sub_array = [];
            $sub_array[] = $row["N_rolID"];
            $sub_array[] = $row["Sdescrip"];
            $sub_array[] = ($row["Cest_rol"] == 1) ? '<span class="label label-pill label-success">Activo</span>' : '<span class="label label-pill label-default">Inactivo</span>';
            $sub_array[] = '<button type="button" onClick="editar(' . $row["N_rolID"] . ');" id="' . $row["N_rolID"] . '" class="btn btn-info btn-sm"><i class="fa fa-cogs"></i></button>';
            $data[] = $sub_array;
        }
        $results = [
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        ];
        echo json_encode($results);
        break;

    case "guardaryeditar":
        if (empty($_POST["N_rolID"])) {
            $rol->insert_rol($_POST["Sdescrip"], $_POST["Cest_rol"]);
        } else {
            $rol->update_rol($_POST["N_rolID"], $_POST["Sdescrip"], $_POST["Cest_rol"]);
        }
        break;

    case "mostrar":
        if (isset($_POST["N_rolID"])) {
            $datos = $rol->get_rol_id($_POST["N_rolID"]);
            if (is_array($datos) && count($datos) > 0) {
                $output["N_rolID"] = $datos["N_rolID"];
                $output["Sdescrip"] = $datos["Sdescrip"];
                $output["Cest_rol"] = $datos["Cest_rol"];
                echo json_encode($output);
            }
        }
        break;
}
?>
