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

switch ($_GET["op"]) {
    case "listar":
        $datos = $usuario->get_usuario();
        $data = [];
        foreach ($datos as $row) {
            $sub_array = [];
            $sub_array[] = $row["Ccod_usu"];
            $sub_array[] = $row["Nom_usu"];
            $sub_array[] = $row["Apell_usu"];
            $sub_array[] = $row["Correo_usu"];
            if ($row["N_rolID"] == '1') {
                $sub_array[] = '<span class="label label-pill label-success">TI</span>';
            } elseif ($row["N_rolID"] == '2') {
                $sub_array[] = '<span class="label label-pill label-primary">Administrador</span>';
            } elseif ($row["N_rolID"] == '3') {
                $sub_array[] = '<span class="label label-pill label-warning">Comercial</span>';
            } elseif ($row["N_rolID"] == '4') {
                $sub_array[] = '<span class="label label-pill label-danger">Almacen</span>';
            } elseif ($row["N_rolID"] == '5') {
                $sub_array[] = '<span class="label label-pill label-default">Operaciones</span>';    
            } else {
                $sub_array[] = '<span class="label label-pill label-default">Invitado</span>';
            }
            $sub_array[] = date("d/m/Y", strtotime($row["Dfech_crea"]));
            $sub_array[] = date("d/m/Y H:i:s", strtotime($row["Dfech_mod"]));
            if ($row["Cusuest"] == '1') {
                $sub_array[] = '<span class="label label-pill label-success">Activo</span>';
            } else {
                $sub_array[] = '<span class="label label-pill label-default">Inactivo</span>';
            }
            $sub_array[] = '<button type="button" onClick="editar(' . $row["Ccod_usuID"] . ');" id="' . $row["Ccod_usuID"] . '" class="btn btn-info btn-sm"><i class="fa fa-cogs"></i></button>';
            $sub_array[] = '<button type="button" onClick="mostrar(' . $row["Ccod_usuID"] . ');" id="' . $row["Ccod_usuID"] . '" class="btn btn-info btn-sm"><i class="fa fa-eye"></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["Ccod_usuID"])) {
            $usuario->insert_usuario($_POST["Ccod_usu"], $_POST["Nom_usu"], $_POST["Apell_usu"], $_POST["Correo_usu"], $_POST["Pass_usu"], $_POST["N_rolID"]);
        } else {
            $usuario->update_usuario($_POST["Ccod_usuID"], $_POST["Ccod_usu"], $_POST["Nom_usu"], $_POST["Apell_usu"], $_POST["Correo_usu"], $_POST["Pass_usu"], $_POST["N_rolID"], $_POST["Cusuest"]);
        }
        break;

    case "mostrar":
        if (isset($_POST["Ccod_usuID"])) {
            $datos = $usuario->get_usuario_id($_POST["Ccod_usuID"]);
            if (is_array($datos) && count($datos) > 0) {
                $output["Ccod_usuID"] = $datos["Ccod_usuID"];
                $output["Ccod_usu"] = $datos["Ccod_usu"];
                $output["Nom_usu"] = $datos["Nom_usu"];
                $output["Apell_usu"] = $datos["Apell_usu"];
                $output["Correo_usu"] = $datos["Correo_usu"];
                $output["Pass_usu"] = $datos["Pass_usu"];
                $output["N_rolID"] = $datos["N_rolID"];
                $output["Cusuest"] = $datos["Cusuest"];
                echo json_encode($output);
            }
        }
        break;
    

    case "combo":
        $datos = $usuario->get_roles();
        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;

}
?>
