bep40 commited on
Commit
0ee35de
·
verified ·
1 Parent(s): 0461039

Upload fix-order-detail-click.js

Browse files
Files changed (1) hide show
  1. fix-order-detail-click.js +217 -0
fix-order-detail-click.js ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Fix: Clicking saved order opens quote modal instead of order detail modal
3
+ * This patches the order search modal click handlers to ensure correct behavior
4
+ */
5
+ (function() {
6
+ 'use strict';
7
+
8
+ console.log('[Fix] Patching order detail click handlers...');
9
+
10
+ // Patch VAI_ORDERS.openSearch to ensure correct click handling
11
+ if (window.VAI_ORDERS && typeof window.VAI_ORDERS.openSearch === 'function') {
12
+ var originalOpenSearch = window.VAI_ORDERS.openSearch;
13
+ window.VAI_ORDERS.openSearch = function() {
14
+ originalOpenSearch.apply(this, arguments);
15
+ // After modal is created, patch the click handler
16
+ setTimeout(patchOrderListClicks, 100);
17
+ };
18
+ }
19
+
20
+ // Also patch the fallback in quote-ui.js if it exists
21
+ if (window.openOrdersFallback) {
22
+ var originalFallback = window.openOrdersFallback;
23
+ window.openOrdersFallback = function() {
24
+ originalFallback.apply(this, arguments);
25
+ setTimeout(patchOrderListClicks, 100);
26
+ };
27
+ }
28
+
29
+ function patchOrderListClicks() {
30
+ // Find the order search modal (both possible IDs)
31
+ var modal = document.getElementById('vai-order-search-modal');
32
+ if (!modal) return;
33
+
34
+ var resultsContainer = modal.querySelector('#vai-os-r, #vai-os-fb-r');
35
+ if (!resultsContainer) return;
36
+
37
+ // Remove existing click handlers by cloning
38
+ var newContainer = resultsContainer.cloneNode(true);
39
+ resultsContainer.parentNode.replaceChild(newContainer, resultsContainer);
40
+
41
+ // Add new click handler with proper event handling
42
+ newContainer.addEventListener('click', function(e) {
43
+ // Handle delete button
44
+ var deleteBtn = e.target.closest('.od, .od-fb');
45
+ if (deleteBtn) {
46
+ e.stopPropagation();
47
+ e.preventDefault();
48
+ var code = deleteBtn.dataset.c || deleteBtn.dataset.code;
49
+ if (code && confirm('Xóa đơn ' + code + '?')) {
50
+ if (window.VAI_ORDERS && typeof window.VAI_ORDERS.delete === 'function') {
51
+ window.VAI_ORDERS.delete(code);
52
+ } else {
53
+ var orders = JSON.parse(localStorage.getItem('vai_orders') || '[]');
54
+ orders = orders.filter(function(o) { return o.code !== code; });
55
+ localStorage.setItem('vai_orders', JSON.stringify(orders));
56
+ }
57
+ // Refresh the list
58
+ if (window.VAI_ORDERS && typeof window.VAI_ORDERS.openSearch === 'function') {
59
+ window.VAI_ORDERS.openSearch();
60
+ } else if (window.openOrdersFallback) {
61
+ window.openOrdersFallback();
62
+ }
63
+ }
64
+ return;
65
+ }
66
+
67
+ // Handle order item click - open detail
68
+ var orderItem = e.target.closest('.oi, .oi-fb');
69
+ if (orderItem) {
70
+ e.stopPropagation();
71
+ e.preventDefault();
72
+
73
+ // Close the search modal
74
+ var searchModal = document.getElementById('vai-order-search-modal');
75
+ if (searchModal) searchModal.remove();
76
+
77
+ // Ensure quote modal is closed
78
+ closeQuoteModal();
79
+
80
+ // Get order index
81
+ var idx = parseInt(orderItem.dataset.i);
82
+ if (isNaN(idx)) return;
83
+
84
+ // Get orders and open detail
85
+ var orders = [];
86
+ try {
87
+ orders = JSON.parse(localStorage.getItem('vai_orders') || '[]');
88
+ } catch (err) {}
89
+
90
+ var order = orders[idx];
91
+ if (!order) return;
92
+
93
+ // Open order detail using VAI_ORDERS
94
+ if (window.VAI_ORDERS && typeof window.VAI_ORDERS.openDetail === 'function') {
95
+ window.VAI_ORDERS.openDetail(order);
96
+ } else {
97
+ // Fallback: manually open detail
98
+ openOrderDetailFallback(order);
99
+ }
100
+ }
101
+ }, true); // Use capture phase to intercept early
102
+
103
+ console.log('[Fix] Order list click handlers patched');
104
+ }
105
+
106
+ function closeQuoteModal() {
107
+ var quoteOverlay = document.querySelector('.quote-overlay');
108
+ if (quoteOverlay) {
109
+ quoteOverlay.classList.remove('open');
110
+ quoteOverlay.style.display = 'none';
111
+ }
112
+ var quoteModal = document.querySelector('.quote-modal');
113
+ if (quoteModal) {
114
+ quoteModal.classList.remove('open');
115
+ quoteModal.style.display = 'none';
116
+ }
117
+ }
118
+
119
+ function openOrderDetailFallback(order) {
120
+ // Replicate the openOrderDetail logic from order-store.js
121
+ var ov = document.createElement('div');
122
+ ov.id = 'vai-order-detail-modal';
123
+ ov.style.cssText = 'position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:10000;display:flex;align-items:flex-start;justify-content:center;padding:16px;overflow-y:auto';
124
+ ov.onclick = function(e) { if (e.target === ov) ov.remove(); };
125
+ document.body.appendChild(ov);
126
+
127
+ var modal = document.createElement('div');
128
+ modal.style.cssText = 'background:#fff;border-radius:16px;max-width:750px;width:100%;max-height:92vh;overflow-y:auto';
129
+ ov.appendChild(modal);
130
+
131
+ // Simple detail view
132
+ var isC = order.status === 'confirmed';
133
+ var h = '<div style="padding:12px 18px;background:#003f62;border-radius:16px 16px 0 0;display:flex;justify-content:space-between;align-items:center">'
134
+ + '<div><b style="color:#fff">' + escapeHtml(order.code || '') + '</b> ' + (isC ? '<span style="background:#dcfce7;color:#16a34a;padding:2px 8px;border-radius:6px;font-size:10px;margin-left:8px">✅ Đã chốt</span>' : '<span style="background:#fef9c3;color:#92400e;padding:2px 8px;border-radius:6px;font-size:10px;margin-left:8px">⏳</span>') + '</div>'
135
+ + '<button id="xc" style="background:none;border:none;color:#fff;font-size:20px;cursor:pointer">✕</button></div>'
136
+ + '<div style="padding:14px 18px">';
137
+
138
+ // Customer info
139
+ h += '<div style="display:grid;grid-template-columns:1fr 1fr;gap:6px;font-size:11px;margin-bottom:10px;padding:10px;background:#f8fafc;border-radius:8px">'
140
+ + '<div><b>KH:</b> ' + escapeHtml(order.customer || '') + '</div>'
141
+ + '<div><b>SĐT:</b> ' + escapeHtml(order.phone || '') + '</div>'
142
+ + '<div style="grid-column:1/-1"><b>ĐC:</b> ' + escapeHtml(order.addr || '') + '</div>'
143
+ + '</div>';
144
+
145
+ // Items table
146
+ h += '<table style="width:100%;border-collapse:collapse;font-size:11px;margin-bottom:8px"><thead>'
147
+ + '<tr style="background:#003f62;color:#fff"><th style="padding:5px">#</th><th style="padding:5px">Ảnh</th><th style="padding:5px;text-align:left">SP</th><th style="padding:5px;text-align:left">Thông tin</th><th style="padding:5px">SL</th><th style="padding:5px;text-align:right">Giá niêm yết</th><th style="padding:5px;text-align:right">Giá CK</th><th style="padding:5px;text-align:right">TT</th></tr></thead><tbody>';
148
+
149
+ function fmt(n) { if (!n || isNaN(n)) return '0đ'; return Number(n).toLocaleString('vi-VN') + 'đ'; }
150
+ function orderItemInfo(it) {
151
+ it = it || {};
152
+ var v = it.info || it.thong_tin || it.dim_info || it.dimension_info || it.specs || it.summary || it.desc || '';
153
+ if (typeof v === 'string') {
154
+ var t = v.trim();
155
+ if (!t || t === '{}' || t === '[]') return '';
156
+ if ((t[0] === '{' && t[t.length-1] === '}') || (t[0] === '[' && t[t.length-1] === ']')) {
157
+ try { return orderItemInfo(JSON.parse(t)); } catch(e) { return t; }
158
+ }
159
+ return t;
160
+ }
161
+ if (Array.isArray(v)) return v.map(orderItemInfo).filter(Boolean).join('; ');
162
+ if (typeof v === 'object') {
163
+ var out = []; Object.keys(v).forEach(function(k) { var val = v[k]; if (val == null || val === '' || (typeof val === 'object' && !Array.isArray(val) && !Object.keys(val).length)) return; out.push(k + ': ' + orderItemInfo(val)); });
164
+ return out.join('; ');
165
+ }
166
+ return String(v);
167
+ }
168
+
169
+ (order.items || []).forEach(function(it, i) {
170
+ var ck = it.discPrice && it.price && it.discPrice < it.price;
171
+ h += '<tr style="border-bottom:1px solid #f1f5f9">'
172
+ + '<td style="padding:3px;text-align:center">' + (i+1) + '</td>'
173
+ + '<td style="padding:3px">' + (it.image ? '<img src="' + it.image + '" style="width:40px;height:40px;object-fit:contain;border-radius:4px" referrerpolicy="no-referrer" onerror="this.style.display=\'none\'">' : '') + '</td>'
174
+ + '<td style="padding:3px"><b style="font-size:10px">' + escapeHtml((it.name || '').substring(0,28)) + '</b><br><span style="font-size:9px;color:#64748b">' + escapeHtml(it.model || '') + '</span></td>'
175
+ + '<td style="padding:3px;font-size:9px;color:#64748b;max-width:150px">' + escapeHtml(orderItemInfo(it)).substring(0,260) + '</td>'
176
+ + '<td style="padding:3px;text-align:center">' + (it.qty || 1) + '</td>'
177
+ + '<td style="padding:3px;text-align:right">' + fmt(it.price) + '</td>'
178
+ + '<td style="padding:3px;text-align:right;' + (ck ? 'color:#dc3545;font-weight:700' : '') + '">' + fmt(it.discPrice || it.price) + '</td>'
179
+ + '<td style="padding:3px;text-align:right;font-weight:700">' + fmt(it.total) + '</td>'
180
+ + '</tr>';
181
+ });
182
+ h += '</tbody></table>';
183
+
184
+ // Totals
185
+ h += '<div style="padding:8px 12px;background:#003f62;border-radius:6px;display:flex;justify-content:space-between;margin-bottom:6px">'
186
+ + '<span style="color:#fff;font-weight:800">TỔNG</span>'
187
+ + '<span style="color:#f0b840;font-weight:900;font-size:15px">' + fmt(order.grandTotal) + '</span></div>';
188
+
189
+ if (order.deposit > 0) {
190
+ h += '<div style="padding:4px 10px;background:#f0fdf4;border-radius:6px;font-size:10px;margin-bottom:6px">'
191
+ + '<span>Cọc: <b>' + fmt(order.deposit) + '</b></span> | <span>Còn: <b style="color:#dc2626">' + fmt(order.remaining) + '</b></span></div>';
192
+ }
193
+
194
+ h += '</div>';
195
+ modal.innerHTML = h;
196
+
197
+ document.getElementById('xc').onclick = function() { ov.remove(); };
198
+ }
199
+
200
+ function escapeHtml(s) {
201
+ return (s || '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
202
+ }
203
+
204
+ // Also patch when DOM is ready
205
+ if (document.readyState === 'loading') {
206
+ document.addEventListener('DOMContentLoaded', function() {
207
+ setTimeout(patchOrderListClicks, 500);
208
+ });
209
+ } else {
210
+ setTimeout(patchOrderListClicks, 500);
211
+ }
212
+
213
+ // Periodic re-patch in case modals are recreated
214
+ setInterval(patchOrderListClicks, 5000);
215
+
216
+ console.log('[Fix] Order detail click fix loaded');
217
+ })();