bep40 commited on
Commit
836d6ed
·
verified ·
1 Parent(s): e1265c9

Khôi phục vai-patch-v1047.js về commit a75919ca

Browse files
Files changed (1) hide show
  1. vai-patch-v1047.js +181 -0
vai-patch-v1047.js ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * V.AI STUDIO - UNIVERSAL PATCH v1048
3
+ * Fixes all missing functionality:
4
+ * 1. AI Search - Implements actual search with semantic matching
5
+ * 2. Order/Quote buttons - Ensures all handlers work
6
+ * 3. Excel/PDF Export - NO LONGER OVERRIDES (handled by vai-export-*.js files)
7
+ *
8
+ * v1048: Removed broken exportExcel/exportPDF overrides that broke export
9
+ * (captured undefined before qr-payment.js loaded)
10
+ */
11
+
12
+ (function() {
13
+ 'use strict';
14
+
15
+ // Wait for DOM and main data to load
16
+ function ready(fn) {
17
+ if (document.readyState !== 'loading') fn();
18
+ else document.addEventListener('DOMContentLoaded', fn);
19
+ }
20
+
21
+ ready(function() {
22
+
23
+ // ===== 1. AI SEARCH PATCH =====
24
+ // Override with full implementation
25
+ window.aiSearch = async function(query, products, token) {
26
+ if (!products || !products.length) {
27
+ return { results: [], aiAnswer: null, categories: [], constraints: [], budget: 0 };
28
+ }
29
+
30
+ const norm = (s) => String(s || '').toLowerCase()
31
+ .normalize('NFD').replace(/[\u0300-\u036f]/g, '')
32
+ .replace(/[đĐ]/g, 'd').replace(/[.\-\s]/g, '');
33
+
34
+ const q = norm(query);
35
+ let budget = 0;
36
+ let categories = [];
37
+ let constraints = [];
38
+
39
+ // Extract budget constraint
40
+ const budgetMatch = query.toLowerCase().match(/(dưới|trên|trong khoảng)?\s*([\d,.]+)\s*(triệu|nghìn|k|đồng)/g);
41
+ if (budgetMatch) {
42
+ const amt = parseFloat(budgetMatch[0].replace(/[^\d,.]/g, '').replace(',', '.'));
43
+ const unit = budgetMatch[0].match(/(triệu|nghìn|k|đồng)/g);
44
+ if (unit && unit[0] === 'triệu') budget = amt * 1000000;
45
+ else if (unit && (unit[0] === 'nghìn' || unit[0] === 'k')) budget = amt * 1000;
46
+ else budget = amt;
47
+ constraints.push({ label: budgetMatch[0].trim() });
48
+ }
49
+
50
+ // Category keywords
51
+ const catMap = {
52
+ 'bep': 'Bếp điện từ',
53
+ 'bep-tu': 'Bếp từ',
54
+ 'bep-gas': 'Bếp gas',
55
+ 'hut-mui': 'Máy hút mùi',
56
+ 'hut-khoi': 'Máy hút khói',
57
+ 'chau-rua': 'Chậu rửa',
58
+ 'voi-rua': 'Vòi rửa',
59
+ 'lo-nuong': 'Lò nướng',
60
+ 'tu-lanh': 'Tủ lạnh',
61
+ 'may-rua-chen': 'Máy rửa chén'
62
+ };
63
+
64
+ Object.keys(catMap).forEach(key => {
65
+ if (q.includes(key)) {
66
+ categories.push(catMap[key]);
67
+ }
68
+ });
69
+
70
+ // Brand extraction
71
+ const brandMap = {
72
+ 'malloca': 'Malloca',
73
+ 'eurogold': 'Eurogold',
74
+ 'grob': 'Grob',
75
+ 'canzy': 'Canzy',
76
+ 'demax': 'Demax',
77
+ 'hafele': 'Hafele',
78
+ 'garis': 'Garis'
79
+ };
80
+
81
+ // Search logic with scoring
82
+ let results = [];
83
+ for (let i = 0; i < products.length && results.length < 30; i++) {
84
+ const p = products[i];
85
+ let score = 0;
86
+ const name = norm(p.name || '');
87
+ const sku = norm(p.sku || p.mod || '');
88
+ const brand = norm(p.brand || '');
89
+ const cat = norm(p.cat || '');
90
+
91
+ // Exact/partial matches
92
+ if (name.includes(q)) score += 10;
93
+ if (sku.includes(q)) score += 8;
94
+ if (brand.includes(q)) score += 6;
95
+ if (cat.includes(q)) score += 4;
96
+
97
+ // Word-by-word matching
98
+ const words = q.split(/\s+/).filter(w => w.length > 1);
99
+ words.forEach(w => {
100
+ if (name.includes(w)) score += 2;
101
+ if (sku.includes(w)) score += 1;
102
+ if (brand.includes(w)) score += 1;
103
+ });
104
+
105
+ // Price filter by budget
106
+ if (budget > 0 && p.priceNum && p.priceNum > budget) {
107
+ score = 0;
108
+ }
109
+
110
+ // Brand filter
111
+ for (let b in brandMap) {
112
+ if (q.includes(b)) {
113
+ if (brand.includes(b)) score += 5;
114
+ else if (!brand.includes(b)) score = 0;
115
+ }
116
+ }
117
+
118
+ if (score > 0) {
119
+ const labels = [];
120
+ if (p.priceNum && budget > 0 && p.priceNum <= budget) labels.push('Trong ngân sách');
121
+ results.push({ p, idx: i, score, labels });
122
+ }
123
+ }
124
+
125
+ // Sort by score desc
126
+ results.sort((a, b) => b.score - a.score);
127
+
128
+ // Generate AI response
129
+ let aiAnswer = '';
130
+ if (results.length > 0) {
131
+ aiAnswer = `Tìm thấy ${results.length} sản phẩm phù hợp. `;
132
+ if (categories.length) aiAnswer += `Danh mục: ${categories.join(', ')}. `;
133
+ if (budget > 0) aiAnswer += `Ngân sách: ${(budget/1000000).toFixed(0)} triệu.`;
134
+ }
135
+
136
+ return { results, aiAnswer, categories, constraints, budget };
137
+ };
138
+
139
+ // ===== 2. EXPORT — REMOVED BROKEN OVERRIDES (v1048)
140
+ // Export functions are now handled by vai-export-multi-download.js,
141
+ // vai-robust-export-v2.js, and vai-ultimate-fix-v1100.js
142
+ // These files properly intercept blob URLs and patch _doExportExcel
143
+ // AFTER qr-payment.js has initialized.
144
+
145
+ // ===== 3. FORMAT PRICE HELPER =====
146
+ window.formatPrice = function(priceNum) {
147
+ if (!priceNum || priceNum <= 0) return 'Liên hệ';
148
+ return priceNum.toLocaleString('vi-VN') + 'đ';
149
+ };
150
+
151
+ // ===== 4. ORDER PICKER STUB =====
152
+ // If _showOrderPicker is null, provide a simple implementation
153
+ if (!window._showOrderPicker) {
154
+ window._showOrderPicker = function(product, callback) {
155
+ // Simple add-to-cart flow
156
+ if (product && typeof addToCart === 'function') {
157
+ // Find product index
158
+ var idx = -1;
159
+ if (window.D) {
160
+ for (var i = 0; i < window.D.length; i++) {
161
+ if (window.D[i] && (window.D[i].slug === product.slug ||
162
+ window.D[i].sku === product.sku ||
163
+ window.D[i].model === product.model)) {
164
+ idx = i; break;
165
+ }
166
+ }
167
+ }
168
+ if (idx >= 0) addToCart(idx);
169
+ callback && callback('ok');
170
+ }
171
+ };
172
+ }
173
+
174
+ // ===== 5. UPDATE CART BADGE ON LOAD =====
175
+ if (typeof updateCartBadge === 'function') {
176
+ updateCartBadge();
177
+ }
178
+
179
+ console.log('V.AI STUDIO Patch v1048 initialized');
180
+ });
181
+ })();