Files
soft_usite/resources/views/conx_db.html
T

471 lines
24 KiB
HTML
Executable File

<div x-data="app" class="bg-white rounded-lg shadow">
<div x-show="loading" class="fixed inset-0 bg-gray-800 bg-opacity-75 flex justify-center items-center z-50">
<img src="../img/loading.gif" alt="Cargando..." class="w-16 h-16" />
</div>
<div class="container mx-auto p-6 w-full">
<div class="justify-between items-center w-full md:flex">
<div>
<h1 class="text-2xl font-bold mb-2">Lista de conexiones DB</h1>
<p class="mb-4 text-sm">Gestión de conexiones DB.</p>
</div>
<div class="mb-4 md:mb-0 relative">
<input type="text" placeholder="Buscar..." class="border border-gray-300 rounded p-2 w-full"
x-model="search" @input.debounce.500ms="loadData()" />
</div>
<div class="flex items-center justify-end mb-4 gap-2 md:mb-0">
<button @click="addModal = true" class="bg-[#8eb02f] text-white px-4 py-2 rounded text-sm">Nuevo</button>
</div>
</div>
<div class="overflow-x-auto">
<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-3 border-b">Puerto</th>
<th class="py-2 px-3 border-b">Usuario</th>
<th class="py-2 px-3 border-b">Password</th>
<th class="py-2 px-3 border-b">Tipo DB</th>
<th class="py-2 px-3 border-b">Servidor</th>
<th class="py-2 px-3 border-b"></th>
</tr>
</thead>
<tbody class="text-gray-500 select-none text-sm">
<template x-for="data in datos" :key="data.id">
<tr class="hover:bg-gray-100">
<td class="py-3 text-xs px-3 border-b" x-text="data.puerto"></td>
<td class="py-3 text-xs px-3 border-b" x-text="data.usuario"></td>
<td class="py-3 text-xs px-3 border-b" x-text="data.password"></td>
<td class="py-3 text-xs px-3 border-b" x-text="tipos.find(ts => ts.ID == data.tipo_db_id)?.nombre || '-'"></td>
<td class="py-3 text-xs px-3 border-b">
<span x-text="(servidores.find(serv => serv.ID == data.servidor_id)?.nombre ?? '-') + ' - ' + (servidores.find(serv => serv.ID == data.servidor_id)?.ip_servidor ?? '')"></span>
</td>
<td class="py-3 text-xs px-3 border-b w-24">
<div class="flex items-center gap-2">
<button @click="openViewModal(data)" 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="openEditModal(data)" 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="openDeleteModal(data)" 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>
<!-- Botón Query Runner -->
<a :href="'/app/query-runner?conx_db_id=' + data.ID" title="Abrir editor SQL"
class="inline-flex items-center justify-center w-5 h-5 text-blue-500 hover:text-blue-700 transition">
<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="M17.25 6.75 22.5 12l-5.25 5.25m-10.5 0L1.5 12l5.25-5.25m7.5-3-4.5 16.5" />
</svg>
</a>
</div>
</td>
</tr>
</template>
</tbody>
</table>
</div>
<div class="flex justify-start items-center gap-2 p-4 text-sm">
<button @click="page = Math.max(1, page - 1); loadData()" :disabled="page === 1"
class="px-4 py-2 text-black bg-gray-200 rounded-l-md opacity-50 disabled:cursor-not-allowed">
Anterior
</button>
<template x-for="pageNum in paginatedPages" :key="pageNum">
<button @click="goToPage(pageNum)"
class="px-4 py-2 text-black bg-gray-200 bg-black text-gray-50 rounded-md hover:bg-gray-300"
:class="{ 'bg-black text-white': pageNum === page }" x-text="pageNum"></button>
</template>
<button @click="page += 1; loadData()" :disabled="datos.length < limit" :disabled="page === totalPages"
class="px-4 py-2 text-black bg-gray-200 rounded-r-md opacity-50 disabled:cursor-not-allowed">
Siguiente
</button>
</div>
</div>
<!-- Modales -->
<div x-show="viewModal" class="fixed inset-0 overflow-auto bg-gray-800 bg-opacity-75 flex justify-center items-center text-sm z-40">
<div class="bg-white rounded-lg shadow-lg p-6 w-11/12 max-w-lg">
<h2 class="text-xl font-bold mb-4">Ver conexión DB</h2>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Puerto:</label>
<input type="text" x-model="selectedItem.puerto" disabled class="border border-gray-300 rounded p-2 w-full" placeholder="Puerto" />
</div>
<div class="w-full">
<label class="block mb-2">Usuario:</label>
<input type="text" x-model="selectedItem.usuario" disabled class="border border-gray-300 rounded p-2 w-full" placeholder="Usuario" />
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Tipo db:</label>
<select x-model="selectedItem.tipo_db_id" disabled class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un tipo</option>
<template x-for="tipo in tipos" :key="tipo.ID">
<option :value="tipo.ID" x-text="`${tipo.nombre}`"></option>
</template>
</select>
</div>
<div class="w-full">
<label class="block mb-2">Servidor:</label>
<select x-model="selectedItem.servidor_id" disabled class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un servidor</option>
<template x-for="servidor in servidores" :key="servidor.ID">
<option :value="servidor.ID" x-text="`${servidor.nombre} - ${servidor.ip_servidor}`"></option>
</template>
</select>
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Password:</label>
<input type="text" x-model="selectedItem.password" disabled class="border border-gray-300 rounded p-2 w-full" placeholder="Password" />
</div>
</div>
<div class="flex justify-end gap-2 text-sm">
<button @click="viewModal = false" class="px-4 py-2 bg-gray-600 text-white rounded">Cerrar</button>
</div>
</div>
</div>
<div x-show="editModal" class="fixed inset-0 overflow-auto bg-gray-800 bg-opacity-75 flex justify-center items-center text-sm z-40">
<div class="bg-white rounded-lg shadow-lg p-6 w-11/12 max-w-lg">
<h2 class="text-xl font-bold mb-4">Editar conexión DB</h2>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Puerto:</label>
<input type="text" x-model="selectedItem.puerto" class="border border-gray-300 rounded p-2 w-full" placeholder="Puerto" />
</div>
<div class="w-full">
<label class="block mb-2">Usuario:</label>
<input type="text" x-model="selectedItem.usuario" class="border border-gray-300 rounded p-2 w-full" placeholder="Usuario" />
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Tipo db:</label>
<select x-model="selectedItem.tipo_db_id" class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un tipo</option>
<template x-for="tipo in tipos" :key="tipo.ID">
<option :value="tipo.ID" x-text="`${tipo.nombre}`"></option>
</template>
</select>
</div>
<div class="w-full">
<label class="block mb-2">Servidor:</label>
<select x-model="selectedItem.servidor_id" class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un servidor</option>
<template x-for="servidor in servidores" :key="servidor.ID">
<option :value="servidor.ID" x-text="`${servidor.nombre} - ${servidor.ip_servidor}`"></option>
</template>
</select>
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Password:</label>
<input type="text" x-model="selectedItem.password" class="border border-gray-300 rounded p-2 w-full" placeholder="Password" />
</div>
</div>
<div class="flex justify-between gap-2 text-sm">
<button @click="updateItem()" class="px-4 py-2 bg-[#8eb02f] text-white rounded">Actualizar</button>
<button @click="closeModals()" class="px-4 py-2 bg-gray-600 text-white rounded">Cerrar</button>
</div>
</div>
</div>
<div x-show="addModal" class="fixed inset-0 overflow-auto bg-gray-800 bg-opacity-75 flex justify-center items-center text-sm z-40">
<div class="bg-white rounded-lg shadow-lg p-6 w-11/12 max-w-lg">
<h2 class="text-xl font-bold mb-4">Añadir conexión DB</h2>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Puerto:</label>
<input type="text" x-model="selectedItem.puerto" class="border border-gray-300 rounded p-2 w-full" placeholder="Puerto" />
</div>
<div class="w-full">
<label class="block mb-2">Usuario:</label>
<input type="text" x-model="selectedItem.usuario" class="border border-gray-300 rounded p-2 w-full" placeholder="Usuario" />
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Tipo db:</label>
<select x-model="selectedItem.tipo_db_id" class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un tipo</option>
<template x-for="tipo in tipos" :key="tipo.ID">
<option :value="tipo.ID" x-text="`${tipo.nombre}`"></option>
</template>
</select>
</div>
<div class="w-full">
<label class="block mb-2">Servidor:</label>
<select x-model="selectedItem.servidor_id" class="border border-gray-300 rounded p-2 w-full">
<option value="">Selecciona un servidor</option>
<template x-for="servidor in servidores" :key="servidor.ID">
<option :value="servidor.ID" x-text="`${servidor.nombre} - ${servidor.ip_servidor}`"></option>
</template>
</select>
</div>
</div>
<div class="flex flex-col sm:flex-row justify-between gap-4 mb-4">
<div class="w-full">
<label class="block mb-2">Password:</label>
<input type="text" x-model="selectedItem.password" class="border border-gray-300 rounded p-2 w-full" placeholder="Password" />
</div>
</div>
<div class="flex justify-between gap-2 text-sm">
<button @click="addItem()" class="px-4 py-2 bg-[#8eb02f] text-white rounded">Guardar</button>
<button @click="addModal = false" class="px-4 py-2 bg-gray-600 text-white rounded">Cancelar</button>
</div>
</div>
</div>
<div x-show="deleteModal"
class="fixed inset-0 overflow-auto bg-gray-800 bg-opacity-75 flex justify-center items-center z-40">
<div class="bg-white rounded-lg shadow-lg p-6 w-11/12 max-w-lg text-sm">
<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="flex mt-4 justify-end gap-2 text-sm">
<button @click="deleteItem()" class="px-4 py-2 bg-red-500 text-white rounded">Eliminar</button>
<button @click="closeModals()" class="bg-gray-600 text-white px-4 py-2 rounded">Cancelar</button>
</div>
</div>
</div>
</div>
<script>
function app() {
return {
datos: [],
tipos: [],
servidores: [],
loading: false,
limit: 10,
page: 1,
search: '',
totalPages: 0,
paginatedPages: [],
// modales
viewModal: false,
addModal: false,
editModal: false,
deleteModal: false,
selectedItem: {},
errors: {},
init() {
this.loadData();
this.loadServidor();
this.loadTipoDb();
},
loadData() {
if (this.search !== '') {
this.page = 1;
this.limit = 100;
} else {
this.limit = 10;
}
this.setLoading(true);
axios.get('/app/loadconexiondb', {
params: {
page: this.page,
limit: this.limit,
search: this.search
}
}).then(response => {
this.datos = response.data.registros.map(data => ({
id: data.ID,
puerto: data.puerto,
usuario: data.usuario,
password: data.password,
tipo_db_id: data.tipo_db_id,
servidor_id: data.servidor_id,
created_at: data.CreatedAt,
}));
this.totalPages = response.data.totalPages;
this.calculatePaginatedPages();
}).catch(error => {
console.error('Error al obtener datos:', error);
}).finally(() => {
this.setLoading(false);
});
},
loadServidor() {
axios.get('/app/loadservidorselect')
.then(response => {
this.servidores = response.data.registros;
})
.catch(error => {
console.error('Error al cargar servidores:', error);
});
},
loadTipoDb() {
axios.get('/app/loadtipodb')
.then(response => {
this.tipos = response.data.registros;
})
.catch(error => {
console.error('Error al cargar tipos de db:', error);
});
},
setSelectedItem(data) {
this.selectedItem = { ...data };
this.errors = {};
},
resetSelectedItem() {
this.selectedItem = {};
this.errors = {};
},
calculatePaginatedPages() {
const maxVisiblePages = 5;
const startPage = Math.max(1, this.page - Math.floor(maxVisiblePages / 2));
const endPage = Math.min(this.totalPages, startPage + maxVisiblePages - 1);
this.paginatedPages = Array.from({ length: endPage - startPage + 1 }, (_, i) => i + startPage);
},
goToPage(page) {
this.page = page;
this.loadData();
},
setLoading(value) {
this.loading = value;
},
openViewModal(data) {
this.setSelectedItem(data);
this.viewModal = true;
},
openEditModal(data) {
this.setSelectedItem(data);
this.editModal = true;
},
deleteItem() {
this.setLoading(true);
axios.delete(`/app/conexiondb/${this.selectedItem.id}`)
.then(() => {
alert("Registro eliminado exitosamente.");
this.resetSelectedItem();
this.deleteModal = false;
this.loadData();
})
.catch(error => console.error("Error al eliminar el registro:", error))
.finally(() => this.setLoading(false));
},
openDeleteModal(data) {
this.selectedItem.id = data.id;
this.deleteModal = true;
},
closeModals() {
this.viewModal = false;
this.addModal = false;
this.editModal = false;
this.deleteModal = false;
this.resetSelectedItem();
},
addItem() {
if (this.selectedItem.tipo_db_id) {
this.selectedItem.tipo_db_id = parseInt(this.selectedItem.tipo_db_id);
} else {
this.selectedItem.tipo_db_id = null;
}
if (this.selectedItem.servidor_id) {
this.selectedItem.servidor_id = parseInt(this.selectedItem.servidor_id);
} else {
this.selectedItem.servidor_id = null;
}
this.setLoading(true);
axios.post('/app/conexiondb', this.selectedItem)
.then(response => {
alert("Conexion Db añadida exitosamente.");
this.resetSelectedItem();
this.addModal = false;
this.loadData();
})
.catch(error => {
alert("Error al agregar la conexion Db.", error);
console.error("Error al añadir la conexion Db:", error);
})
.finally(() => {
this.setLoading(false);
});
},
updateItem() {
if (this.selectedItem.tipo_db_id) {
this.selectedItem.tipo_db_id = parseInt(this.selectedItem.tipo_db_id);
} else {
this.selectedItem.tipo_db_id = null;
}
if (this.selectedItem.servidor_id) {
this.selectedItem.servidor_id = parseInt(this.selectedItem.servidor_id);
} else {
this.selectedItem.servidor_id = null;
}
const dataToSend = {
puerto: this.selectedItem.puerto,
usuario: this.selectedItem.usuario,
password: this.selectedItem.password,
tipo_db_id: this.selectedItem.tipo_db_id,
servidor_id: this.selectedItem.servidor_id,
};
this.setLoading(true);
axios.put(`/app/conexiondb/${this.selectedItem.id}`, dataToSend)
.then(response => {
alert("Conexion Db actualizada exitosamente.");
this.resetSelectedItem();
this.editModal = false;
this.loadData();
})
.catch(error => {
console.error("Error al actualizar la conexion Db:", error);
})
.finally(() => {
this.setLoading(false);
});
}
}
}
</script>