bep40 commited on
Commit
636636a
·
verified ·
1 Parent(s): 6197eff

v15: replace inline doAISearch with context-aware version using showDetail(index)

Browse files
Files changed (1) hide show
  1. index.html +58 -61
index.html CHANGED
@@ -2741,68 +2741,65 @@ Miễn phí giao hàng trong TPHCM. | Giá đã bao gồm VAT.
2741
  </div>
2742
  <script src="search-plus-boot.js?v=15"></script>
2743
 
2744
- <script>
2745
- function doAISearch(){
2746
- const q=document.getElementById('aiSearch').value.trim();
2747
- if(!q){document.getElementById('aiResults').style.display='block';document.getElementById('aiResults').innerHTML='💡 Nhập mã SP (VD: MDI702) hoặc nhiều mã: MDI702, MC9086HS';return}
2748
- const res=document.getElementById('aiResults');
2749
- res.style.display='block';res.innerHTML=' Đang tìm...';
2750
- if(typeof D==='undefined'||!D.length){res.innerHTML='⏳ Đang tải SP...';return}
2751
- // Split by comma/semicolon for multi-code
2752
- let queries=q.split(/[,;]+/).map(c=>c.trim()).filter(c=>c.length>=2);
2753
- if(queries.length===0)queries=[q];
2754
- let allResults=[];
2755
- queries.forEach(query=>{
2756
- const qLow=query.toLowerCase().replace(/[.\-_ \/]/g,'');
2757
- // Priority 1: EXACT match on any identifier field
2758
- let exact=D.filter(p=>{
2759
- const fields=[(p.sku||''),(p.mod||''),(p.slug||'')].map(f=>f.toLowerCase().replace(/[.\-_ \/]/g,''));
2760
- return fields.some(f=>f===qLow);
2761
- });
2762
- if(exact.length){allResults.push(...exact);return}
2763
- // Priority 2: Identifier CONTAINS query or query CONTAINS identifier
2764
- let partial=D.filter(p=>{
2765
- const fields=[(p.sku||''),(p.mod||''),(p.name||'').split('|')[0]].map(f=>f.toLowerCase().replace(/[.\-_ \/]/g,''));
2766
- return fields.some(f=>f&&qLow.length>=3&&(f.includes(qLow)||qLow.includes(f)));
2767
- });
2768
- if(partial.length){allResults.push(...partial.slice(0,5));return}
2769
- // Priority 3: Keyword search in name+category
2770
- const qNorm=query.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/đ/g,'d');
2771
- const words=qNorm.split(/\s+/).filter(w=>w.length>=2&&!['cua','cai','cho','cac','nhung','voi','duoi','tren','khoang'].includes(w));
2772
- const bm=query.match(/(\d+)\s*(tr|triệu|m)/i);
2773
- const budget=bm?parseInt(bm[1])*1e6:0;
2774
- let scored=D.map(p=>{
2775
- const pText=((p.name||'')+(p.cat||'')).toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/đ/g,'d');
2776
- let sc=0;
2777
- words.forEach(w=>{if(pText.includes(w))sc+=10});
2778
- if(/malloca/i.test(p.brand))sc+=3;
2779
- if(/grob/i.test(p.brand))sc+=2;
2780
- if(budget&&p.priceNum>0){if(p.priceNum<=budget*1.2)sc+=5;else if(p.priceNum>budget*1.5)sc-=10}
2781
- return{p,sc};
2782
- }).filter(x=>x.sc>=10).sort((a,b)=>b.sc-a.sc);
2783
- allResults.push(...scored.slice(0,6).map(x=>x.p));
2784
- });
2785
- // Deduplicate by sku
2786
- let seen=new Set();let unique=[];
2787
- allResults.forEach(p=>{const id=p.sku||p.name;if(!seen.has(id)){seen.add(id);unique.push(p)}});
2788
- if(!unique.length){res.innerHTML='❌ Không tìm thấy "'+q+'". Thử nhập chính xác mã SP.';return}
2789
- // Render results with specs
2790
- let h='<div style="font-weight:700;margin-bottom:8px;font-size:.9rem">🔍 '+unique.length+' sản phẩm:</div>';
2791
- h+='<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:10px">';
2792
- unique.slice(0,12).forEach(p=>{
2793
- h+='<div style="background:#fff;border-radius:10px;padding:10px;box-shadow:0 2px 6px rgba(0,0,0,.08);cursor:pointer;transition:.2s" onclick="if(typeof showDetail===\'function\')showDetail('+D.indexOf(p)+')" onmouseover="this.style.transform=\'translateY(-2px)\'" onmouseout="this.style.transform=\'\'"><div style="display:flex;align-items:center;gap:4px;margin-bottom:4px"><span style="font-size:.72rem;font-weight:700;color:#003f62">'+(p.sku||p.mod||'')+'</span><span style="font-size:.58rem;color:#888;background:#f0f0f0;padding:1px 4px;border-radius:3px">'+p.brand+'</span></div>';
2794
- if(p.image)h+='<img src="'+p.image+'" style="width:100%;height:90px;object-fit:contain;border-radius:6px;margin-bottom:6px;background:#f8f8f8" loading="lazy">';
2795
- h+='<div style="font-size:.7rem;line-height:1.3;height:2.5em;overflow:hidden;color:#333">'+((p.name||'').split('|').pop()||'').trim().substring(0,50)+'</div>';
2796
- h+='<div style="font-size:.88rem;font-weight:800;color:#003f62;margin-top:4px">'+(p.price||'Liên hệ')+'</div>';
2797
- const specs=p.specs||{};const sk=Object.entries(specs).slice(0,3);
2798
- if(sk.length){h+='<div style="margin-top:4px;padding-top:4px;border-top:1px solid #eee;font-size:.58rem;color:#555">';sk.forEach(([k,v])=>{h+='<div>'+k+': <b>'+v+'</b></div>'});h+='</div>'}
2799
- h+='<button onclick="event.stopPropagation();addToCart('+D.indexOf(p)+')" style="margin-top:5px;width:100%;padding:5px;background:var(--p);color:#fff;border:none;border-radius:5px;font-size:.68rem;cursor:pointer;font-weight:600">+ Thêm giỏ hàng</button>';
2800
-
2801
- h+='<button onclick="event.stopPropagation();saveToOrder('+D.indexOf(p)+')" style="margin-top:5px;width:100%;padding:4px;background:var(--p,#003f62);color:#fff;border:none;border-radius:5px;font-size:.68rem;cursor:pointer">+ Them don</button>';
2802
- h+='</div>';
2803
  });
2804
- h+='</div>';
2805
- res.innerHTML=h;
2806
  }
2807
  function doAIQuote(){
2808
  const q=document.getElementById('aiSearch').value.trim()||'bếp từ + máy hút mùi + chậu rửa khoảng 50 triệu';
 
2741
  </div>
2742
  <script src="search-plus-boot.js?v=15"></script>
2743
 
2744
+ <script>function doAISearch(){
2745
+ // Delegated to search-plus-boot.js v15 (ALL-IN-ONE)
2746
+ if(window._vaiSearchContext && typeof D!=='undefined' && D.length>0){
2747
+ var input=document.getElementById('aiSearch');var resultsDiv=document.getElementById('aiResults');
2748
+ if(!input||!resultsDiv)return;var query=input.value.trim();
2749
+ if(!query||query.length<2){resultsDiv.style.display='block';resultsDiv.innerHTML='💡 Nhập mã SP hoặc mô tả: "bếp từ Grob dưới 10tr", "máy hút mùi Malloca"';return;}
2750
+ resultsDiv.style.display='block';resultsDiv.innerHTML='⏳ Đang tìm...';
2751
+ // Multi-code search: nếu dấu phẩy thì tìm theo mã
2752
+ var queries=query.split(/[,;]+/).map(function(c){return c.trim()}).filter(function(c){return c.length>=2});
2753
+ if(queries.length>1){
2754
+ var allResults=[];
2755
+ queries.forEach(function(q2){
2756
+ var qLow=q2.toLowerCase().replace(/[.\-_ \/]/g,'');
2757
+ for(var i=0;i<D.length&&allResults.length<20;i++){
2758
+ var p=D[i];if(!p)continue;
2759
+ var fields=[(p.sku||''),(p.mod||''),(p.slug||'')].map(function(f){return f.toLowerCase().replace(/[.\-_ \/]/g,'')});
2760
+ if(fields.some(function(f){return f===qLow||(f&&qLow.length>=3&&(f.indexOf(qLow)!==-1||qLow.indexOf(f)!==-1))})){allResults.push(p);}
2761
+ }
2762
+ });
2763
+ if(allResults.length){_renderAIResults(allResults,resultsDiv);return;}
2764
+ }
2765
+ // Context search (category + brand + price + keywords)
2766
+ var results=window._vaiSearchContext(query,24);
2767
+ if(!results.length){resultsDiv.innerHTML='❌ Không tìm thấy "'+query+'". Thử: "bếp từ Malloca", "máy hút mùi dưới 15tr"';return;}
2768
+ var prods=results.map(function(r){return r.p;});
2769
+ _renderAIResults(prods,resultsDiv);
2770
+ return;
2771
+ }
2772
+ // Fallback: basic
2773
+ var q2=document.getElementById('aiSearch').value.trim();
2774
+ if(!q2){document.getElementById('aiResults').innerHTML='💡 Nhập mã hoặc tên SP';document.getElementById('aiResults').style.display='block';return;}
2775
+ document.getElementById('aiResults').innerHTML='⏳ Đang tải dữ liệu...';document.getElementById('aiResults').style.display='block';
2776
+ }
2777
+ function _renderAIResults(prods,res){
2778
+ var norm=window._vaiSearchContext?function(s){return(s||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[Đđ]/g,'d').toLowerCase()}:function(s){return(s||'').toLowerCase()};
2779
+ var cat=window._vaiDetectCategory?window._vaiDetectCategory(document.getElementById('aiSearch').value):'';
2780
+ var brand=window._vaiDetectBrand?window._vaiDetectBrand(document.getElementById('aiSearch').value):'';
2781
+ var fmt=function(n){if(!n||isNaN(n))return'LH';return Number(n).toLocaleString('vi-VN')+'đ'};
2782
+ var parts=[];
2783
+ if(cat)parts.push('📂 '+cat);if(brand)parts.push('🏷 '+brand);
2784
+ parts.push('🔍 '+prods.length+' SP');
2785
+ var h='<div style="margin-bottom:10px;display:flex;gap:6px;flex-wrap:wrap">'+parts.map(function(p){return'<span style="background:#eff6ff;padding:3px 8px;border-radius:20px;font-size:11px;font-weight:600;color:#1e40af;border:1px solid #bfdbfe">'+p+'</span>'}).join('')+'</div>';
2786
+ h+='<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(170px,1fr));gap:10px">';
2787
+ prods.slice(0,24).forEach(function(p){
2788
+ var idx=D.indexOf(p);
2789
+ var name=p.name||p.n||'';var price=p.price||p.p||'LH';var img=p.image||p.img||p.i||'';var sku=p.sku||p.model||p.mod||'';var brand2=p.brand||'';
2790
+ var feats=(p.feats||[]).slice(0,2).join(' ');
2791
+ var specs=p.specs||{};var sk=Object.entries(specs).slice(0,3);
2792
+ h+='<div style="background:#fff;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden;transition:all .2s;cursor:pointer" onclick="showDetail('+idx+')" onmouseover="this.style.transform=\'translateY(-2px)\';this.style.boxShadow=\'0 4px 12px rgba(0,0,0,.1)\'" onmouseout="this.style.transform=\'\';this.style.boxShadow=\'\'">';
2793
+ if(img)h+='<div style="height:90px;background:#f8fafc;display:flex;align-items:center;justify-content:center"><img src="'+img+'" style="max-width:100%;max-height:85px;object-fit:contain" onerror="this.parentElement.style.display=\'none\'"></div>';
2794
+ h+='<div style="padding:8px"><div style="font-size:11px;font-weight:600;color:#003f62;line-height:1.3;height:30px;overflow:hidden">'+name.substring(0,55)+'</div>';
2795
+ if(sku)h+='<div style="font-size:9px;color:#94a3b8;margin-top:2px">'+sku+'</div>';
2796
+ if(brand2)h+='<div style="font-size:9px;color:#64748b;margin-top:1px">'+brand2+'</div>';
2797
+ if(feats)h+='<div style="font-size:8.5px;color:#64748b;margin-top:2px;height:12px;overflow:hidden">'+feats+'</div>';
2798
+ if(sk.length){h+='<div style="margin-top:3px;font-size:8px;color:#555;border-top:1px solid #f0f0f0;padding-top:3px">';sk.forEach(function(kv){h+=kv[0]+': <b>'+kv[1]+'</b><br>'});h+='</div>'}
2799
+ h+='<div style="font-size:13px;font-weight:800;color:#059669;margin-top:4px">'+price+'</div>';
2800
+ h+='</div></div>';
 
 
2801
  });
2802
+ h+='</div>';res.innerHTML=h;
 
2803
  }
2804
  function doAIQuote(){
2805
  const q=document.getElementById('aiSearch').value.trim()||'bếp từ + máy hút mùi + chậu rửa khoảng 50 triệu';