Spaces:
Running
Running
File size: 5,508 Bytes
9afdbbe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | /**
* Search Dropdown & Add Product Fix
* =================================
* 1. Khôi phục dropdown kết quả khi tìm kiếm thường
* 2. Khôi phục nút "Thêm sản phẩm" mở modal
*/
(function() {
'use strict';
// === PATCH doSearch() để hiện dropdown ===
// Ghi đè hàm doSearch gốc
var originalDoSearch = window.doSearch;
window.doSearch = function() {
var input = document.getElementById('q');
var sidebar = document.getElementById('sidebar');
var grid = document.getElementById('grid');
// Ẩn sidebar khi search
if (sidebar) sidebar.style.display = 'none';
// Tạo dropdown nếu chưa có
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;
}
// Tìm kiếm trong dữ liệu
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;
}
// Render dropdown results
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';
// Bind click to show detail
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);
};
// Ẩn dropdown khi click ngoài
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';
}
});
// === THÊM NÚT "THÊM SẢN PHẨM" ===
// Kiểm tra nút đã tồn tại chưa
function addAddProductButton() {
var existing = document.getElementById('addProductQuickBtn');
if (existing) return;
// Tạo nút
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() {
// Mở modal thêm sản phẩm
var modal = document.getElementById('addProductOverlay');
if (modal) {
modal.classList.add('open');
modal.style.display = 'flex';
}
};
// Thêm vào header
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');
}
// Chờ DOM sẵn sàng
setTimeout(addAddProductButton, 2000);
console.log('[Search Dropdown Fix] Loaded');
})(); |