cambiado ael añadir envios

This commit is contained in:
2025-04-24 23:44:07 +02:00
parent f16654f4ad
commit 5c68334e9a
8 changed files with 200 additions and 109 deletions

View File

@ -1,38 +1,55 @@
import Ajax from '../../components/ajax.js';
import ClassSelect from '../../components/select2.js';
$(() => {
$('#buscadorPedidos').on('keydown', function (e) {
if (e.key === 'Enter' || e.keyCode === 13) {
e.preventDefault(); // Evita el submit si está dentro de un form
let search = $(this).val().trim();
new Ajax(
'/logistica/buscar/' + search,
{},
{},
function (response) {
if (!response.status) {
popErrorAlert(response.message);
}
if (response.multienvio) {
mostrarSwalDirecciones(response);
}
else if (response.data) {
window.open(`${window.location.origin}/logistica/envio/${response.data.id_envio}`);
}
},
function (xhr, status, error) {
if (status == 'error' && typeof (error) == 'string')
popErrorAlert(error);
else
popErrorAlert(error.responseJSON.message);
}
).get();
}
const selectPedidos = new ClassSelect($('#buscadorPedidos'), '/logistica/selectPedidosForEnvio', "");
selectPedidos.init();
const selectDirecciones = new ClassSelect($('#selectDirecciones'), '/logistica/selectDireccionForEnvio', "", true, {
pedido_id: () => selectPedidos.getVal()
});
selectDirecciones.init();
selectPedidos.item.on('select2:open', () => {
$('.select-direcciones').addClass('d-none');
$('.add-envio').addClass('d-none');
})
selectPedidos.item.on('change', () => {
selectDirecciones.empty();
$('.select-direcciones').removeClass('d-none');
})
selectDirecciones.item.on('select2:open', () => {
$('.add-envio').addClass('d-none');
})
selectDirecciones.item.on('change', () => {
$('.add-envio').removeClass('d-none');
})
$('#btnAddEnvio').on('click', () => {
const pedido_id = selectPedidos.getVal();
const direccionSeleccionada = selectDirecciones.getText();
$.post('/logistica/generateEnvio', {
pedido_id: pedido_id,
direccion: direccionSeleccionada
}, function (response) {
if (response.status) {
window.open(`${window.location.origin}/logistica/envio/${response.data.id_envio}`);
selectDirecciones.empty();
selectPedidos.empty();
$('.select-direcciones').addClass('d-none');
$('.add-envio').addClass('d-none');
} else {
popErrorAlert(response.message);
}
}).fail(function (xhr, status, error) {
popErrorAlert(error);
});
})
const tableEnvios = $('#tableOfEnvios').DataTable({
processing: true,
@ -82,51 +99,6 @@ $(() => {
window.location.href = '/logistica/envio/' + $(this).attr('data-id');
});
function mostrarSwalDirecciones(response) {
// Crear el select como HTML
let options = response.direcciones.map((dir, index) =>
`<option value="${index}">${dir}</option>`
).join("");
Swal.fire({
title: 'Múltiples direcciones',
html: `
<p>El pedido tiene múltiples direcciones. Debe seleccionar una dirección:</p>
<select id="swal-select-direccion" class="form-select" style="width: 100%; margin-top: 1rem;">
${options}
</select>
`,
confirmButtonText: 'Aceptar',
cancelButtonText: 'Cancelar',
customClass: {
confirmButton: 'swal2-confirm btn btn-primary',
cancelButton: 'swal2-cancel btn btn-secondary' // Estilo gris
},
preConfirm: () => {
const select = document.getElementById('swal-select-direccion');
if (!select.value) {
Swal.showValidationMessage('Debe seleccionar una dirección');
return false;
}
return response.direcciones[select.value]; // Devuelve la dirección seleccionada
}
}).then((result) => {
if (result.isConfirmed) {
const direccionSeleccionada = result.value;
$.post('/logistica/generateEnvio', {
pedido_id: response.pedido_id,
direccion: direccionSeleccionada
}, function (response) {
if (response.status) {
window.open(`${window.location.origin}/logistica/envio/${response.data.id_envio}`);
} else {
popErrorAlert(response.message);
}
}).fail(function (xhr, status, error) {
popErrorAlert(error);
});
}
});
}
});