396 lines
21 KiB
HTML
396 lines
21 KiB
HTML
<!-- Container Principal -->
|
|
<div x-data="modals" @keydown.escape="closeModal" class="bg-white rounded-lg shadow">
|
|
<div class="container mx-auto p-6 w-full ">
|
|
<h1 class="text-2xl font-bold mb-4">Lista de tarifas </h1>
|
|
<div class="justify-between w-full md:flex">
|
|
<p class="mb-4 text-sm">Gestión de tarifas.</p>
|
|
<!-- Campo de Búsqueda -->
|
|
<div class="mb-4 relative">
|
|
<input type="text" x-model="searchQuery" placeholder="Buscar..."
|
|
class="border border-gray-300 rounded w-full md:w-60 p-2 text-sm" @input="loadRegister()">
|
|
|
|
<!-- Contenedor para el botón o el ícono de carga -->
|
|
<div x-show="isLoading" class="absolute right-2 top-2 flex items-center">
|
|
<!-- SVG de cargando (puedes reemplazarlo con un botón si lo prefieres) -->
|
|
<svg class="animate-spin h-5 w-5 text-gray-500" xmlns="http://www.w3.org/2000/svg" fill="none"
|
|
viewBox="0 0 24 24">
|
|
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
|
|
</circle>
|
|
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"></path>
|
|
</svg>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Botón Nuevo registro -->
|
|
<div class="flex justify-end mb-4">
|
|
<button class="bg-[#8eb02f] text-white px-4 py-2 rounded text-sm"
|
|
x-on:click="addModal=true">Nuevo</button>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- Agregar el GIF de carga -->
|
|
<div x-show="loading" class="fixed inset-0 bg-gray-800 bg-opacity-75 flex z-50 justify-center items-center">
|
|
<img src="../img/loading.gif" alt="Cargando..." class="w-16 h-16" />
|
|
</div>
|
|
<div class="overflow-x-auto max-w-full">
|
|
<!-- Tabla de Registros -->
|
|
<table class=" table-auto w-full">
|
|
<thead class="text-sm text-left py-4 border-b border-gray-300">
|
|
<tr class="text-left font-semibold border-collapse">
|
|
<th class="py-2 px-4 border-b">Item</th>
|
|
<th class="py-2 px-4 border-b">Descripción</th>
|
|
<th class="py-2 px-4 border-b">Archivo</th>
|
|
<th class="py-2 px-4 border-b"></th>
|
|
<th class="py-2 px-4 border-b w-32"></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody class="text-gray-500 select-none text-sm ">
|
|
<!-- Mensaje de Carga Inicial -->
|
|
<tr class="hover:bg-gray-100" x-show="isLoading">
|
|
<td colspan="" class="py-2 px-4 border-b text-center w-full">
|
|
<div class="text-center mx-auto justify-center flex space-x-2">
|
|
<!-- SVG de cargando (puedes reemplazarlo con un botón si lo prefieres) -->
|
|
<svg class="animate-spin h-5 w-5 mx-1 text-gray-500" xmlns="http://www.w3.org/2000/svg"
|
|
fill="none" viewBox="0 0 24 24">
|
|
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor"
|
|
stroke-width="4">
|
|
</circle>
|
|
<path class="opacity-75" fill="currentColor"
|
|
d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z">
|
|
</path>
|
|
</svg>
|
|
Cargando registros...
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<!-- Itera sobre los roles -->
|
|
<template x-for="registro in registros" :key="registro.ID">
|
|
<tr class="hover:bg-gray-100" :data-role-id="registro.ID">
|
|
<td class="py-3 text-xs px-4 border-b" x-text="registro.ID"></td>
|
|
<td class="py-3 text-xs px-4 border-b" x-text="registro.description"></td>
|
|
<td class="py-3 text-xs px-4 border-b" x-text="registro.ruta"></td>
|
|
<td class="py-3 text-xs px-4 border-b">
|
|
<button class="bg-[#8eb02f] text-white px-4 py-2 rounded"
|
|
x-on:click="window.open(`${window.location.origin}/${registro.ruta}`, '_blank')">
|
|
Ver archivo
|
|
</button>
|
|
</td>
|
|
<td class="py-3 text-xs px-4 border-b">
|
|
<div class="flex justify-between gap-2">
|
|
|
|
<button
|
|
@click="viewModal = true; Description = registro.description; Ruta = registro.ruta;"
|
|
title="Ver">
|
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
|
|
stroke-width="1.5" stroke="currentColor" class="w-5 text-[#8eb02f]">
|
|
<path stroke-linecap="round" stroke-linejoin="round"
|
|
d="M2.036 12.322a1.012 1.012 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178Z" />
|
|
<path stroke-linecap="round" stroke-linejoin="round"
|
|
d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
|
|
</svg>
|
|
</button>
|
|
<button
|
|
@click="editModal = true; Id = registro.ID; Description = registro.description; Ruta = registro.ruta;"
|
|
title="Editar">
|
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
|
|
stroke-width="1.5" stroke="currentColor" class="w-5">
|
|
<path stroke-linecap="round" stroke-linejoin="round"
|
|
d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
|
|
</svg>
|
|
</button>
|
|
<button @click="showConfirmModal = true; deleteId = registro.ID;" title="Eliminar">
|
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
|
|
stroke-width="1.5" stroke="currentColor" class="w-5 text-red-500">
|
|
<path stroke-linecap="round" stroke-linejoin="round"
|
|
d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
|
|
<tr x-show="sinregistro">
|
|
<td colspan="3" class="py-2 px-4 border-b text-center">No hay registros disponibles.</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div>
|
|
<!-- Los botones de paginación se generarán aquí -->
|
|
<div id="pagination" class="flex justify-left my-4 text-sm">
|
|
</div>
|
|
<!-- Mostrar conteo de registros -->
|
|
<div id="recordCountDiv" class="text-sm text-gray-700 ml-4">
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Ver -->
|
|
<div x-cloak x-show="viewModal" x-transition
|
|
class="fixed inset-0 bg-gray-800 bg-opacity-75 flex z-40 justify-center items-center text-sm">
|
|
<div class="bg-white p-6 rounded shadow-lg max-w-sm w-full overflow-y-auto max-h-full">
|
|
<h2 class="text-xl font-bold mb-2">Ver registro</h2>
|
|
<p class="font-semibold" x-text="Description"></p>
|
|
<p class="mt-2" x-text="Ruta"></p>
|
|
<button class="mt-4 bg-[#8eb02f] text-white px-4 py-2 rounded"
|
|
x-on:click="viewModal = false">Cerrar</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Editar -->
|
|
<div x-cloak x-show="editModal" x-transition
|
|
class="fixed inset-0 bg-gray-800 bg-opacity-75 flex z-40 justify-center items-center text-sm">
|
|
<div class="bg-white p-6 rounded shadow-lg max-w-sm w-full overflow-y-auto max-h-full">
|
|
<h2 class="text-xl font-bold mb-2">Editar registro</h2>
|
|
<form @submit.prevent="submitEdit">
|
|
<label class="block mt-4 mb-2" for="editDescription">Descripción</label>
|
|
<textarea id="editDescription" x-model="Description" placeholder="Descripción"
|
|
class="border border-gray-300 rounded w-full p-2" required></textarea>
|
|
<label class="block mb-2" for="editRuta">Archivo</label>
|
|
<input type="file" id="editFile" @change="handleUpdateFileChange"
|
|
class="border border-gray-300 rounded w-full p-2" accept=".jpg,.png,.pdf,.doc,.docx">
|
|
<div class="mt-4 flex justify-between">
|
|
<button type="submit" class="bg-[#8eb02f] text-white px-4 py-2 rounded">Guardar Cambios</button>
|
|
<button type="button" class="bg-gray-600 text-white px-4 py-2 rounded"
|
|
x-on:click="editModal = false">Cancelar</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Añadir -->
|
|
<div x-cloak x-show="addModal" x-transition
|
|
class="fixed inset-0 bg-gray-800 bg-opacity-75 flex z-40 justify-center items-center text-sm">
|
|
<div class="bg-white p-6 rounded shadow-lg max-w-sm w-full overflow-y-auto max-h-full">
|
|
<h2 class="text-xl font-bold mb-2">Agregar registro</h2>
|
|
<form @submit.prevent="submitAdd">
|
|
<label class="block mt-4 mb-2" for="description">Descripción</label>
|
|
<textarea id="description" x-model="newDescription" placeholder="Descripción"
|
|
class="border border-gray-300 rounded w-full p-2" required></textarea>
|
|
<label class="block mb-2" for="file">Archivo</label>
|
|
<input type="file" id="file" @change="handleFileChange"
|
|
class="border border-gray-300 rounded w-full p-2" accept=".jpg,.png,.pdf,.doc,.docx" required>
|
|
<div class="mt-4 flex justify-between">
|
|
<button type="submit" class="bg-[#8eb02f] text-white px-4 py-2 rounded">Guardar registro</button>
|
|
<button type="button" class="bg-gray-600 text-white px-4 py-2 rounded"
|
|
x-on:click="closeModal">Cancelar</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Confirmar Eliminación -->
|
|
<div x-cloak x-show="showConfirmModal" x-transition
|
|
class="fixed inset-0 bg-gray-800 bg-opacity-75 flex z-40 justify-center items-center text-sm">
|
|
<div class="bg-white p-6 rounded shadow-lg max-w-sm w-full overflow-y-auto max-h-full">
|
|
<h2 class="text-xl font-bold mb-4">Confirmar Eliminación</h2>
|
|
<p>¿Estás seguro de que deseas eliminar este registro?</p>
|
|
<div class="mt-4 flex justify-end">
|
|
<button class="bg-red-500 text-white px-4 py-2 rounded mr-2" @click="deleteItem()">Eliminar</button>
|
|
<button @click="closeModal" class="bg-gray-600 text-white px-4 py-2 rounded">Cancelar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alpine.js Script -->
|
|
<script>
|
|
document.addEventListener('alpine:init', () => {
|
|
Alpine.data('modals', () => ({
|
|
addModal: false,
|
|
editModal: false,
|
|
viewModal: false,
|
|
showConfirmModal: false,
|
|
|
|
newDescription: '',
|
|
newRuta: '',
|
|
newFile: null, // Archivo para la carga
|
|
|
|
Description: '',
|
|
Ruta: '',
|
|
updateFile: null, // Archivo para la actualización
|
|
|
|
sinregistro: '',
|
|
Id: null,
|
|
deleteId: null,
|
|
loading: false,
|
|
totalRecords: 0,
|
|
isLoading: false,
|
|
registros: '',
|
|
|
|
init() {
|
|
this.loadRegister();
|
|
},
|
|
|
|
searchQuery: '',
|
|
|
|
loadRegister(page = 1) {
|
|
this.isLoading = true;
|
|
const search = this.searchQuery ? `&search=${this.searchQuery}` : '';
|
|
fetch(`/loadtarifas?page=${page}${search}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
this.totalRecords = data.total;
|
|
this.registros = data.registros;
|
|
this.totalPages = data.totalPages;
|
|
this.createPagination(this.totalPages, page);
|
|
this.sinregistro = data.registros.length === 0;
|
|
})
|
|
.catch(error => console.error('Error:', error))
|
|
.finally(() => {
|
|
this.isLoading = false;
|
|
});
|
|
},
|
|
|
|
createPagination(totalPages, currentPage) {
|
|
const paginationContainer = document.getElementById('pagination');
|
|
paginationContainer.innerHTML = '';
|
|
|
|
const paginationButtonsContainer = document.createElement('div');
|
|
paginationButtonsContainer.className = "md:flex mx-auto mb-2 md:justify-between items-center w-full";
|
|
|
|
const createButton = (text, isDisabled, onClick, additionalClasses = '') => {
|
|
const button = document.createElement('button');
|
|
button.textContent = text;
|
|
button.disabled = isDisabled;
|
|
button.className = `px-4 py-2 text-black bg-gray-200 ${additionalClasses} ${isDisabled ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-300'}`;
|
|
if (!isDisabled) {
|
|
button.onclick = onClick;
|
|
}
|
|
return button;
|
|
};
|
|
|
|
const leftContainer = document.createElement('div');
|
|
leftContainer.className = "md:flex text-center mb-2";
|
|
leftContainer.appendChild(createButton('Anterior', currentPage === 1, () => this.loadRegister(currentPage - 1), 'rounded-l-md'));
|
|
|
|
// Rango de páginas para mostrar
|
|
const maxVisibleButtons = 5; // Número máximo de botones a mostrar
|
|
const halfRange = Math.floor(maxVisibleButtons / 2);
|
|
let startPage = Math.max(1, currentPage - halfRange);
|
|
let endPage = Math.min(totalPages, currentPage + halfRange);
|
|
|
|
// Ajustar si estamos al inicio o al final del rango de páginas
|
|
if (currentPage <= halfRange) {
|
|
endPage = Math.min(totalPages, maxVisibleButtons);
|
|
}
|
|
if (currentPage + halfRange > totalPages) {
|
|
startPage = Math.max(1, totalPages - maxVisibleButtons + 1);
|
|
}
|
|
|
|
// Crear botones de paginación
|
|
for (let i = startPage; i <= endPage; i++) {
|
|
const pageButton = createButton(
|
|
i,
|
|
false,
|
|
() => this.loadRegister(i),
|
|
`mx-1 ${i === currentPage ? 'bg-black text-gray-50' : 'bg-white text-black'} rounded-md`
|
|
);
|
|
leftContainer.appendChild(pageButton);
|
|
}
|
|
|
|
leftContainer.appendChild(createButton('Siguiente', currentPage === totalPages, () => this.loadRegister(currentPage + 1), 'rounded-r-md'));
|
|
paginationButtonsContainer.appendChild(leftContainer);
|
|
|
|
const recordCountDiv = document.createElement('div');
|
|
const startRecord = (currentPage - 1) * 10 + 1;
|
|
const endRecord = Math.min(currentPage * 10, this.totalRecords);
|
|
recordCountDiv.className = "text-sm text-gray-700 text-center md:ml-auto";
|
|
recordCountDiv.innerHTML = `Mostrando ${startRecord} a ${endRecord} de ${this.totalRecords} registros`;
|
|
|
|
paginationButtonsContainer.appendChild(recordCountDiv);
|
|
paginationContainer.appendChild(paginationButtonsContainer);
|
|
},
|
|
|
|
closeModal() {
|
|
this.addModal = false;
|
|
this.editModal = false;
|
|
this.viewModal = false;
|
|
this.showConfirmModal = false;
|
|
this.newFile = null; // Reiniciar archivo
|
|
this.updateFile = null; // Reiniciar archivo
|
|
},
|
|
|
|
handleFileChange(event) {
|
|
this.newFile = event.target.files[0]; // Manejar el archivo seleccionado
|
|
},
|
|
|
|
handleUpdateFileChange(event) {
|
|
this.updateFile = event.target.files[0]; // Manejar el archivo seleccionado para la actualización
|
|
},
|
|
|
|
submitAdd() {
|
|
this.loading = true;
|
|
const formData = new FormData();
|
|
formData.append('description', this.newDescription);
|
|
if (this.newFile) formData.append('file', this.newFile); // Adjuntar el archivo si existe
|
|
|
|
fetch('/app/tarifas', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
this.newDescription = '';
|
|
this.newRuta = data.ruta; // Guardar la ruta del archivo en la respuesta
|
|
this.newFile = null; // Reinicia la variable de archivo
|
|
// Limpia manualmente el input de archivo en el DOM
|
|
document.getElementById('file').value = '';
|
|
this.closeModal();
|
|
this.loadRegister();
|
|
})
|
|
.catch(error => console.error('Error:', error))
|
|
.finally(() => {
|
|
this.loading = false;
|
|
});
|
|
},
|
|
|
|
submitEdit() {
|
|
this.loading = true;
|
|
const formData = new FormData();
|
|
formData.append('description', this.Description);
|
|
if (this.updateFile) formData.append('file', this.updateFile); // Adjuntar el archivo si existe
|
|
|
|
fetch(`/app/tarifas/${this.Id}`, {
|
|
method: 'PUT',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
this.Description = '';
|
|
this.Ruta = data.ruta; // Actualizar la ruta del archivo en la respuesta
|
|
this.updateFile = null; // Reinicia la variable de archivo
|
|
// Limpia manualmente el input de archivo en el DOM
|
|
document.getElementById('editFile').value = '';
|
|
this.closeModal();
|
|
this.loadRegister();
|
|
})
|
|
.catch(error => console.error('Error:', error))
|
|
.finally(() => {
|
|
this.loading = false;
|
|
});
|
|
},
|
|
|
|
deleteItem() {
|
|
this.loading = true;
|
|
fetch(`/app/tarifas/${this.deleteId}`, {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
.then(() => {
|
|
this.closeModal();
|
|
this.loadRegister();
|
|
})
|
|
.catch(error => console.error('Error:', error))
|
|
.finally(() => {
|
|
this.loading = false;
|
|
});
|
|
}
|
|
}));
|
|
});
|
|
</script>
|
|
|
|
</div> |