feat: Agregar buscador inteligente de productos en seccion de venta de mostrador
This commit is contained in:
@@ -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 */
|
||||
|
||||
+175
-8
@@ -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 <strong>${list.length}</strong> de <strong>${posProducts.length}</strong> 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 = `
|
||||
<div class="pos-search-empty-state">
|
||||
<span style="font-size: 2.2rem; display: block; margin-bottom: 8px;">🔍</span>
|
||||
<h4>No se encontraron productos para "${safeQuery}"</h4>
|
||||
<p>Verifica el nombre, precio o intenta buscando en otra categoría.</p>
|
||||
<div style="display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 14px;">
|
||||
<button type="button" class="btn-saas btn-saas-outline" onclick="clearPosSearch()">Limpiar búsqueda</button>
|
||||
${catId !== null ? `<button type="button" class="btn-saas btn-saas-outline" onclick="filterPosCat(null)">Buscar en todas las categorías</button>` : ''}
|
||||
<button type="button" class="btn-saas btn-saas-primary" onclick="openQuickProductModal('${safeQuery}')">
|
||||
<span>⚡</span> + Crear "${safeQuery}" como Rápido
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
grid.innerHTML = '<p style="grid-column: 1 / -1; text-align: center; color: #64748b; padding: 40px; font-size: 0.9rem;">No hay productos disponibles en esta categoría.</p>';
|
||||
}
|
||||
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 `
|
||||
<div class="pos-prod-card ${isOutOfStock ? 'out-of-stock-pos' : ''}"
|
||||
<div class="pos-prod-card ${isOutOfStock ? 'out-of-stock-pos' : ''} ${isFirstMatch ? 'search-match-first' : ''}"
|
||||
title="${isFirstMatch ? 'Primer resultado coincidente (pulsa Enter para cobrar)' : ''}"
|
||||
onclick="${isOutOfStock ? `alert('El producto \\'${prod.nombre.replace(/'/g, "\\'")}\\' se encuentra bloqueado o agotado.')` : `addToPosCart(${prod.id})`}">
|
||||
${isOutOfStock ? '<div style="position: absolute; top: 10px; right: 10px;"><span class="saas-status-badge saas-status-blocked"><span class="saas-dot"></span>BLOQUEADO</span></div>' : ''}
|
||||
${isOutOfStock ? '<div style="position: absolute; top: 10px; right: 10px;"><span class="saas-status-badge saas-status-blocked"><span class="saas-dot"></span>BLOQUEADO</span></div>' : (isFirstMatch ? '<div style="position: absolute; top: 8px; right: 8px;"><span class="saas-status-badge saas-status-active" style="font-size: 0.65rem; padding: 2px 6px;">Enter ↵</span></div>' : '')}
|
||||
<div style="font-size: 2.2rem; text-align: center; margin-bottom: 6px;">${prod.icono || '🥪'}</div>
|
||||
<div>
|
||||
<div class="prod-title">${prod.nombre}</div>
|
||||
@@ -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') {
|
||||
|
||||
@@ -101,6 +101,34 @@
|
||||
|
||||
<!-- VISTA 1: CATÁLOGO TÁCTIL DE PRODUCTOS (MOSTRADOR) -->
|
||||
<div id="viewPosMostrador">
|
||||
<!-- BUSCADOR INTELIGENTE DE PRODUCTOS EN MOSTRADOR -->
|
||||
<div class="pos-search-bar-wrap">
|
||||
<div class="pos-search-input-box">
|
||||
<svg class="pos-search-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2">
|
||||
<circle cx="11" cy="11" r="8"/>
|
||||
<line x1="21" y1="21" x2="16.65" y2="16.65"/>
|
||||
</svg>
|
||||
<input
|
||||
type="text"
|
||||
id="posProductSearch"
|
||||
class="pos-search-input"
|
||||
placeholder="Buscar por nombre, categoría o precio (ej: empanada, jugo, 1200, mep)..."
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
oninput="handlePosSearchInput(this.value)"
|
||||
onkeydown="handlePosSearchKeyDown(event)"
|
||||
/>
|
||||
<button type="button" id="posSearchClearBtn" class="pos-search-clear-btn" onclick="clearPosSearch()" title="Borrar búsqueda" style="display: none;">
|
||||
✕
|
||||
</button>
|
||||
<div class="pos-search-shortcut-pill" title="Atajo rápido de teclado: presiona '/'">/</div >
|
||||
</div>
|
||||
<div id="posSearchStats" class="pos-search-stats" style="display: none;">
|
||||
<span id="posSearchStatsText">Mostrando 0 productos</span>
|
||||
<button type="button" class="pos-search-reset-link" onclick="clearPosSearch()">Mostrar catálogo completo</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pos-category-toolbar">
|
||||
<div class="pos-category-scroll-container" id="posCategoriesBar">
|
||||
<!-- Filtros de categorías -->
|
||||
|
||||
Reference in New Issue
Block a user