Actualizar configuracion.html
This commit is contained in:
@@ -8,136 +8,116 @@
|
|||||||
<script src="https://unpkg.com/lucide@latest"></script>
|
<script src="https://unpkg.com/lucide@latest"></script>
|
||||||
<style>
|
<style>
|
||||||
.fade-in { animation: fadeIn 0.3s ease-in-out; }
|
.fade-in { animation: fadeIn 0.3s ease-in-out; }
|
||||||
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
|
|
||||||
.scroller::-webkit-scrollbar { width: 6px; }
|
.scroller::-webkit-scrollbar { width: 6px; }
|
||||||
.scroller::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 4px; }
|
.scroller::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 4px; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-gray-50 text-gray-800 font-sans h-screen overflow-hidden">
|
<body class="bg-gray-50 text-gray-800 font-sans h-screen overflow-hidden flex">
|
||||||
|
|
||||||
<div class="flex h-full">
|
<div id="sidebar-container" class="h-full shrink-0"></div>
|
||||||
<div id="sidebar-container" class="h-full"></div>
|
|
||||||
|
|
||||||
<div class="flex-1 flex flex-col h-full relative">
|
<div class="flex-1 flex flex-col h-full relative min-w-0">
|
||||||
<div id="header-container"></div>
|
<div id="header-container"></div>
|
||||||
|
|
||||||
|
<main class="flex-1 flex flex-col overflow-hidden relative">
|
||||||
|
|
||||||
<main class="flex-1 overflow-hidden flex flex-col p-6 fade-in">
|
<div class="bg-gray-50 p-6 pb-0 z-20 shrink-0">
|
||||||
|
|
||||||
<h2 class="text-2xl font-bold text-gray-800 mb-6 flex items-center gap-2">
|
<h2 class="text-2xl font-bold text-gray-800 mb-6 flex items-center gap-2">
|
||||||
<i data-lucide="settings" class="text-blue-600"></i> Configuración Avanzada
|
<i data-lucide="settings" class="text-blue-600"></i> Configuración Avanzada
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<div class="flex border-b border-gray-200 mb-6 overflow-x-auto">
|
<div class="flex border-b border-gray-200 overflow-x-auto no-scrollbar">
|
||||||
<button onclick="showTab('zones')" id="tab-zones" class="tab-btn px-6 py-3 text-sm font-bold text-blue-600 border-b-2 border-blue-600 hover:bg-gray-50 transition whitespace-nowrap">
|
<button onclick="showTab('templates')" id="tab-templates" class="tab-btn px-6 py-3 text-sm font-bold text-blue-600 border-b-2 border-blue-600 hover:bg-gray-100 transition whitespace-nowrap">
|
||||||
<i data-lucide="map" class="inline w-4 h-4 mr-1"></i> Zonas de Actuación
|
|
||||||
</button>
|
|
||||||
<button onclick="showTab('templates')" id="tab-templates" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-50 transition whitespace-nowrap">
|
|
||||||
<i data-lucide="file-text" class="inline w-4 h-4 mr-1"></i> Plantillas
|
<i data-lucide="file-text" class="inline w-4 h-4 mr-1"></i> Plantillas
|
||||||
</button>
|
</button>
|
||||||
<button onclick="showTab('whatsapp')" id="tab-whatsapp" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-50 transition whitespace-nowrap">
|
<button onclick="showTab('whatsapp')" id="tab-whatsapp" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-100 transition whitespace-nowrap">
|
||||||
<i data-lucide="message-circle" class="inline w-4 h-4 mr-1"></i> WhatsApp
|
<i data-lucide="message-circle" class="inline w-4 h-4 mr-1"></i> WhatsApp
|
||||||
</button>
|
</button>
|
||||||
<button onclick="showTab('providers')" id="tab-providers" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-50 transition whitespace-nowrap">
|
<button onclick="showTab('providers')" id="tab-providers" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-100 transition whitespace-nowrap">
|
||||||
<i data-lucide="truck" class="inline w-4 h-4 mr-1"></i> Proveedores
|
<i data-lucide="truck" class="inline w-4 h-4 mr-1"></i> Proveedores
|
||||||
</button>
|
</button>
|
||||||
<button onclick="showTab('portal')" id="tab-portal" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-50 transition whitespace-nowrap">
|
<button onclick="showTab('portal')" id="tab-portal" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-100 transition whitespace-nowrap">
|
||||||
<i data-lucide="globe" class="inline w-4 h-4 mr-1"></i> Portal Cliente
|
<i data-lucide="globe" class="inline w-4 h-4 mr-1"></i> Portal Cliente
|
||||||
</button>
|
</button>
|
||||||
|
<button onclick="showTab('others')" id="tab-others" class="tab-btn px-6 py-3 text-sm font-medium text-gray-500 hover:text-blue-600 hover:bg-gray-100 transition whitespace-nowrap">
|
||||||
|
<i data-lucide="sliders" class="inline w-4 h-4 mr-1"></i> Otras Configuraciones
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex-1 overflow-hidden relative">
|
|
||||||
|
|
||||||
<div id="view-zones" class="tab-content h-full flex flex-col">
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 h-full">
|
|
||||||
<div class="bg-white rounded-xl shadow border border-gray-100 flex flex-col h-full overflow-hidden">
|
|
||||||
<div class="p-3 bg-gray-50 border-b border-gray-200 font-bold text-sm text-gray-700">1. Selecciona Provincia</div>
|
|
||||||
<div class="p-2 border-b"><input type="text" id="searchProv" placeholder="Filtrar..." class="w-full text-xs p-2 border rounded" onkeyup="filterProvinces()"></div>
|
|
||||||
<div id="listProvinces" class="flex-1 overflow-y-auto scroller p-2 space-y-1"></div>
|
|
||||||
</div>
|
|
||||||
<div class="bg-white rounded-xl shadow border border-gray-100 flex flex-col h-full overflow-hidden opacity-50 pointer-events-none transition-opacity" id="colZones">
|
|
||||||
<div class="p-3 bg-gray-50 border-b border-gray-200 font-bold text-sm text-gray-700 flex justify-between items-center">
|
|
||||||
<span>2. Zonas Definidas</span>
|
|
||||||
<button onclick="openZoneModal()" class="text-blue-600 hover:bg-blue-100 p-1 rounded font-bold flex items-center gap-1 text-xs transition-colors"><i data-lucide="plus" class="w-3 h-3"></i> Nueva</button>
|
|
||||||
</div>
|
|
||||||
<div id="listZones" class="flex-1 overflow-y-auto scroller p-2 space-y-2 text-sm">
|
|
||||||
<p class="text-center text-gray-400 mt-10">Selecciona una provincia</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="bg-white rounded-xl shadow border border-gray-100 flex flex-col h-full overflow-hidden opacity-50 pointer-events-none transition-opacity" id="colOperators">
|
|
||||||
<div class="p-3 bg-gray-50 border-b border-gray-200 font-bold text-sm text-gray-700 flex justify-between items-center">
|
|
||||||
<span>3. Asignar Operarios</span>
|
|
||||||
<button onclick="saveZoneAssignment()" class="bg-green-600 text-white text-xs px-3 py-1 rounded hover:bg-green-700 shadow transition-colors">Guardar</button>
|
|
||||||
</div>
|
|
||||||
<div id="listOperators" class="flex-1 overflow-y-auto scroller p-2 space-y-1">
|
|
||||||
<p class="text-center text-gray-400 mt-10">Selecciona una zona</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="view-templates" class="tab-content hidden h-full">
|
|
||||||
<div class="bg-white rounded-xl shadow p-8 text-center border border-gray-100">
|
|
||||||
<i data-lucide="file-text" class="w-12 h-12 text-gray-300 mx-auto mb-4"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-700">Gestor de Plantillas</h3>
|
|
||||||
<p class="text-gray-500 mb-4">Configura aquí los textos predefinidos.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="view-whatsapp" class="tab-content hidden h-full">
|
|
||||||
<div class="bg-white rounded-xl shadow p-8 text-center border border-gray-100">
|
|
||||||
<i data-lucide="message-circle" class="w-12 h-12 text-green-500 mx-auto mb-4"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-700">Conexión WhatsApp</h3>
|
|
||||||
<p class="text-gray-500 mb-4">Configuración de API.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="view-providers" class="tab-content hidden h-full">
|
|
||||||
<div class="bg-white rounded-xl shadow p-8 text-center border border-gray-100">
|
|
||||||
<i data-lucide="truck" class="w-12 h-12 text-gray-300 mx-auto mb-4"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-700">Proveedores</h3>
|
|
||||||
<p class="text-gray-500 mb-4">Gestión de proveedores.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="view-portal" class="tab-content hidden h-full">
|
|
||||||
<div class="bg-white rounded-xl shadow p-8 text-center border border-gray-100">
|
|
||||||
<i data-lucide="globe" class="w-12 h-12 text-blue-300 mx-auto mb-4"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-700">Portal del Cliente</h3>
|
|
||||||
<p class="text-gray-500 mb-4">Configuración de acceso.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="zoneModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center backdrop-blur-sm">
|
|
||||||
<div class="bg-white rounded-xl shadow-2xl w-full max-w-lg p-6 animate-slide-in flex flex-col max-h-[80vh]">
|
|
||||||
<h3 class="text-xl font-bold mb-4">Nueva Zona / Ruta</h3>
|
|
||||||
<input type="text" id="newZoneName" placeholder="Nombre de la zona (Ej: Ruta Bahía)" class="w-full border p-2 rounded mb-4">
|
|
||||||
|
|
||||||
<p class="text-xs font-bold text-gray-500 mb-2">Selecciona las Poblaciones incluidas:</p>
|
|
||||||
<div id="townsList" class="border p-3 rounded overflow-y-auto flex-1 grid grid-cols-2 gap-2 text-sm bg-gray-50">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="flex justify-end gap-2 mt-4 pt-4 border-t">
|
|
||||||
<button onclick="document.getElementById('zoneModal').classList.add('hidden')" class="px-4 py-2 text-gray-500 hover:text-gray-700">Cancelar</button>
|
|
||||||
<button onclick="createZone()" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 font-bold">Crear Zona</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
<div class="flex-1 overflow-hidden p-6 bg-gray-50">
|
||||||
|
|
||||||
|
<div id="view-templates" class="tab-content h-full fade-in">
|
||||||
|
<div class="bg-white rounded-xl shadow p-10 text-center border border-gray-100">
|
||||||
|
<i data-lucide="file-text" class="w-16 h-16 text-gray-200 mx-auto mb-4"></i>
|
||||||
|
<h3 class="text-xl font-bold text-gray-700">Gestor de Plantillas</h3>
|
||||||
|
<p class="text-gray-500 mt-2">Próximamente podrás editar tus textos aquí.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="view-whatsapp" class="tab-content hidden h-full fade-in">
|
||||||
|
<div class="bg-white rounded-xl shadow p-10 text-center border border-gray-100">
|
||||||
|
<i data-lucide="message-circle" class="w-16 h-16 text-green-100 mx-auto mb-4 text-green-500"></i>
|
||||||
|
<h3 class="text-xl font-bold text-gray-700">Conexión WhatsApp</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="view-providers" class="tab-content hidden h-full fade-in">
|
||||||
|
<div class="bg-white rounded-xl shadow p-10 text-center border border-gray-100">
|
||||||
|
<i data-lucide="truck" class="w-16 h-16 text-gray-200 mx-auto mb-4"></i>
|
||||||
|
<h3 class="text-xl font-bold text-gray-700">Proveedores</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="view-portal" class="tab-content hidden h-full fade-in">
|
||||||
|
<div class="bg-white rounded-xl shadow p-10 text-center border border-gray-100">
|
||||||
|
<i data-lucide="globe" class="w-16 h-16 text-blue-200 mx-auto mb-4"></i>
|
||||||
|
<h3 class="text-xl font-bold text-gray-700">Portal del Cliente</h3>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="view-others" class="tab-content hidden h-full fade-in flex flex-col">
|
||||||
|
|
||||||
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
<div class="bg-white rounded-xl shadow border border-gray-100 flex flex-col h-[500px]">
|
||||||
|
<div class="p-4 bg-gray-50 border-b border-gray-200 shrink-0">
|
||||||
|
<h3 class="font-bold text-gray-700 flex items-center gap-2">
|
||||||
|
<i data-lucide="shield" class="w-4 h-4 text-blue-600"></i> Compañías de Seguros
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="p-4 border-b border-gray-100 bg-white">
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<input type="text" id="newCompanyInput" placeholder="Nombre de la compañía..." class="flex-1 border rounded-lg px-3 py-2 text-sm outline-none focus:border-blue-500">
|
||||||
|
<button onclick="addCompany()" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-xs font-bold transition-colors">
|
||||||
|
Añadir
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="listCompanies" class="flex-1 overflow-y-auto scroller p-2 space-y-1">
|
||||||
|
<p class="text-center text-xs text-gray-400 mt-10">Cargando...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="bg-white rounded-xl shadow border border-gray-100 p-6 flex items-center justify-center text-gray-400 text-sm">
|
||||||
|
<p>Más configuraciones próximamente...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="toast" class="fixed bottom-5 right-5 bg-slate-800 text-white px-6 py-3 rounded-lg shadow-2xl transform translate-y-20 opacity-0 transition-all duration-300 z-50 flex items-center gap-3"><span id="toastMsg">Msg</span></div>
|
<div id="toast" class="fixed bottom-5 right-5 bg-slate-800 text-white px-6 py-3 rounded-lg shadow-2xl transform translate-y-20 opacity-0 transition-all duration-300 z-50 flex items-center gap-3"><span id="toastMsg">Msg</span></div>
|
||||||
|
|
||||||
<script src="js/layout.js"></script>
|
<script src="js/layout.js"></script>
|
||||||
<script>
|
<script>
|
||||||
let provinces = [], currentProvId = null, currentZoneId = null;
|
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
if (!localStorage.getItem("token")) window.location.href = "index.html";
|
if (!localStorage.getItem("token")) window.location.href = "index.html";
|
||||||
showTab('zones');
|
showTab('templates'); // Pestaña por defecto
|
||||||
loadProvinces();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function showTab(tabId) {
|
function showTab(tabId) {
|
||||||
@@ -151,182 +131,63 @@
|
|||||||
const btn = document.getElementById(`tab-${tabId}`);
|
const btn = document.getElementById(`tab-${tabId}`);
|
||||||
btn.classList.add('text-blue-600', 'border-b-2', 'border-blue-600', 'font-bold');
|
btn.classList.add('text-blue-600', 'border-b-2', 'border-blue-600', 'font-bold');
|
||||||
btn.classList.remove('text-gray-500', 'font-medium');
|
btn.classList.remove('text-gray-500', 'font-medium');
|
||||||
|
|
||||||
|
// Cargar datos si entramos en "Otras Configuraciones"
|
||||||
|
if(tabId === 'others') {
|
||||||
|
loadCompanies();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- LÓGICA DE ZONAS ---
|
// --- GESTIÓN DE COMPAÑÍAS ---
|
||||||
async function loadProvinces() {
|
async function loadCompanies() {
|
||||||
|
const list = document.getElementById('listCompanies');
|
||||||
|
list.innerHTML = '<p class="text-center text-xs text-gray-400 mt-10">Cargando...</p>';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_URL}/provinces`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
const res = await fetch(`${API_URL}/companies`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
provinces = data.provinces;
|
|
||||||
renderProvinces(provinces);
|
|
||||||
} catch(e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderProvinces(list) {
|
|
||||||
const cont = document.getElementById('listProvinces');
|
|
||||||
cont.innerHTML = "";
|
|
||||||
list.forEach(p => {
|
|
||||||
const div = document.createElement('div');
|
|
||||||
div.className = "p-2 hover:bg-blue-50 rounded cursor-pointer text-sm flex justify-between items-center group prov-item transition-colors";
|
|
||||||
div.innerHTML = `<span>${p.name}</span><i data-lucide="chevron-right" class="w-4 h-4 text-gray-300 group-hover:text-blue-500"></i>`;
|
|
||||||
div.onclick = () => selectProvince(p.id, div);
|
|
||||||
cont.appendChild(div);
|
|
||||||
});
|
|
||||||
lucide.createIcons();
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterProvinces() {
|
|
||||||
const term = document.getElementById('searchProv').value.toLowerCase();
|
|
||||||
renderProvinces(provinces.filter(p => p.name.toLowerCase().includes(term)));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function selectProvince(id, el) {
|
|
||||||
currentProvId = id;
|
|
||||||
document.querySelectorAll('.prov-item').forEach(d => d.classList.remove('bg-blue-100', 'font-bold', 'text-blue-700'));
|
|
||||||
el.classList.add('bg-blue-100', 'font-bold', 'text-blue-700');
|
|
||||||
|
|
||||||
document.getElementById('colZones').classList.remove('opacity-50', 'pointer-events-none');
|
|
||||||
document.getElementById('colOperators').classList.add('opacity-50', 'pointer-events-none');
|
|
||||||
document.getElementById('listOperators').innerHTML = '<p class="text-center text-gray-400 mt-10">Selecciona una zona</p>';
|
|
||||||
loadZones(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadZones(provId) {
|
|
||||||
const res = await fetch(`${API_URL}/zones?province_id=${provId}`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
|
||||||
const data = await res.json();
|
|
||||||
const list = document.getElementById('listZones');
|
|
||||||
list.innerHTML = "";
|
|
||||||
|
|
||||||
if(data.zones.length === 0) list.innerHTML = '<p class="text-center text-xs text-gray-400 p-4">No hay zonas definidas en esta provincia.</p>';
|
|
||||||
|
|
||||||
data.zones.forEach(z => {
|
|
||||||
const div = document.createElement('div');
|
|
||||||
div.className = "p-3 border border-gray-100 rounded hover:border-blue-300 hover:shadow-sm cursor-pointer bg-white zone-item transition-all";
|
|
||||||
let townsText = z.towns_names && z.towns_names.length > 0 ? z.towns_names.join(", ") : "Sin poblaciones asignadas";
|
|
||||||
|
|
||||||
div.innerHTML = `
|
list.innerHTML = "";
|
||||||
<div class="flex justify-between items-start mb-1">
|
if(data.companies.length === 0) {
|
||||||
<span class="font-bold text-gray-800 text-sm">${z.name}</span>
|
list.innerHTML = '<p class="text-center text-xs text-gray-400 mt-4">No hay compañías registradas.</p>';
|
||||||
${z.owner_id ? `<button onclick="deleteZone(event, ${z.id})" class="text-gray-400 hover:text-red-500 transition-colors p-1" title="Borrar Zona"><i data-lucide="trash-2" class="w-4 h-4"></i></button>` : ''}
|
return;
|
||||||
</div>
|
}
|
||||||
<p class="text-xs text-gray-500 truncate leading-relaxed bg-gray-50 p-1 rounded">${townsText}</p>
|
|
||||||
`;
|
data.companies.forEach(c => {
|
||||||
div.onclick = () => selectZone(z.id, div);
|
const div = document.createElement('div');
|
||||||
list.appendChild(div);
|
div.className = "p-3 border border-gray-100 rounded-lg bg-gray-50 flex justify-between items-center group hover:border-blue-200 transition-colors";
|
||||||
});
|
div.innerHTML = `
|
||||||
lucide.createIcons();
|
<span class="font-bold text-gray-700 text-sm">${c.name}</span>
|
||||||
|
<i data-lucide="building-2" class="w-4 h-4 text-gray-300"></i>
|
||||||
|
`;
|
||||||
|
list.appendChild(div);
|
||||||
|
});
|
||||||
|
lucide.createIcons();
|
||||||
|
} catch(e) { list.innerHTML = '<p class="text-red-500 text-xs text-center">Error al cargar</p>'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function openZoneModal() {
|
async function addCompany() {
|
||||||
if(!currentProvId) return;
|
const input = document.getElementById('newCompanyInput');
|
||||||
document.getElementById('newZoneName').value = "";
|
const name = input.value.trim();
|
||||||
|
if(!name) return;
|
||||||
const res = await fetch(`${API_URL}/provinces/${currentProvId}/towns`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
|
||||||
const data = await res.json();
|
|
||||||
const list = document.getElementById('townsList');
|
|
||||||
list.innerHTML = "";
|
|
||||||
|
|
||||||
if(data.towns.length === 0) list.innerHTML = '<p class="col-span-2 text-center text-gray-400 py-4">No hay poblaciones disponibles para esta provincia.</p>';
|
|
||||||
|
|
||||||
data.towns.forEach(t => {
|
|
||||||
list.innerHTML += `<label class="flex items-center gap-2 p-1.5 hover:bg-blue-50 rounded cursor-pointer"><input type="checkbox" value="${t.id}" class="town-check text-blue-600 rounded"> <span class="truncate text-gray-700">${t.name}</span></label>`;
|
|
||||||
});
|
|
||||||
document.getElementById('zoneModal').classList.remove('hidden');
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createZone() {
|
|
||||||
const name = document.getElementById('newZoneName').value;
|
|
||||||
const checks = document.querySelectorAll('.town-check:checked');
|
|
||||||
const townIds = Array.from(checks).map(c => c.value);
|
|
||||||
|
|
||||||
if(!name) { alert("Debes poner un nombre a la zona"); return; }
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_URL}/zones`, {
|
const res = await fetch(`${API_URL}/companies`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||||
body: JSON.stringify({ province_id: currentProvId, name, town_ids: townIds })
|
body: JSON.stringify({ name: name })
|
||||||
});
|
});
|
||||||
if(res.ok) {
|
if(res.ok) {
|
||||||
showToast("Zona creada correctamente");
|
showToast("Compañía añadida");
|
||||||
document.getElementById('zoneModal').classList.add('hidden');
|
input.value = "";
|
||||||
loadZones(currentProvId);
|
loadCompanies();
|
||||||
} else { showToast("Error al crear zona", true); }
|
} else { showToast("Error al añadir", true); }
|
||||||
} catch(e) { showToast("Error de conexión", true); }
|
} catch(e) { showToast("Error de conexión", true); }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteZone(event, id) {
|
|
||||||
event.stopPropagation(); // Evita seleccionar la zona al borrar
|
|
||||||
if(!confirm("¿Estás seguro de borrar esta zona? Esta acción no se puede deshacer.")) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/zones/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
|
||||||
if(res.ok) {
|
|
||||||
showToast("Zona eliminada");
|
|
||||||
loadZones(currentProvId);
|
|
||||||
// Resetear panel de operarios si estaba seleccionada esa zona
|
|
||||||
if(currentZoneId === id) {
|
|
||||||
document.getElementById('colOperators').classList.add('opacity-50', 'pointer-events-none');
|
|
||||||
document.getElementById('listOperators').innerHTML = '<p class="text-center text-gray-400 mt-10">Selecciona una zona</p>';
|
|
||||||
}
|
|
||||||
} else { showToast("No se pudo borrar", true); }
|
|
||||||
} catch(e) { showToast("Error de conexión", true); }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function selectZone(id, el) {
|
|
||||||
currentZoneId = id;
|
|
||||||
document.querySelectorAll('.zone-item').forEach(d => d.classList.remove('border-blue-500', 'bg-blue-50', 'ring-1', 'ring-blue-500'));
|
|
||||||
el.classList.add('border-blue-500', 'bg-blue-50', 'ring-1', 'ring-blue-500');
|
|
||||||
document.getElementById('colOperators').classList.remove('opacity-50', 'pointer-events-none');
|
|
||||||
loadOperatorsForZone(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadOperatorsForZone(zoneId) {
|
|
||||||
const list = document.getElementById('listOperators');
|
|
||||||
list.innerHTML = '<p class="text-center text-xs text-gray-400 p-4">Cargando operarios...</p>';
|
|
||||||
|
|
||||||
const resOps = await fetch(`${API_URL}/operators`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
|
||||||
const dataOps = await resOps.json();
|
|
||||||
|
|
||||||
const resAssigned = await fetch(`${API_URL}/zones/${zoneId}/operators`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
|
||||||
const dataAssigned = await resAssigned.json();
|
|
||||||
const assignedSet = new Set(dataAssigned.assignedIds);
|
|
||||||
|
|
||||||
list.innerHTML = "";
|
|
||||||
|
|
||||||
if(dataOps.operators.length === 0) {
|
|
||||||
list.innerHTML = '<p class="text-center text-xs text-gray-400 p-4">No hay operarios registrados en el sistema.</p>';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dataOps.operators.forEach(op => {
|
|
||||||
const checked = assignedSet.has(op.id) ? 'checked' : '';
|
|
||||||
list.innerHTML += `
|
|
||||||
<label class="flex items-center p-2.5 hover:bg-gray-50 rounded cursor-pointer border-b border-gray-50 last:border-0 transition-colors">
|
|
||||||
<input type="checkbox" value="${op.id}" class="op-zone-check w-4 h-4 text-green-600 rounded mr-3 focus:ring-green-500" ${checked}>
|
|
||||||
<span class="text-sm font-medium text-gray-700">${op.full_name}</span>
|
|
||||||
</label>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function saveZoneAssignment() {
|
|
||||||
const checkboxes = document.querySelectorAll('.op-zone-check:checked');
|
|
||||||
const ids = Array.from(checkboxes).map(c => c.value);
|
|
||||||
try {
|
|
||||||
await fetch(`${API_URL}/zones/${currentZoneId}/assign`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
|
||||||
body: JSON.stringify({ operator_ids: ids })
|
|
||||||
});
|
|
||||||
showToast("Asignación guardada correctamente ✅");
|
|
||||||
} catch(e) { showToast("Error al guardar asignación", true); }
|
|
||||||
}
|
|
||||||
|
|
||||||
function showToast(msg, isError = false) {
|
function showToast(msg, isError = false) {
|
||||||
const t = document.getElementById('toast'), m = document.getElementById('toastMsg');
|
const t = document.getElementById('toast'), m = document.getElementById('toastMsg');
|
||||||
t.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-2xl transition-all duration-300 z-50 flex items-center gap-3 ${isError ? 'bg-red-600' : 'bg-slate-800'} text-white`;
|
t.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-xl transition-all duration-300 z-50 flex items-center gap-3 ${isError ? 'bg-red-600' : 'bg-slate-800'} text-white font-medium`;
|
||||||
m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0');
|
m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0');
|
||||||
setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000);
|
setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user