<?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);
    $html = '';
    $breaks = array('\r\n', '\r', '\n', '\n\r');
    if(isset($_POST['nota'])) {
        $nota = Statics::SafeInput($con, $_POST['nota']);
        $pos = $con->query('SELECT MAX(POS) AS POS FROM VEHICULOS_NOTAS')->fetch_array();
        $p = $pos['POS'] + 1;
        $Schema->executeQuery('INSERT INTO VEHICULOS_NOTAS(CLAVE, POS, FECHA, USUARIO, NOTA) VALUES(?, ?, ?, ?, ?)', 'iisss', [$_SESSION['CODIGO_COCHE'], $p, date('Y-m-d H:i:s'), $usuarioSession, $nota]);
    }
    $res = $Schema->executeQuery('SELECT * FROM VEHICULOS_NOTAS WHERE CLAVE = ? AND CODIGO_TAS = ? ORDER BY POS DESC', 'is', [$_SESSION['CODIGO_COCHE'], FALSE]);
    if($res->num_rows) {
        while($row = $res->fetch_array()) {
            $html .= '<tr id="tr-' . $row['POS'] . '">';
                $html .= '<td>';
                    if($usuarioSession == $row['USUARIO']) {
                        $html .= '<i class="fas fa-trash-alt mr-3 c-pointer text-danger delete-note" data-position="' . $row['POS'] . '"></i><i class="fas fa-pen mr-1 c-pointer text-info edit-note" data-position="' . $row['POS'] . '"></i>';
                    } else {
                        $html .= '<i class="fas fa-trash-alt mr-3 text-muted"></i><i class="fas fa-pen mr-1 text-muted"></i>';
                    }
                $html .= '</td>';
                $html .= '<td>' . date('d/m/Y', strtotime($row['FECHA'])) . '</td>';
                $html .= '<td>' . $row['USUARIO'] . '</td>'; 
                $notas = str_ireplace($breaks, "<br>", $row['NOTA']); 
                $html .= '<td id="td-' . $row['POS'] . '" data-values="' . $notas . '">' . $notas . '</td>';
            $html .= '</tr>';
        }
    } else {
        $html .= '<tr><td class="text-center" colspan="4">' . $sin_resultados_lg . '</td></tr>';
    }

    echo $html;
?>

<script>
    $('.delete-note').on('click', function() {
        if(confirm("<?php echo $seguro_eliminar_nota_lg; ?>")){
            var pos = $(this).data('position');
            /* console.log(pos); */
            $('#tr-' + pos).fadeOut();
            $.post('php_o/notas-destacadas.php', {pos: pos})
        }
    })

    $('.edit-note').on('click', function() {
        var pos = $(this).data('position');
        $('#td-' + pos).html('');
        var value = $('#td-' + pos).data('values');
        var textarea = '<div id="edit-' + pos + '"><textarea class="form-control" id="edit-this-note-' + pos + '">' + value + '</textarea><button type="button" class="btn btn-primary btn-block btn-sm mt-2" id="edit-note-btn-' + pos + '">edit</button></div>';
        $('#td-' + pos).append(textarea)
        $('#edit-note-btn-' + pos).on('click', function() {
            console.log(pos);
            var noteEdit = $('#edit-this-note-' + pos).val();
            if(noteEdit == '') {
                $('#edit-this-note-' + pos).addClass('is-invalid');
            } else {
                $('#edit-this-note-' + pos).removeClass('is-invalid');
                $.post('php_o/notas-destacadas.php', {pos: pos, edit: noteEdit});
                $('#td-' + pos).remove('#edit-' + pos);
                $('#td-' + pos).html(noteEdit);
                $('#td-' + pos).data('values', noteEdit);
            }
        })
        console.log(pos);
    })

</script>