Files
2025-04-24 02:07:47 +00:00

450 lines
25 KiB
HTML
Executable File

<!-- 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 deberes y derechos de usuario</h1>
<div class="justify-between w-full md:flex">
<p class="mb-4 text-sm">Gestión de deberes y derechos de usuario</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()">
<div x-show="isLoading" class="absolute right-2 top-2 flex items-center">
<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>
<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>
<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">Título</th>
<th class="py-2 px-4 border-b">Descripción</th>
<th class="py-2 px-4 border-b">Imagen</th>
<th class="py-2 px-4 border-b">Archivo</th>
<th class="py-2 px-4 border-b">Botón de Texto</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>
<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.title"></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">
<button class="bg-[#8eb02f] text-white px-4 py-2 rounded"
x-on:click="window.open(`${window.location.origin}/${registro.imagen}`, '_blank')">
Ver imagen
</button>
</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.archivo}`, '_blank')">
Ver archivo
</button>
</td>
<td class="py-3 text-xs px-4 border-b" x-text="registro.text_button"></td>
<td class="py-3 text-xs px-4 border-b">
<div class="flex justify-between gap-2">
<button
@click="viewModal = true; Title = registro.title; Description = registro.description; Imagen = registro.imagen; Archivo = registro.archivo; Text_button = registro.text_button;"
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; Title = registro.title; Description = registro.description; Imagen = registro.imagen; Archivo = registro.archivo; Text_button = registro.text_button;"
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="7" class="py-2 px-4 border-b text-center">No hay registros disponibles.</td>
</tr>
</tbody>
</table>
</div>
<div>
<div id="pagination" class="flex justify-left my-4 text-sm"></div>
<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 deberes y derechos de usuario</h2>
<p class="font-semibold" x-text="Title"></p>
<p class="mt-2" x-text="Description"></p>
<p class="mt-2" x-text="Text_button"></p>
<button class="bg-[#8eb02f] text-white px-4 py-2 rounded"
x-on:click="window.open(`${window.location.origin}/${Imagen}`, '_blank')">
Ver imagen
</button>
<button class="bg-[#8eb02f] text-white px-4 py-2 rounded"
x-on:click="window.open(`${window.location.origin}/${Archivo}`, '_blank')">
Ver archivo
</button>
<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 deberes y derechos de usuario</h2>
<form @submit.prevent="submitEdit">
<label class="block mt-4 mb-2" for="editTitle">Título</label>
<input id="editTitle" x-model="Title" class="border border-gray-300 rounded w-full p-2" required>
<label class="block mt-4 mb-2" for="editDescription">Descripción</label>
<textarea id="editDescription" x-model="Description" class="border border-gray-300 rounded w-full p-2"
required></textarea>
<label class="block mt-4 mb-2" for="editImagen">Imagen</label>
<input type="file" id="editImagen" @change="handleUpdateFileChange($event, 'imagen')"
class="border border-gray-300 rounded w-full p-2" accept="image/*">
<label class="block mb-2" for="editArchivo">Archivo</label>
<input type="file" id="editArchivo" @change="handleUpdateFileChange($event, 'archivo')"
class="border border-gray-300 rounded w-full p-2" accept=".jpg,.png,.pdf,.doc,.docx">
<label class="block mt-4 mb-2">Botón de Texto</label>
<input type="text" x-model="Text_button" class="border border-gray-300 rounded w-full p-2" required>
<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 deberes y derechos de usuario</h2>
<form @submit.prevent="submitAdd">
<label class="block mt-4 mb-2" for="newTitle">Título</label>
<input id="title" x-model="newTitle" class="border border-gray-300 rounded w-full p-2" required>
<label class="block mt-4 mb-2" for="newDescription">Descripción</label>
<textarea id="description" x-model="newDescription" class="border border-gray-300 rounded w-full p-2"
required></textarea>
<label class="block mt-4 mb-2" for="newImagen">Imagen</label>
<input type="file" id="imagen" @change="handleFileChange($event, 'imagen')"
class="border border-gray-300 rounded w-full p-2" accept="image/*" required>
<label class="block mb-2" for="newArchivo">Archivo</label>
<input type="file" id="archivo" @change="handleFileChange($event, 'archivo')"
class="border border-gray-300 rounded w-full p-2" accept=".jpg,.png,.pdf,.doc,.docx" required>
<label class="block mt-4 mb-2">Botón de Texto</label>
<input type="text" id="text_button" x-model="newText_button"
class="border border-gray-300 rounded w-full p-2" required>
<div class="mt-4 flex justify-between">
<button type="submit" class="bg-[#8eb02f] text-white px-4 py-2 rounded">Guardar Publicación</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 esta 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,
newTitle: '',
newDescription: '',
newImagen: null, // Imagen para la carga
newArchivo: null, // Archivo para la carga
newText_button: '', // Botón de texto (como texto)
Title: '',
Description: '',
Imagen: '',
Archivo: '',
Text_button: '', // Botón de texto (como texto)
updateImagen: null, // Imagen para la actualización
updateArchivo: 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(`/loaddeberesusuario?page=${page}${search}`)
.then(response => response.json())
.then(data => {
console.log(data);
this.totalRecords = data.total;
this.registros = data.records;
this.totalPages = data.totalPages;
this.createPagination(this.totalPages, page);
this.sinregistro = data.records.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'));
for (let i = 1; i <= totalPages; 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.newImagen = null; // Reiniciar imagen
this.newArchivo = null; // Reiniciar archivo
this.updateImagen = null; // Reiniciar imagen de actualización
this.updateArchivo = null; // Reiniciar archivo de actualización
},
handleFileChange(event, field) {
if (field === 'imagen') {
this.newImagen = event.target.files[0];
} else if (field === 'archivo') {
this.newArchivo = event.target.files[0];
}
},
handleUpdateFileChange(event, field) {
if (field === 'imagen') {
this.updateImagen = event.target.files[0];
} else if (field === 'archivo') {
this.updateArchivo = event.target.files[0];
}
},
submitAdd() {
this.loading = true;
const formData = new FormData();
formData.append('title', this.newTitle);
formData.append('description', this.newDescription);
formData.append('text_button', this.newText_button); // Guardar el texto
if (this.newImagen) formData.append('imagen', this.newImagen); // Adjuntar la imagen si existe
if (this.newArchivo) formData.append('archivo', this.newArchivo); // Adjuntar el archivo si existe
fetch('/app/deberesusuario', { // Cambié la ruta a 'politicashseq'
method: 'POST',
body: formData
})
.then(response => response.json())
.then(() => {
this.newTitle = '';
this.newDescription = '';
this.newText_button = '';
this.newImagen = null;
this.newArchivo = null;
// Limpia los campos de archivo en el DOM
document.getElementById('imagen').value = ''; // Restablece el input de imagen
document.getElementById('archivo').value = ''; // Restablece el input de archivo
this.closeModal();
this.loadRegister();
})
.catch(error => console.error('Error:', error))
.finally(() => {
this.loading = false;
});
},
submitEdit() {
this.loading = true;
const formData = new FormData();
formData.append('title', this.Title);
formData.append('description', this.Description);
formData.append('text_button', this.Text_button); // Guardar el texto
// Adjuntar la imagen si existe, o usar la ruta de imagen
if (this.updateImagen) {
formData.append('imagen', this.updateImagen);
} else if (this.Imagen) {
formData.append('imagen', this.Imagen);
}
// Adjuntar el archivo si existe, o usar la ruta del archivo
if (this.updateArchivo) {
formData.append('archivo', this.updateArchivo);
} else if (this.Archivo) {
formData.append('archivo', this.Archivo);
}
fetch(`/app/deberesusuario/${this.Id}`, { // Cambié la ruta a 'politicashseq'
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(() => {
this.Title = '';
this.Description = '';
this.Text_button = '';
this.updateImagen = null;
this.updateArchivo = null;
// Limpia los campos de archivo en el DOM
document.getElementById('editImagen').value = ''; // Restablece el input de imagen
document.getElementById('editArchivo').value = ''; // Restablece el input de archivo
this.closeModal();
this.loadRegister();
})
.catch(error => console.error('Error:', error))
.finally(() => {
this.loading = false;
});
},
deleteItem() {
this.loading = true;
fetch(`/app/deberesusuario/${this.deleteId}`, { // Cambié la ruta a 'politicashseq'
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }
})
.then(() => {
this.closeModal();
this.loadRegister();
})
.catch(error => console.error('Error:', error))
.finally(() => {
this.loading = false;
});
}
}));
});
</script>
</div>