<?php
    session_start();
    $usuarioSession = $_SESSION['USUARIO'];
    $grupo = $_SESSION['GRUPO'];
    $contacto = $_SESSION['CONTACTO'];
    $awa_session_id = $_SESSION['AWA_SESION_ID'];
    include('../includes/conex.php');
    include('../includes/eat.php');
    include('../class/Schema.php');
    include('../class/Static.php');
    include('../class/Permisos.php');
    if(!isset($usuarioSession) or $awa_session_id != $awa_id) {
        eatCookies();
        header('Location:../index.php');
    }
    if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] !== $staticUsu ) {
        eatCookies();
        header('Loction:../index.php');
    }
    if(isset($_COOKIE['idioma'])) {
        include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
    } else { include('../lg/LG_SP.php'); }

    $Schema = new Schema($con);
    $Permisos = new Permisos($con);
    $radio_value = isset($_POST['radio_value']) ? (int)Statics::SafeInput($con,$_POST['radio_value']) : 0;

    if($radio_value === 1) {
        $condition = ' AND ACTIVO = ? AND ORGANIZACION = ? ';
        $type = 'ii';
        $array_value = [1, 1];
    } elseif($radio_value === 2) {
        $condition = ' AND ACTIVO = ? AND CONTACTO = ? ';
        $type = 'ii';
        $array_value = [1, 1];
    } else {
        $condition = ' AND ACTIVO = ? ';
        $type = 'i';
        $array_value = [1];
    }

    $html = '<table class="table table-sm table-hover table-borderless table-striped" id="agenda">';

        $html .= '<thead>';
            $html .= '<tr class="text-dark">';
                $html .= '<th>' . $nombre_lg . '</th>';
                $html .= '<th></th>';
            $html .= '</tr>';
        $html .= '</thead >';
            
        $html .= '<tbody>';

        $editar_contacto = $Permisos->permisosACC(26, $_SESSION['GRUPO']) ? TRUE : FALSE;
        $relacion_contacto = $Permisos->permisosACC(27, $_SESSION['GRUPO']) ? TRUE : FALSE;
        $res = $Schema->executeQuery("SELECT * FROM CONTACTOS WHERE NOMBRE != '' $condition ORDER BY MOSTRAR ASC", $type, $array_value);
        while ($row = $res->fetch_array()) {
            $tipo_contacto_icono = $row['CONTACTO'] == 1 ? '<i class="fas fa-user pl-2 mr-2" data-toggle="tooltip" data-placement="top" data-title="' . $contacto_lg . '"></i><span class="d-none">' . $contacto_lg . '</span>' : '<i class="fas fa-building pl-2 mr-2" data-toggle="tooltip" data-placement="top" data-title="' . $organizacion_lg . '"></i><span class="d-none">' . $organizacion_lg . '</span>';
            if($editar_contacto) {
                $button_editar_contacto = '<a href="agenda-editar-contacto.php?codigo_contacto=' . $row['CODIGO'] . '">
                    <i class="fas fa-pen text-secondary" data-toggle="tooltip" data-placement="top" data-title="' . $editar_lg . '"></i>
                </a>';
            } else { $button_editar_contacto = FALSE; }

            if($relacion_contacto) {
                $button_relacion_contacto = ' <a data-toggle="tooltip" onclick="agendaRelacion(' . $row['CODIGO'] . ')" class="c-pointer" data-placement="top" data-title="' . $relacionar_lg . '">
                    <i class="fas fa-link text-secondary" data-toggle="modal" data-target=".relacionAgenda"></i>
                </a>';
            } else { $button_relacion_contacto = FALSE; }

            $button_delete = '<a data-toggle="tooltip" class="delete-register x c-pointer" data-title="' . $eliminar_lg . '" data-values="' . $row['CODIGO'] . '">
                <i class="fas fa-trash text-secondary text-danger"></i>
            </a>';

            $html .= '<tr id="c-' . $row['CODIGO'] . '">';
                $html .= '<td class="text-dark">' . $tipo_contacto_icono . mb_strtoupper($row['MOSTRAR'], 'UTF-8') . '</td>';
                $html .= '<td class="d-flex justify-content-around">'  . $button_editar_contacto . $button_relacion_contacto . $button_delete . '</td>';
            $html .= '</tr>';
        }
        $html .= '</tbody>';
    $html .= '</table>';
    echo $html;
?>

<script>
    <?php if($lg == 'UK'){ ?>
        $('#agenda').dataTable();
    <?php } else { ?>
        $("#agenda").dataTable({
            "targets": 'no-sort',
            "bSort": false,
            "order": [],
            "language": {
                "lengthMenu": "_MENU_",
                "zeroRecords": "No se encontraron resultados",
                "info": "Registros del _START_ al _END_",
                "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                "sSearch": "",
                "searchPlaceholder": "Buscar por los campos",
                "oPaginate": {
                    "sFirst": "Primero",
                    "sLast": "Último",
                    "sNext": "Siguiente",
                    "sPrevious": "Anterior"
                },
                "sProcessing": "Procesando..."
            }
        });
    <?php } ?>

    $('#agenda tbody').on('click', 'tr td .delete-register', function() {
        var contacto = $(this).data('values');
        if(confirm("<?php echo $eliminar_contacto_lg; ?>")) {
            $.post('ajax/contacto_json.php', {value: contacto, request: 2}, function(data) {
                var object = JSON.parse(data);
                if(object.status === 1) {
                    $('#c-' + contacto).fadeOut();
                }
            });
        }
    });
</script>