diff --git a/web/cobranza.html b/web/cobranza.html
index de83a3d..ff6a45f 100644
--- a/web/cobranza.html
+++ b/web/cobranza.html
@@ -241,6 +241,15 @@
.modal-close { width: 32px; height: 32px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--line); cursor: pointer; color: var(--text-secondary); font-size: 18px; line-height: 1; transition: all 0.15s ease; }
.modal-close:hover { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
+ /* Combobox buscable de cliente (alta de cobro personalizado) */
+ .cli-list { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); margin-top: 8px; max-height: 260px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
+ .cli-head { font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-tertiary); font-weight: 700; padding: 9px 12px 4px; position: sticky; top: 0; background: var(--surface); }
+ .cli-item { padding: 9px 12px; font-size: 13.5px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
+ .cli-item:hover { background: var(--renta-soft); }
+ .cli-item .tel { color: var(--text-tertiary); font-size: 12px; white-space: nowrap; }
+ .cli-item.nuevo { color: var(--accent); font-weight: 600; }
+ .cli-empty { padding: 12px; text-align: center; color: var(--text-tertiary); font-size: 12.5px; }
+
.opt-card { background: var(--surface); border-radius: 13px; box-shadow: none; border: 1.5px solid var(--line); padding: 1rem 1.15rem; margin-bottom: 11px; cursor: pointer; transition: all 0.17s ease; }
.opt-card:hover { border-color: var(--accent); background: var(--accent-soft); }
.opt-card:active { transform: scale(0.99); }
@@ -1207,35 +1216,21 @@ function normNombre(s){
return (s||"").toUpperCase().replace(/\./g,"").replace(/\s+/g," ").trim();
}
-// Contactos del directorio deduplicados por nombre_norm (prefiere el que
-// tenga teléfono), ordenados alfabéticamente. Sirve al selector de cliente.
-function contactosDirectorio(){
- const map = new Map();
- (directorio||[]).forEach(c => {
- const k = c.nombre_norm || normNombre(c.nombre);
- const prev = map.get(k);
- if(!prev || (!prev.telefono && c.telefono)) {
- map.set(k, {nombre_norm: k, nombre: c.nombre, telefono: c.telefono||""});
- }
- });
- return [...map.values()].sort((a,b)=>a.nombre.localeCompare(b.nombre,'es'));
-}
+let rmCliSel = null; // contacto elegido: {nombre,nombre_norm,telefono} | "__nuevo__" | null
+let rmCliShown = []; // items actualmente listados (para resolver por índice)
function abrirCrearRenglon(){
document.getElementById("modal-inner").classList.remove("lote");
const hoyISO = new Date().toISOString().slice(0,10);
- const opciones = contactosDirectorio().map(c =>
- ``
- ).join("");
+ rmCliSel = null; rmCliShown = [];
document.getElementById("modal-inner").innerHTML = `
@@ -1273,22 +1268,75 @@ function abrirCrearRenglon(){
`;
document.getElementById("modal").classList.add("open");
- setTimeout(() => document.getElementById("rm-cliente-sel").focus(), 100);
+ setTimeout(() => document.getElementById("rm-cli-search").focus(), 100);
}
-// Muestra los campos de "nuevo contacto" solo cuando se elige esa opción.
-function onClienteSelChange(){
- const sel = document.getElementById("rm-cliente-sel");
- const blk = document.getElementById("rm-nuevo-contacto");
- if(!sel || !blk) return;
- const esNuevo = sel.value === "__nuevo__";
- blk.style.display = esNuevo ? "block" : "none";
- if(esNuevo) setTimeout(() => document.getElementById("rm-new-nombre").focus(), 50);
+// Normaliza para búsqueda: minúsculas + sin acentos.
+function _busqStr(s){ return (s||"").toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g,""); }
+
+// Listas seccionadas del directorio: personalizados (manuales) y Sheet,
+// deduplicadas por nombre_norm. Un nombre que exista como manual NO se repite
+// en la sección del Sheet.
+function rmCliListas(){
+ const dedupe = (arr) => {
+ const m = new Map();
+ arr.forEach(c => {
+ const k = c.nombre_norm || normNombre(c.nombre);
+ const prev = m.get(k);
+ if(!prev || (!prev.telefono && c.telefono)) m.set(k, {nombre_norm:k, nombre:c.nombre, telefono:c.telefono||""});
+ });
+ return [...m.values()].sort((a,b)=>a.nombre.localeCompare(b.nombre,'es'));
+ };
+ const manuales = dedupe((directorio||[]).filter(c => c.origen === "manual"));
+ const claveManual = new Set(manuales.map(c => c.nombre_norm));
+ const sheet = dedupe((directorio||[]).filter(c => c.origen !== "manual"))
+ .filter(c => !claveManual.has(c.nombre_norm));
+ return {manuales, sheet};
+}
+
+// Render del desplegable buscable. Orden: Agregar nuevo → Personalizados → Sheet.
+function rmCliRender(filtro){
+ const list = document.getElementById("rm-cli-list");
+ if(!list) return;
+ list.style.display = "block";
+ const q = _busqStr(filtro);
+ const {manuales, sheet} = rmCliListas();
+ const fil = (arr) => arr.filter(c => !q || _busqStr(c.nombre).includes(q));
+ const fm = fil(manuales), fs = fil(sheet);
+ rmCliShown = [];
+ const item = (c) => {
+ const i = rmCliShown.push(c) - 1;
+ return `
${c.nombre}${c.telefono?`+521 ${fmtTel(c.telefono)}`:""}
`;
+ };
+ let html = `
➕ Agregar contacto nuevo…
`;
+ if(fm.length){ html += `
Personalizados
` + fm.map(item).join(""); }
+ if(fs.length){ html += `
Directorio (Sheet)
` + fs.map(item).join(""); }
+ if(!fm.length && !fs.length){ html += `
Sin coincidencias. Usa “Agregar contacto nuevo”.
`; }
+ list.innerHTML = html;
+}
+
+function rmCliPickIdx(i){
+ const c = rmCliShown[i];
+ if(!c) return;
+ rmCliSel = c;
+ const inp = document.getElementById("rm-cli-search");
+ if(inp) inp.value = c.nombre + (c.telefono ? " · +521 "+fmtTel(c.telefono) : "");
+ const list = document.getElementById("rm-cli-list"); if(list) list.style.display = "none";
+ const blk = document.getElementById("rm-nuevo-contacto"); if(blk) blk.style.display = "none";
+}
+
+function rmCliPickNuevo(){
+ rmCliSel = "__nuevo__";
+ const inp = document.getElementById("rm-cli-search");
+ if(inp) inp.value = "➕ Contacto nuevo";
+ const list = document.getElementById("rm-cli-list"); if(list) list.style.display = "none";
+ const blk = document.getElementById("rm-nuevo-contacto"); if(blk) blk.style.display = "block";
+ setTimeout(() => { const e = document.getElementById("rm-new-nombre"); if(e) e.focus(); }, 50);
}
async function crearRenglonSubmit(){
// El alta manual siempre crea un cobro personalizado (tipo 'otro').
- const sel = document.getElementById("rm-cliente-sel").value;
+ const sel = (rmCliSel === "__nuevo__") ? "__nuevo__" : (rmCliSel ? rmCliSel.nombre_norm : "");
const vehiculo = (document.getElementById("rm-vehiculo").value || "").trim();
const etiqueta = (document.getElementById("rm-etiqueta").value || "").trim();
const monto = parseFloat(document.getElementById("rm-monto").value);
@@ -1307,7 +1355,7 @@ async function crearRenglonSubmit(){
if(!nuevoNombre){ toast("Falta el nombre del nuevo contacto"); return; }
if(nuevoTel.length !== 10){ toast("El teléfono debe tener 10 dígitos"); return; }
} else {
- contacto = contactosDirectorio().find(x => x.nombre_norm === sel);
+ contacto = (rmCliSel && typeof rmCliSel === "object") ? rmCliSel : null;
if(!contacto){ toast("Cliente no válido"); return; }
}
if(!vehiculo){ toast("Falta vehículo"); return; }