diff --git a/presupuestos.html b/presupuestos.html
index 1742ffa..9998220 100644
--- a/presupuestos.html
+++ b/presupuestos.html
@@ -64,8 +64,12 @@
@@ -511,6 +515,39 @@ async function downloadPDF(id) {
lucide.createIcons();
}
+ // ------------------ BUSCADOR DE CLIENTES AUTOMÁTICO ------------------
+ async function searchClientByPhone() {
+ const phoneInput = document.getElementById('c_phone');
+ const phone = phoneInput.value.trim();
+ const loading = document.getElementById('phoneLoading');
+
+ // Si el teléfono tiene menos de 9 dígitos, no hacemos nada
+ if (!phone || phone.length < 9) return;
+
+ loading.classList.remove('hidden'); // Mostramos el iconito de pensar
+
+ try {
+ const res = await fetch(`${API_URL}/clients/search?phone=${encodeURIComponent(phone)}`, {
+ headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
+ });
+ const data = await res.json();
+
+ if (data.ok && data.client) {
+ // Si encuentra al cliente, rellenamos los datos
+ document.getElementById('c_name').value = data.client.full_name || '';
+ if (data.client.addresses && data.client.addresses.length > 0) {
+ // Cogemos la primera dirección que tenga guardada
+ document.getElementById('c_address').value = data.client.addresses[0] || '';
+ }
+ showToast("✅ Cliente encontrado y cargado");
+ }
+ } catch (e) {
+ console.error("Error buscando cliente", e);
+ } finally {
+ loading.classList.add('hidden'); // Ocultamos el iconito
+ }
+ }
+
// ------------------ CREACIÓN DE PRESUPUESTO (ITEMS) ------------------
function addItem() {
const conceptInput = document.getElementById('item_concept');