Selector de teléfonos en asignados y calendario, priorizando móviles sobre fijos

This commit is contained in:
2026-08-27 07:05:13 +00:00
parent b33c65093b
commit fd2dea56a6
2 changed files with 75 additions and 8 deletions

View File

@@ -169,8 +169,10 @@
<button onclick="copyAndOpenPortal()" id="btnPortal" class="w-full mb-6 bg-indigo-50 text-indigo-700 hover:bg-indigo-100 font-black py-2.5 rounded-xl border border-indigo-200 flex items-center justify-center gap-2 text-[10px] uppercase tracking-widest active:scale-95 transition-all text-center">
<i data-lucide="external-link" class="w-4 h-4"></i> Copiar y Abrir Portal
</button>
</button>
<div id="phoneSelectorRow" class="hidden flex items-center gap-2 mb-3 overflow-x-auto no-scrollbar"></div>
<button onclick="callClient()" class="w-full bg-primary-dynamic text-white font-black py-4 rounded-2xl shadow-xl flex items-center justify-center gap-3 uppercase text-sm tracking-widest active:scale-95 transition-all">
<i data-lucide="phone" class="w-5 h-5 fill-current"></i> Llamar al Cliente
</button>
@@ -1010,9 +1012,16 @@
const fullAddress = `${raw["Dirección"] || ""}, ${raw["Código Postal"] || ""} ${raw["Población"] || ""}`;
document.getElementById('detAddress').innerText = fullAddress;
// 📞 Usamos los teléfonos ya extraídos y priorizados (móvil primero) por la API.
// Si por caché no vinieran, caemos al match simple de siempre.
const rawPhone = raw["Teléfono"] || raw["TELEFONOS"] || raw["TELEFONO"] || "";
const matchPhone = String(rawPhone).match(/[6789]\d{8}/);
document.getElementById('detPhoneRaw').value = matchPhone ? matchPhone[0] : "";
const fallbackPhone = matchPhone ? matchPhone[0] : "";
const detailPhones = Array.isArray(s.phones) && s.phones.length > 0
? s.phones
: (fallbackPhone ? [{ number: fallbackPhone, isMobile: /^[67]/.test(fallbackPhone), horario: '', contacto: '' }] : []);
document.getElementById('detPhoneRaw').value = detailPhones[0]?.number || "";
renderPhoneSelector(detailPhones);
const banner = document.getElementById('urgentBanner');
if (s.is_urgent) banner.classList.remove('hidden');
@@ -1386,13 +1395,37 @@
} else { alert("Sin teléfono"); }
}
function openWhatsApp() {
const p = document.getElementById('detPhoneRaw').value;
function openWhatsApp() {
const p = document.getElementById('detPhoneRaw').value;
if (p) {
sendLog("WhatsApp Abierto", "El operario ha abierto el chat de WhatsApp con el cliente.");
window.open(`https://wa.me/34${p}`, '_blank');
window.open(`https://wa.me/34${p}`, '_blank');
} else { alert("Sin teléfono"); }
}
// 📞 Cuando el servicio trae más de un teléfono (típico de HomeServe), pintamos un
// selector de chips para que el operario elija cuál usar antes de Llamar/WhatsApp.
// Con un solo teléfono no mostramos nada (para no añadir ruido visual de más).
function renderPhoneSelector(phones) {
const row = document.getElementById('phoneSelectorRow');
if (!row) return;
if (!phones || phones.length <= 1) { row.innerHTML = ''; row.classList.add('hidden'); return; }
const active = document.getElementById('detPhoneRaw').value;
row.innerHTML = phones.map(p => {
const isActive = p.number === active;
return `
<button type="button" onclick="selectPhone('${p.number}')" class="shrink-0 flex items-center gap-1.5 px-3 py-1.5 rounded-full border text-[10px] font-black uppercase tracking-widest ${isActive ? 'bg-primary-dynamic text-white border-primary-dynamic' : 'bg-white text-slate-500 border-slate-200'}">
<i data-lucide="${p.isMobile ? 'smartphone' : 'phone'}" class="w-3 h-3"></i> ${p.number}
</button>`;
}).join('');
row.classList.remove('hidden');
lucide.createIcons();
}
function selectPhone(number) {
document.getElementById('detPhoneRaw').value = number;
const s = localServices.find(x => String(x.id) === String(document.getElementById('detId').value));
renderPhoneSelector(Array.isArray(s?.phones) && s.phones.length > 0 ? s.phones : [{number, isMobile:/^[67]/.test(number)}]);
}
async function copyAndOpenPortal() {
const btn = document.getElementById('btnPortal');