| |
| |
| |
| |
| |
| |
|
|
| (function() { |
| 'use strict'; |
| |
| |
| |
| var originalDoSearch = window.doSearch; |
| |
| window.doSearch = function() { |
| var input = document.getElementById('q'); |
| var sidebar = document.getElementById('sidebar'); |
| var grid = document.getElementById('grid'); |
| |
| |
| if (sidebar) sidebar.style.display = 'none'; |
| |
| |
| var dropdown = document.getElementById('searchDropdown'); |
| if (!dropdown) { |
| dropdown = document.createElement('div'); |
| dropdown.id = 'searchDropdown'; |
| dropdown.style.cssText = 'position:absolute;z-index:100;top:100%;left:0;right:0;background:#fff;border:1px solid #e2e8f0;border-radius:10px;max-height:400px;overflow-y:auto;box-shadow:0 4px 16px rgba(0,0,0,0.1);margin-top:5px'; |
| input.parentNode.appendChild(dropdown); |
| } |
| |
| var query = (input.value || '').trim(); |
| if (!query || query.length < 2) { |
| dropdown.innerHTML = ''; |
| dropdown.style.display = 'none'; |
| if (originalDoSearch) originalDoSearch(); |
| return; |
| } |
| |
| |
| var D = window.D || []; |
| var results = []; |
| var qNorm = query.toLowerCase().normalize('NFD').replace(/[̀-ͯ]/g, '').replace(/đ/g, 'd'); |
| |
| for (var i = 0; i < D.length && results.length < 10; i++) { |
| var p = D[i]; |
| var name = (p.name || '').toLowerCase(); |
| var sku = (p.sku || '').toLowerCase(); |
| var model = (p.sku || p.mod || ''); |
| |
| if (name.indexOf(qNorm) !== -1 || sku.indexOf(qNorm) !== -1) { |
| results.push(p); |
| } |
| } |
| |
| if (results.length === 0) { |
| dropdown.innerHTML = '<div style="padding:12px;color:#64748b;font-size:.85rem">Không tìm thấy sản phẩm</div>'; |
| dropdown.style.display = 'block'; |
| return; |
| } |
| |
| |
| dropdown.innerHTML = results.map(function(p, i) { |
| var idx = window.D.indexOf(p); |
| var brandColor = p.brand === 'Eurogold' ? '#c8102c' : (p.brand === 'Grob' ? '#2e7d32' : '#003f62'); |
| return '<div data-idx="' + idx + '" style="padding:10px 12px;cursor:pointer;border-bottom:1px solid #e2e8f0;transition:background .2s;font-size:.82rem;display:flex;gap:10px;align-items:center" ' + |
| 'onmouseover="this.style.background=\'#f8fafc\'" onmouseout="this.style.background=\'#fff\'">' + |
| '<img src="' + (p.image || '') + '" style="width:48px;height:48px;object-fit:contain;background:#f8fafc;border-radius:6px" onerror="this.style.display=\'none\'">' + |
| '<div style="flex:1"><div style="font-weight:600;color:#0f172a;margin-bottom:2px">' + p.name + '</div>' + |
| '<div style="font-size:.72rem;color:' + brandColor + '">' + model + ' ' + (p.price ? ' — ' + p.price : '') + '</div></div>' + |
| '<button onclick="event.stopPropagation();addToCart({id:\'' + p.sku + '\',name:\'' + p.name.replace(/'/g, "\\'") + '\',price:' + (p.priceNum || 0) + ',sku:\'' + p.sku + '\',image:\'' + (p.image || '') + '\',brand:\'' + p.brand + '\'});this.innerHTML=\'✅\';this.disabled=true;" ' + |
| 'style="background:#25d366;color:#fff;border:none;border-radius:5px;padding:4px 8px;font-size:.7rem;cursor:pointer">Thêm giỏ</button></div>'; |
| }).join(''); |
| |
| dropdown.style.display = 'block'; |
| |
| |
| setTimeout(function() { |
| dropdown.querySelectorAll('[data-idx]').forEach(function(el) { |
| el.onclick = function() { |
| var idx = parseInt(this.getAttribute('data-idx')); |
| showDetail(idx); |
| dropdown.innerHTML = ''; |
| dropdown.style.display = 'none'; |
| input.value = ''; |
| }; |
| }); |
| }, 100); |
| }; |
| |
| |
| document.addEventListener('click', function(e) { |
| var dropdown = document.getElementById('searchDropdown'); |
| var input = document.getElementById('q'); |
| if (dropdown && input && !input.contains(e.target) && !dropdown.contains(e.target)) { |
| dropdown.innerHTML = ''; |
| dropdown.style.display = 'none'; |
| } |
| }); |
| |
| |
| |
| function addAddProductButton() { |
| var existing = document.getElementById('addProductQuickBtn'); |
| if (existing) return; |
| |
| |
| var btn = document.createElement('button'); |
| btn.id = 'addProductQuickBtn'; |
| btn.innerHTML = '<i class="fas fa-plus"></i> Thêm sản phẩm'; |
| btn.style.cssText = 'padding:8px 16px;background:#003f62;color:#fff;border:none;border-radius:8px;font-size:.8rem;cursor:pointer;margin-left:10px'; |
| btn.onclick = function() { |
| |
| var modal = document.getElementById('addProductOverlay'); |
| if (modal) { |
| modal.classList.add('open'); |
| modal.style.display = 'flex'; |
| } |
| }; |
| |
| |
| var navIcons = document.querySelector('.nav-icons'); |
| if (navIcons) { |
| navIcons.insertBefore(btn, navIcons.firstChild); |
| } |
| |
| console.log('[Search Fix] Added "Thêm sản phẩm" button'); |
| } |
| |
| |
| setTimeout(addAddProductButton, 2000); |
| |
| console.log('[Search Dropdown Fix] Loaded'); |
| })(); |