diff --git a/public/css/styles.css b/public/css/styles.css index 5fb5d17..17c7ecb 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -691,6 +691,161 @@ body.dark-mode .floating-cart-bar { color: #ffffff; border-color: #0f172a; box-shadow: 0 2px 5px rgba(15, 23, 42, 0.15); +/* ========================================================================== + BUSCADOR INTELIGENTE DE PRODUCTOS EN MOSTRADOR (POS) + ========================================================================== */ +.pos-search-bar-wrap { + margin-bottom: 12px; + display: flex; + flex-direction: column; + gap: 6px; + width: 100%; +} + +.pos-search-input-box { + display: flex; + align-items: center; + gap: 10px; + background: #ffffff; + border: 1.5px solid #cbd5e1; + border-radius: 12px; + padding: 0 14px; + height: 42px; + box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03); + transition: all 0.15s ease; + position: relative; +} + +.pos-search-input-box:focus-within { + border-color: #0284c7; + box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.12), 0 2px 6px rgba(0, 0, 0, 0.04); +} + +.pos-search-icon { + color: #64748b; + flex-shrink: 0; + transition: color 0.15s ease; +} + +.pos-search-input-box:focus-within .pos-search-icon { + color: #0284c7; +} + +.pos-search-input { + flex: 1; + min-width: 0; + border: none; + background: transparent; + color: #0f172a; + font-size: 0.9rem; + font-weight: 600; + padding: 0; + height: 100%; + outline: none; +} + +.pos-search-input::placeholder { + color: #94a3b8; + font-weight: 500; +} + +.pos-search-clear-btn { + background: none; + border: none; + color: #94a3b8; + font-size: 0.92rem; + font-weight: 800; + cursor: pointer; + padding: 4px 8px; + border-radius: 6px; + display: inline-flex; + align-items: center; + justify-content: center; + transition: all 0.15s ease; +} + +.pos-search-clear-btn:hover { + color: #dc2626; + background: #fee2e2; +} + +.pos-search-shortcut-pill { + background: #f1f5f9; + border: 1px solid #e2e8f0; + color: #64748b; + font-size: 0.72rem; + font-weight: 700; + padding: 2px 7px; + border-radius: 6px; + line-height: 1; + pointer-events: none; + user-select: none; +} + +.pos-search-stats { + display: flex; + justify-content: space-between; + align-items: center; + font-size: 0.76rem; + color: #64748b; + padding: 0 4px; +} + +.pos-search-stats strong { + color: #0f172a; +} + +.pos-search-reset-link { + background: none; + border: none; + color: #0284c7; + font-size: 0.76rem; + font-weight: 700; + cursor: pointer; + padding: 2px 4px; + text-decoration: underline; +} + +.pos-search-reset-link:hover { + color: #0369a1; +} + +.pos-search-empty-state { + grid-column: 1 / -1; + background: #ffffff; + border: 1.5px dashed #cbd5e1; + border-radius: 16px; + padding: 36px 20px; + text-align: center; + color: #64748b; + margin: 10px 0; +} + +.pos-search-empty-state h4 { + font-size: 1.05rem; + font-weight: 800; + color: #0f172a; + margin: 8px 0 4px 0; +} + +.pos-search-empty-state p { + font-size: 0.82rem; + color: #64748b; + margin: 0 0 14px 0; +} + +.pos-prod-card.search-match-first { + border-color: #0284c7; + box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2); +} + +@media (max-width: 640px) { + .pos-search-shortcut-pill { + display: none; + } + .pos-search-input { + font-size: 0.85rem; + } } /* Toolbar de categorías y botón + Rápido */ diff --git a/public/js/pos.js b/public/js/pos.js index 588a270..47a8ba4 100644 --- a/public/js/pos.js +++ b/public/js/pos.js @@ -113,28 +113,177 @@ function renderPosCategories() { } let currentPosCatId = null; +let posSearchQuery = ''; + +function normalizeSearchStr(s) { + if (!s) return ''; + return String(s) + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase() + .trim(); +} + +function matchesProduct(prod, query) { + if (!query) return true; + const cleanQ = normalizeSearchStr(query); + if (!cleanQ) return true; + + // 1. Operadores de precio (ej: <1000, <=1200, >500, >=800) + const priceOpMatch = cleanQ.match(/^([<>]=?)(\d+)$/); + if (priceOpMatch) { + const op = priceOpMatch[1]; + const val = parseInt(priceOpMatch[2], 10); + if (op === '<') return prod.precio_colones < val; + if (op === '<=') return prod.precio_colones <= val; + if (op === '>') return prod.precio_colones > val; + if (op === '>=') return prod.precio_colones >= val; + } + + // 2. Filtros especiales por palabras clave + if (cleanQ === 'mep' || cleanQ === 'saludable') { + return Boolean(prod.cumple_mep || prod.es_saludable); + } + if (cleanQ === 'bloqueado' || cleanQ === 'agotado' || cleanQ === 'sin stock') { + return Boolean(prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0)); + } + if (cleanQ === 'disponible' || cleanQ === 'en stock') { + return Boolean(prod.disponible === 1 && (!prod.control_stock || prod.stock > 0)); + } + + // 3. Coincidencia por precio numérico exacto o parcial + if (/^\d+$/.test(cleanQ)) { + const pStr = String(prod.precio_colones); + if (pStr === cleanQ || pStr.includes(cleanQ)) return true; + } + + // 4. Búsqueda multi-término inteligente (nombre, categoría, descripción, emoji) + const tokens = cleanQ.split(/\s+/).filter(Boolean); + const targetStr = normalizeSearchStr( + `${prod.nombre || ''} ${prod.categoria_nombre || ''} ${prod.descripcion || ''} ${prod.icono || ''} ${prod.precio_colones || ''}` + ); + + return tokens.every(token => targetStr.includes(token)); +} + +function handlePosSearchInput(val) { + posSearchQuery = val || ''; + const clearBtn = document.getElementById('posSearchClearBtn'); + if (clearBtn) clearBtn.style.display = posSearchQuery.trim() ? 'inline-flex' : 'none'; + renderPosProducts(currentPosCatId); +} + +function clearPosSearch() { + posSearchQuery = ''; + const input = document.getElementById('posProductSearch'); + if (input) { + input.value = ''; + input.focus(); + } + const clearBtn = document.getElementById('posSearchClearBtn'); + if (clearBtn) clearBtn.style.display = 'none'; + const statsEl = document.getElementById('posSearchStats'); + if (statsEl) statsEl.style.display = 'none'; + renderPosProducts(currentPosCatId); +} + +function handlePosSearchKeyDown(e) { + if (e.key === 'Escape') { + clearPosSearch(); + e.target.blur(); + return; + } + + if (e.key === 'Enter') { + e.preventDefault(); + const grid = document.getElementById('posProductsGrid'); + if (!grid) return; + const firstMatchCard = grid.querySelector('.pos-prod-card:not(.out-of-stock-pos)'); + if (firstMatchCard) { + firstMatchCard.click(); + if (window.sounds) window.sounds.playCoin(); + clearPosSearch(); + } + } +} function filterPosCat(catId, btn) { currentPosCatId = catId; document.querySelectorAll('#posCategoriesBar .pos-cat-pill').forEach(b => b.classList.remove('active')); - if (btn) btn.classList.add('active'); + if (btn) { + btn.classList.add('active'); + } else if (catId === null) { + const firstPill = document.querySelector('#posCategoriesBar .pos-cat-pill'); + if (firstPill) firstPill.classList.add('active'); + } renderPosProducts(catId); } function renderPosProducts(catId) { const grid = document.getElementById('posProductsGrid'); + if (!grid) return; + + const hasSearch = Boolean(posSearchQuery && posSearchQuery.trim()); let list = posProducts; - if (catId !== null) { + + // Filtrado compuesto: búsqueda inteligente y categoría + if (hasSearch) { + list = list.filter(p => matchesProduct(p, posSearchQuery)); + if (catId !== null) { + list = list.filter(p => p.categoria_id === catId); + } + } else if (catId !== null) { list = list.filter(p => p.categoria_id === catId); } - grid.innerHTML = list.map(prod => { + // Barra informativa de estado de búsqueda + const statsEl = document.getElementById('posSearchStats'); + const statsText = document.getElementById('posSearchStatsText'); + if (statsEl && statsText) { + if (hasSearch) { + statsEl.style.display = 'flex'; + const catLabel = catId !== null + ? `en categoría "${(posCategories.find(c => c.id === catId) || {}).nombre || ''}"` + : 'en todo el catálogo'; + statsText.innerHTML = `Mostrando ${list.length} de ${posProducts.length} productos ${catLabel}`; + } else { + statsEl.style.display = 'none'; + } + } + + // Estado cuando no hay productos coincidentes + if (list.length === 0) { + if (hasSearch) { + const safeQuery = posSearchQuery.replace(/"/g, '"').trim(); + grid.innerHTML = ` +
+ 🔍 +

No se encontraron productos para "${safeQuery}"

+

Verifica el nombre, precio o intenta buscando en otra categoría.

+
+ + ${catId !== null ? `` : ''} + +
+
+ `; + } else { + grid.innerHTML = '

No hay productos disponibles en esta categoría.

'; + } + return; + } + + grid.innerHTML = list.map((prod, idx) => { const isOutOfStock = prod.disponible === 0 || (prod.control_stock === 1 && prod.stock <= 0); + const isFirstMatch = hasSearch && idx === 0 && !isOutOfStock; return ` -
- ${isOutOfStock ? '
BLOQUEADO
' : ''} + ${isOutOfStock ? '
BLOQUEADO
' : (isFirstMatch ? '
Enter ↵
' : '')}
${prod.icono || '🥪'}
${prod.nombre}
@@ -153,15 +302,21 @@ function renderPosProducts(catId) { // NUEVO PRODUCTO RÁPIDO EN CALIENTE (POS) // ========================================== -function openQuickProductModal() { +function openQuickProductModal(defaultName = '') { const modal = document.getElementById('modalProductoRapido'); if (!modal) return; modal.style.display = 'flex'; const inputNom = document.getElementById('inputQuickProdNombre'); const inputPre = document.getElementById('inputQuickProdPrecio'); if (inputNom) { - inputNom.value = ''; - setTimeout(() => inputNom.focus(), 80); + inputNom.value = defaultName ? String(defaultName).trim() : ''; + setTimeout(() => { + if (defaultName) { + if (inputPre) inputPre.focus(); + } else { + inputNom.focus(); + } + }, 80); } if (inputPre) inputPre.value = ''; } @@ -701,6 +856,18 @@ function initPistolScanner() { return; } + // Atajo rápido: Presionar '/' fuera de campos de texto enfoca el buscador de productos + if (e.key === '/' && document.activeElement && !['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName)) { + e.preventDefault(); + switchPosTab('mostrador'); + const searchInput = document.getElementById('posProductSearch'); + if (searchInput) { + searchInput.focus(); + searchInput.select(); + } + return; + } + // Si presiona Enter y el modal está en pantalla de éxito, cerrarlo para el siguiente const modalSuccess = document.getElementById('pistolaStateSuccess'); if (e.key === 'Enter' && modalSuccess && modalSuccess.style.display !== 'none') { diff --git a/public/pos.html b/public/pos.html index 30559db..88395d9 100644 --- a/public/pos.html +++ b/public/pos.html @@ -101,6 +101,34 @@
+ +
+
+ + + + + + +
/
+
+ +
+