<?php 

session_start();
$usuarioSession = $_SESSION['USUARIO'];
$grupo = $_SESSION['GRUPO'];
$contacto = $_SESSION['CONTACTO'];
$awa_session_id = $_SESSION['AWA_SESION_ID'];

include('../../lg/config_vo.php');
include('../../includes/conex.php');
include('../../includes/eat.php');
include('../../class/Schema.php');

$cookieUser = filter_input(INPUT_COOKIE, 'usuario') ?? null; 
$cookieIdioma = filter_input(INPUT_COOKIE, 'idioma') ?? null; 
if((!isset($usuarioSession) or $awa_session_id != $awa_id) or (isset($cookieUser) AND $cookieUser != $staticUsu )){
    eatCookies();
    header('Location:../../index.php');}
empty($cookieIdioma)?
    include('../../lg/LG_' . $cookieIdioma . '.php'):
    include('../../lg/LG_SP.php');

$codigogrupo = (int)filter_input(INPUT_POST, 'codigogrupo') ?? null;
$perfil      = filter_input(INPUT_POST, 'nombreperfil') ?? null;
$permiso     = filter_input(INPUT_POST, 'tipopermiso') ?? null; 
$codrolpost  = (!empty(filter_input(INPUT_POST, 'rol')))|| (filter_input(INPUT_POST, 'rol') === "0") ? (int)filter_input(INPUT_POST, 'rol') : null; 

$html = '';
$html .= '<div class="modal-header">';
    $html .= '<h5 class="modal-title text-dark">' . $permisos_de_rol_lg . ": <span class='font-weight-light'>" . $perfil . "</span>" . '</h5>';
        $html .= '<button type="button" class="close" data-dismiss="modal" aria-label="Close">';
            $html .= '<span aria-hidden="true">&times;</span>';
        $html .= '</button>';
    $html .= '</div>';
$html .= '<div class="modal-body">';

//TO-DO  ESTA TABLA AUN NO EXISTE, SE HARDCODEA DE MOMENTO
//$res = $con->query("SELECT * FROM ROLES_ACCESO");
$roles = [0 => "Administrador", 1 => "Gerente",2 => "Comercial",3 => "GestorOnline",4 => "Tasador",99 => "AdoptaUnCoche"];
unset($roles[99]);// No se ofrece esta opcion al usuario
$html .= '<table class="table table-bordered"  style="border:none">';
    $html .= '<tbody style="border:none">';
    //while($row = $res->fetch_array()) { // TO-DO PARA RECORRER UNA CONSULTA
    foreach ($roles as  $codigorol => $descripcionrol){
        $checked =  ($codigorol === $codrolpost ) ? " checked='checked' " : "";
        $html .= '<tr style="border:none; align:center;">';
            $html .= '<td style="border:none; text-align:center; width:10%; "  ><input type="radio" id="'.$codigorol.'" name="radiobtn_rol" '.$checked.' value="'.$codigorol.'"></td>';
            $html .= '<td style="border:none"><label for="'.$codigorol.'">'.$descripcionrol.'</label></td>';
        $html .= '</tr>';
    }
    $html .= '</tbody>';
$html .= '</table>';
$html .= '</div>';
//
//$html .= '<div class="modal-footer">';
//$html .=    '<button type="button" class="btn btn-primary" data-dismiss="modal">' . $guardar_cerrar_lg . '</button>';
//$html .= '</div>';

echo $html;

?>
    
<script>
    $("input[name='radiobtn_rol']").change(function(){
        //console.log("Cambio en el radiobutton de cambio de rol");
        $.post('ajax/ajustes-permisos-update-rol.php', {codigogrupo:<?=$codigogrupo?>, codigonuevorol: this.value });
    });
</script>