Añadir servicios.html

This commit is contained in:
2026-02-08 12:05:22 +00:00
parent 4b44d4a175
commit f937d03ca3

308
servicios.html Normal file
View File

@@ -0,0 +1,308 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nuevo Servicio - IntegraRepara</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<style>.fade-in { animation: fadeIn 0.3s ease-in-out; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }</style>
</head>
<body class="bg-gray-50 text-gray-800 font-sans antialiased">
<div class="flex h-screen overflow-hidden">
<div id="sidebar-container" class="h-full"></div>
<div class="flex-1 flex flex-col overflow-hidden relative">
<div id="header-container"></div>
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 p-6 fade-in">
<div class="max-w-5xl mx-auto">
<h2 class="text-2xl font-bold text-gray-800 mb-6 flex items-center gap-2">
<i data-lucide="clipboard-list" class="text-blue-600"></i> Nuevo Servicio
</h2>
<form onsubmit="createService(event)" class="space-y-6">
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
<h3 class="text-lg font-bold text-gray-700 mb-4 border-b pb-2">Datos del Cliente</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Teléfono (Buscador) *</label>
<div class="relative">
<input type="tel" id="sPhone" required placeholder="600..."
class="w-full pl-10 pr-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none font-medium"
onblur="searchClientByPhone()">
<i data-lucide="phone" class="absolute left-3 top-2.5 w-4 h-4 text-gray-400"></i>
</div>
<p class="text-xs text-blue-500 mt-1 hidden" id="clientFoundMsg">¡Cliente encontrado!</p>
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Nombre Completo *</label>
<input type="text" id="sName" required class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div class="md:col-span-2">
<label class="block text-xs font-bold text-gray-600 mb-1">Dirección y Población *</label>
<div class="flex gap-2">
<input type="text" id="sAddress" required placeholder="C/ Ejemplo 123, Madrid" class="flex-1 px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none">
<select id="sAddressSelect" class="hidden w-1/3 border rounded-lg text-sm bg-gray-50" onchange="selectAddress(this.value)">
<option value="">Direcciones guardadas...</option>
</select>
</div>
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Email (Opcional)</label>
<input type="email" id="sEmail" class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none">
</div>
</div>
</div>
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
<h3 class="text-lg font-bold text-gray-700 mb-4 border-b pb-2">Detalles del Servicio</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-4">
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Fecha</label>
<input type="date" id="sDate" class="w-full px-3 py-2 border rounded-lg outline-none">
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Hora</label>
<input type="time" id="sTime" class="w-full px-3 py-2 border rounded-lg outline-none">
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Duración Est.</label>
<select id="sDuration" class="w-full px-3 py-2 border rounded-lg bg-white outline-none">
<option value="15">15 min</option>
<option value="30" selected>30 min</option>
<option value="45">45 min</option>
<option value="60">1 hora</option>
<option value="90">1h 30m</option>
<option value="120">2 horas</option>
</select>
</div>
<div class="flex items-end pb-2">
<label class="flex items-center space-x-2 cursor-pointer bg-red-50 px-4 py-2 rounded-lg border border-red-100 w-full hover:bg-red-100 transition">
<input type="checkbox" id="sUrgent" class="form-checkbox text-red-600 w-5 h-5">
<span class="text-sm font-bold text-red-700">¡Es Urgente!</span>
</label>
</div>
</div>
<div class="mb-4">
<label class="block text-xs font-bold text-gray-600 mb-1">Detalles / Avería</label>
<textarea id="sDesc" rows="2" class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none" placeholder="Describe qué hay que hacer..."></textarea>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Notas Internas (Solo oficina)</label>
<textarea id="sNotesInternal" rows="2" class="w-full px-3 py-2 border bg-yellow-50 rounded-lg outline-none text-sm"></textarea>
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Notas para Cliente (Se verán en el parte)</label>
<textarea id="sNotesClient" rows="2" class="w-full px-3 py-2 border rounded-lg outline-none text-sm"></textarea>
</div>
</div>
</div>
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
<div class="flex items-center justify-between mb-4 border-b pb-2">
<h3 class="text-lg font-bold text-gray-700">Compañía / Aseguradora</h3>
<label class="flex items-center space-x-2 cursor-pointer">
<span class="text-sm text-gray-600">¿Es de compañía?</span>
<input type="checkbox" id="sIsCompany" class="form-checkbox text-blue-600 w-5 h-5" onchange="toggleCompanyFields()">
</label>
</div>
<div id="companyFields" class="hidden grid grid-cols-1 md:grid-cols-2 gap-4 bg-gray-50 p-4 rounded-lg">
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Seleccionar Compañía</label>
<div class="flex gap-2">
<select id="sCompanyId" class="w-full px-3 py-2 border rounded-lg bg-white outline-none">
<option value="">-- Seleccionar --</option>
</select>
<button type="button" onclick="quickAddCompany()" class="bg-slate-800 text-white p-2 rounded hover:bg-slate-700"><i data-lucide="plus" class="w-4 h-4"></i></button>
</div>
</div>
<div>
<label class="block text-xs font-bold text-gray-600 mb-1">Número de Expediente / Servicio</label>
<input type="text" id="sCompanyRef" class="w-full px-3 py-2 border rounded-lg outline-none" placeholder="Ej: EXP-2024-001">
</div>
</div>
</div>
<div class="pt-4">
<button type="submit" id="btnSave" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-4 rounded-xl shadow-lg shadow-blue-500/30 text-lg transition-all flex justify-center items-center gap-2">
<i data-lucide="save" class="w-6 h-6"></i> Crear Servicio
</button>
</div>
</form>
</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>
<script src="js/layout.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const token = localStorage.getItem("token");
if (!token) window.location.href = "index.html";
// Establecer fecha y hora actuales
document.getElementById('sDate').valueAsDate = new Date();
const now = new Date();
document.getElementById('sTime').value = now.toTimeString().slice(0,5);
loadCompanies();
});
// --- LÓGICA DE CLIENTE ---
async function searchClientByPhone() {
const phone = document.getElementById('sPhone').value;
if(phone.length < 8) return;
try {
const res = await fetch(`${API_URL}/clients/search?phone=${phone}`, {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
});
const data = await res.json();
if (data.ok && data.client) {
const c = data.client;
document.getElementById('sName').value = c.full_name;
document.getElementById('sEmail').value = c.email || "";
document.getElementById('clientFoundMsg').classList.remove('hidden');
// Gestionar direcciones
const addresses = c.addresses || [];
if (addresses.length > 0) {
// Ponemos la última usada en el input
document.getElementById('sAddress').value = addresses[addresses.length - 1];
// Llenamos el select
const select = document.getElementById('sAddressSelect');
select.innerHTML = '<option value="">Otras direcciones...</option>';
addresses.forEach(addr => {
select.innerHTML += `<option value="${addr}">${addr}</option>`;
});
if (addresses.length > 1) select.classList.remove('hidden');
}
} else {
document.getElementById('clientFoundMsg').classList.add('hidden');
document.getElementById('sAddressSelect').classList.add('hidden');
}
} catch (e) { console.error(e); }
}
function selectAddress(val) {
if(val) document.getElementById('sAddress').value = val;
}
// --- LÓGICA COMPAÑÍA ---
function toggleCompanyFields() {
const isChecked = document.getElementById('sIsCompany').checked;
const div = document.getElementById('companyFields');
if(isChecked) {
div.classList.remove('hidden');
loadCompanies();
} else {
div.classList.add('hidden');
}
}
async function loadCompanies() {
try {
const res = await fetch(`${API_URL}/companies`, {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
});
const data = await res.json();
if (data.ok) {
const sel = document.getElementById('sCompanyId');
sel.innerHTML = '<option value="">-- Seleccionar --</option>';
data.companies.forEach(c => {
sel.innerHTML += `<option value="${c.id}">${c.name}</option>`;
});
}
} catch (e) {}
}
async function quickAddCompany() {
const name = prompt("Nombre de la nueva compañía:");
if(!name) return;
try {
const res = await fetch(`${API_URL}/companies`, {
method: 'POST',
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify({ name })
});
if(res.ok) loadCompanies();
} catch(e) { alert("Error al crear"); }
}
// --- CREAR SERVICIO ---
async function createService(e) {
e.preventDefault();
const btn = document.getElementById('btnSave');
btn.disabled = true; btn.innerText = "Guardando...";
const data = {
phone: document.getElementById('sPhone').value,
name: document.getElementById('sName').value,
address: document.getElementById('sAddress').value,
email: document.getElementById('sEmail').value,
description: document.getElementById('sDesc').value,
scheduled_date: document.getElementById('sDate').value,
scheduled_time: document.getElementById('sTime').value,
duration: document.getElementById('sDuration').value,
is_urgent: document.getElementById('sUrgent').checked,
is_company: document.getElementById('sIsCompany').checked,
company_id: document.getElementById('sCompanyId').value || null,
company_ref: document.getElementById('sCompanyRef').value,
internal_notes: document.getElementById('sNotesInternal').value,
client_notes: document.getElementById('sNotesClient').value
};
try {
const res = await fetch(`${API_URL}/services`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("token")}`
},
body: JSON.stringify(data)
});
const json = await res.json();
if (json.ok) {
showToast("✅ Servicio Creado Correctamente");
setTimeout(() => window.location.href = "panel.html", 1500); // Volver al panel o recargar
} else {
showToast("❌ " + json.error, true);
btn.disabled = false; btn.innerText = "Crear Servicio";
}
} catch (e) {
showToast("Error de conexión", true);
btn.disabled = false; btn.innerText = "Crear Servicio";
}
}
function showToast(msg, isError = false) {
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`;
m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0');
setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000);
}
</script>
</body>
</html>