-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab-dispatch-map-mode.js
More file actions
378 lines (338 loc) · 14.3 KB
/
Copy pathlab-dispatch-map-mode.js
File metadata and controls
378 lines (338 loc) · 14.3 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// ============================================================================
// BƯỚC 2 — DISPATCH MODE TRÊN BẢN ĐỒ (chiếu kết quả điều phối lên bản đồ nền)
// Hệ thống RRT-HCDC
// ----------------------------------------------------------------------------
// TÁI DÙNG hoàn toàn: engine (LabDispatch), Dispatch Modal (S.lastResult),
// lớp PXN (LabMapLayer). KHÔNG viết lại logic chấm điểm/OSRM.
//
// Ý tưởng: khi Dispatch Modal có kết quả VÀ đang ở trang Bản đồ, chiếu kết quả
// (điểm sự cố + Top 3 + tuyến OSRM) lên BẢN ĐỒ LỚN phía sau; làm mờ lớp nền để
// nổi bật. Bản đồ CHỈ ĐỂ XEM — chốt điều mẫu vẫn qua modal.
//
// NHÚNG (sau lab-dispatch-modal.js + lab-map-layer.js + map-module-v2.js):
// <script src="lab-dispatch-map-mode.js"></script>
//
// CÁCH KÍCH HOẠT (2 cách, chọn 1):
// (A) Tự động: mỗi khi modal render kết quả, tự chiếu lên bản đồ nếu đang mở
// trang bản đồ. → gọi window.LabDispatchMap.project(result, origin) từ
// renderResults() của modal (thêm 1 dòng — xem cuối file).
// (B) Thủ công: thêm nút "Xem trên bản đồ lớn" trong modal.
//
// PHỤ THUỘC: cần map instance. Module bản đồ v2 giữ `map` trong scope riêng;
// file này lấy map qua window._getLeafletMap() (thêm 1 dòng export — xem cuối).
// ============================================================================
(function () {
'use strict';
const esc = (s) =>
window.escapeHtml
? window.escapeHtml(String(s ?? ''))
: String(s ?? '').replace(
/[&<>"']/g,
(c) =>
({
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
}[c])
);
const RANK_COLORS = ['#16a34a', '#0ea5e9', '#f59e0b'];
const FALLBACK_COLOR = '#6b7280';
// Lớp riêng chứa mọi thứ vẽ ra ở dispatch mode (dễ xóa sạch)
let _dispatchLayer = null;
let _dimApplied = false;
let _mapRef = null;
function getMap() {
if (_mapRef) return _mapRef;
if (typeof window._getLeafletMap === 'function') {
_mapRef = window._getLeafletMap();
}
return _mapRef;
}
// Đảm bảo pane riêng biệt cho dispatch layer
function ensureDispatchPane(map) {
const paneName = 'dispatch-pane';
if (!map.getPane(paneName)) {
const pane = map.createPane(paneName);
pane.style.zIndex = '700';
pane.style.pointerEvents = 'all';
}
}
// Có đang ở trang bản đồ không (để quyết định có chiếu hay không)
function isMapPageVisible() {
// Đáng tin hơn: kiểm tra container bản đồ có kích thước thật trên màn hình.
// Không phụ thuộc cách app ẩn/hiện trang (style.display / class / v.v.)
const mapEl = document.getElementById('containerMap');
if (!mapEl) return false;
const rect = mapEl.getBoundingClientRect();
return rect.width > 0 && rect.height > 0; // đang hiển thị thật
}
// ------------------------------------------------------------------
// LÀM MỜ lớp nền để nổi bật dispatch (grayscale + giảm opacity)
// ------------------------------------------------------------------
function applyDim() {
const map = getMap();
if (!map || _dimApplied) return;
const container = map.getContainer();
// Làm mờ các pane nền, trừ dispatch-pane và popup-pane
const panesToDim = container.querySelectorAll(
'.leaflet-tile-pane, ' +
'.leaflet-overlay-pane:not([pane="dispatch-pane"]), ' +
'.leaflet-marker-pane:not([pane="dispatch-pane"])'
);
panesToDim.forEach((p) => {
p.style.transition = 'filter 0.3s, opacity 0.3s';
p.style.filter = 'grayscale(0.85)';
p.style.opacity = '0.45';
});
// Đảm bảo dispatch-pane và popup-pane không bị mờ
const dispatchPane = container.querySelector('[pane="dispatch-pane"]');
if (dispatchPane) {
dispatchPane.style.filter = '';
dispatchPane.style.opacity = '';
}
const popupPane = container.querySelector('.leaflet-popup-pane');
if (popupPane) {
popupPane.style.filter = '';
popupPane.style.opacity = '';
}
_dimApplied = true;
}
function removeDim() {
const map = getMap();
if (!map) return;
const container = map.getContainer();
const panes = container.querySelectorAll(
'.leaflet-tile-pane, .leaflet-overlay-pane, .leaflet-marker-pane, .leaflet-popup-pane'
);
panes.forEach((p) => {
p.style.filter = '';
p.style.opacity = '';
});
_dimApplied = false;
}
// ------------------------------------------------------------------
// Màu tải trọng ĐỘNG (giờ đã biết loại XN → tô chính xác)
// >=70% còn trống → xanh; 20–70% → vàng; <20% hoặc không đủ → đỏ
// ------------------------------------------------------------------
function loadColor(lab) {
const maxCap = lab.max_capacity_per_day || 0;
if (maxCap <= 0) return '#9ca3af';
const ratio = (lab.remaining_today ?? 0) / maxCap;
if (!lab.is_enough) return '#dc2626'; // không đủ cho lô này → đỏ
if (ratio >= 0.7) return '#16a34a'; // còn nhiều → xanh
if (ratio >= 0.2) return '#f59e0b'; // vơi → vàng
return '#dc2626'; // gần cạn → đỏ
}
// ------------------------------------------------------------------
// CHIẾU KẾT QUẢ LÊN BẢN ĐỒ
// result: object trả về từ LabDispatch.findBestLabs (S.lastResult)
// origin: { lat, lng, name }
// ------------------------------------------------------------------
window.LabDispatchMap = {
project: function (result, origin) {
const map = getMap();
if (!map || typeof L === 'undefined') {
console.warn('[dispatch-map] Không có map để chiếu.');
return;
}
if (!result || !result.ranked || !origin) return;
if (!isMapPageVisible()) return; // chỉ chiếu khi đang xem bản đồ
this.clear();
applyDim();
ensureDispatchPane(map);
_dispatchLayer = L.layerGroup().addTo(map);
// Gán lớp vào pane riêng để nổi bật
_dispatchLayer.getLayerId = () => 'dispatch-group';
_dispatchLayer.eachLayer((layer) => {
if (layer instanceof L.Polyline) {
layer.options.pane = 'dispatch-pane';
layer.redraw();
} else if (layer instanceof L.Marker) {
layer.options.pane = 'dispatch-pane';
}
});
// Marker điểm sự cố (A) — nổi bật trên nền mờ
const originMarker = L.marker([origin.lat, origin.lng], {
icon: L.divIcon({
className: '',
html: `<div style="background:#dc2626;color:#fff;border-radius:50%;width:30px;height:30px;
display:flex;align-items:center;justify-content:center;font-weight:bold;font-size:14px;
box-shadow:0 0 8px rgba(220,38,38,.8);border:2px solid #fff;">A</div>`,
iconSize: [30, 30],
iconAnchor: [15, 15],
}),
zIndexOffset: 1000,
pane: 'dispatch-pane',
}).addTo(_dispatchLayer);
originMarker.bindPopup(
`<b>Sự kiện khẩn cấp</b><br>${esc(origin.name || '')}`,
{
pane: 'popupPane', // giữ popup trong pane riêng
}
);
const top = result.top || [];
const bounds = [[origin.lat, origin.lng]];
// Vẽ Top 3: tuyến OSRM + marker màu hạng
top.forEach((lab) => {
const color =
lab.rank <= 3 ? RANK_COLORS[lab.rank - 1] : FALLBACK_COLOR;
const isTop1 = lab.rank === 1;
// Tuyến đường
if (lab.route?.geometry?.coordinates) {
const coords = lab.route.geometry.coordinates.map((c) => [
c[1],
c[0],
]);
L.polyline(coords, {
color,
weight: isTop1 ? 6 : 4,
opacity: isTop1 ? 0.95 : 0.6,
dashArray: isTop1 ? null : '8,6',
pane: 'dispatch-pane',
}).addTo(_dispatchLayer);
coords.forEach((c) => bounds.push(c));
}
// Marker PXN: viền = hạng, nền = tải trọng động
const fill = loadColor(lab);
const marker = L.marker([lab.lat, lab.lng], {
icon: L.divIcon({
className: '',
html: `<div style="background:${fill};border:3px solid ${color};color:#fff;border-radius:50%;
width:${isTop1 ? 34 : 28}px;height:${
isTop1 ? 34 : 28
}px;display:flex;align-items:center;
justify-content:center;font-weight:bold;font-size:${
isTop1 ? 15 : 12
}px;
box-shadow:0 0 6px rgba(0,0,0,.5);">${lab.rank}</div>`,
iconSize: [isTop1 ? 34 : 28, isTop1 ? 34 : 28],
iconAnchor: [isTop1 ? 17 : 14, isTop1 ? 17 : 14],
}),
zIndexOffset: 900 - lab.rank,
pane: 'dispatch-pane',
}).addTo(_dispatchLayer);
marker.bindPopup(
`
<div style="min-width:200px;font-family:'Inter',sans-serif;">
<div style="font-weight:bold;color:${color};">#${lab.rank} ${
lab.rank === 1 ? 'TỐI ƯU' : 'Dự phòng ' + lab.rank
}</div>
<div style="font-weight:bold;">${esc(lab.lab_name)}</div>
<div style="font-size:12px;color:#6b7280;">${esc(
lab.address || ''
)}</div>
<hr style="margin:6px 0;">
<div style="font-size:12px;line-height:1.6;">
<div><i class='bx bx-map-pin'></i> ${lab.route.km} km · ${
lab.route.minutes
} phút
${
lab.route.source === 'haversine'
? '<span style="color:#9ca3af;">(ước lượng)</span>'
: ''
}</div>
<div><i class='bx bx-timer'></i> Trả KQ: ${
lab.turnaround_hours != null ? lab.turnaround_hours + 'h' : '—'
}</div>
<div><i class='bx bx-box'></i> Còn nhận: <b style="color:${fill};">${
lab.remaining_today
}</b> mẫu
${
lab.is_enough
? ''
: '<span style="color:#dc2626;"> (không đủ)</span>'
}</div>
<div><i class='bx bx-bar-chart'></i> Điểm: <b>${
lab.scores.total
}</b></div>
</div>
<button class="btn btn-sm btn-outline-primary w-100 mt-2"
onclick="window.LabRouteSteps.show(${origin.lat}, ${origin.lng}, ${
lab.lat
}, ${lab.lng}, '${esc(lab.lab_name).replace(/'/g, "\\'")}')">
<i class='bx bx-directions'></i> Xem chỉ đường
</button>
</div>`,
{ maxWidth: 260 }
);
bounds.push([lab.lat, lab.lng]);
if (isTop1) setTimeout(() => marker.openPopup(), 300);
});
// Ôm trọn điểm sự cố + Top 3
if (bounds.length > 1) {
try {
map.fitBounds(L.latLngBounds(bounds).pad(0.2));
} catch (_) {}
}
// Nút "Thoát chế độ điều phối" nổi trên bản đồ
_addExitButton();
},
clear: function () {
const map = getMap();
if (_dispatchLayer && map) {
map.removeLayer(_dispatchLayer);
_dispatchLayer = null;
}
_removeExitButton();
removeDim();
},
};
// ------------------------------------------------------------------
// Nút thoát chế độ điều phối (nổi góc trái dưới bản đồ)
// ------------------------------------------------------------------
let _exitCtrl = null;
function _addExitButton() {
const map = getMap();
if (!map || _exitCtrl) return;
_exitCtrl = L.control({ position: 'bottomleft' });
_exitCtrl.onAdd = function () {
const div = L.DomUtil.create('div', '');
div.innerHTML = `
<button style="background:#006a75;color:#fff;border:none;padding:8px 14px;border-radius:8px;
font-weight:bold;box-shadow:0 2px 6px rgba(0,0,0,.3);cursor:pointer;">
<i class='bx bx-x'></i> Thoát chế độ điều phối
</button>`;
L.DomEvent.disableClickPropagation(div);
div.querySelector('button').onclick = () => window.LabDispatchMap.clear();
return div;
};
_exitCtrl.addTo(map);
}
function _removeExitButton() {
if (_exitCtrl) {
try {
_exitCtrl.remove();
} catch (_) {}
_exitCtrl = null;
}
}
console.log(
'[lab-dispatch-map-mode.js] ✅ Dispatch Mode trên bản đồ sẵn sàng. (window.LabDispatchMap)'
);
})();
/* ============================================================================
TÍCH HỢP — thêm 2 dòng nhỏ, KHÔNG viết lại logic
----------------------------------------------------------------------------
(1) EXPORT map từ module bản đồ v2:
Trong map-module-v2.js, thêm vào cuối (trong scope có biến `map`):
window._getLeafletMap = function () { return map; };
(2) CHIẾU kết quả khi modal render xong:
Trong lab-dispatch-modal.js, hàm renderResults(), NGAY SAU khi đã gọi
renderDispatchActions + showPendingSuggestions (cuối hàm), thêm:
// Chiếu kết quả lên bản đồ nền nếu đang ở trang Bản đồ (Bước 2)
if (window.LabDispatchMap && ranked.length) {
window.LabDispatchMap.project(result, {
lat: S.lat, lng: S.lng, name: S.incidentName || 'Điểm sự kiện',
});
}
(3) DỌN khi đóng modal:
Trong lab-dispatch-modal.js, phần 'hidden.bs.modal' của dispatchModal,
thêm 1 dòng (không bắt buộc — nếu muốn giữ hình trên bản đồ sau khi đóng
modal thì BỎ QUA bước này):
if (window.LabDispatchMap) window.LabDispatchMap.clear();
→ Khuyến nghị: KHÔNG clear khi đóng modal, để admin đóng modal vẫn xem
được Top 3 + tuyến trên bản đồ lớn. Admin tự bấm "Thoát chế độ điều
phối" khi xong. (Đúng tinh thần "bản đồ để xem".)
============================================================================ */