import Ajax from '../../components/ajax.js';

document.addEventListener('DOMContentLoaded', function () {

    const lastColNr = $('#tableOfPaises').find("tr:first th").length - 1;

    const theTable = $('#tableOfPaises').DataTable({
        processing: true,
        serverSide: true,
        autoWidth: true,
        responsive: true,
        scrollX: true,
        lengthMenu: [5, 10, 25, 50, 75, 100, 250, 500, 1000, 2500],
        pageLength: 10,
        lengthChange: true,
        dom: 'lfBrtip',
        buttons: [
            'copy', 'csv', 'excel', 'print', {
                extend: 'pdfHtml5',
                orientation: 'landscape',
                pageSize: 'A4'
            }
        ],
        stateSave: true,
        order: [[1, 'asc']],
        language: {
            url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
        },
        ajax: {
            url: '/configuracion/paises/datatable',
            method: 'GET'
        },
        columnDefs: [
            {
                orderable: false,
                searchable: false,
                targets: [lastColNr]
            }
        ],
        columns: [
            { data: 'nombre' },
            { data: 'code' },
            { data: 'code3' },
            { data: 'moneda' },
            { data: 'url_erp' },
            { data: 'user_erp' },
            { data: 'key_erp' },
            { data: 'show_erp' },
            { data: 'actionBtns' }
        ]
    });

    $(document).on('click', '.btn-edit', function (e) {
        window.location.href = '/configuracion/paises/edit/' + $(this).attr('data-id');
    });

    $(document).on('click', '.btn-delete', function (e) {
        e.preventDefault();
        const row = $(this).closest('tr')[0]._DT_RowIndex;
        const dataId = $(this).attr('data-id');

        Swal.fire({
            title: '¿Estás seguro?',
            text: 'Esta acción no se puede deshacer.',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Sí',
            cancelButtonText: 'No',
            reverseButtons: false, 
            buttonsStyling: true, 
            customClass: {
                confirmButton: 'btn btn-danger',   // rojo para "Sí"
                cancelButton: 'btn btn-secondary'  // gris para "No"
            }
        }).then((result) => {
            if (result.isConfirmed) {
                new Ajax(
                    '/configuracion/paises/delete/' + dataId,
                    {},
                    {},
                    (data, textStatus, jqXHR) => {
                        theTable.clearPipeline();
                        theTable.row($(row)).invalidate().draw();
        
                        popSuccessAlert(data.msg ?? jqXHR.statusText);
                    },
                    (error) => {
                        console.error(error);
                        Swal.fire('Error', 'No se pudo eliminar el país.', 'error');
                    }
                ).get();
            }
        });
        
    });

});
