Spaces:
Sleeping

doluong6 commited on
Commit
cb3e892
·
verified ·
1 Parent(s): c3a0846

Upload 2 files

Browse files
Files changed (2) hide show
  1. static/script (4).js +801 -0
  2. static/style.css +0 -103
static/script (4).js ADDED
@@ -0,0 +1,801 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function login() {
2
+ const u = document.getElementById("username").value;
3
+ const p = document.getElementById("password").value;
4
+
5
+ if (u === "admin" && p === "1234") {
6
+ document.getElementById("login").style.display = "none";
7
+ document.getElementById("dashboard").style.display = "block";
8
+ enableSubmenuClick();
9
+
10
+ // ❗❗ Ẩn tất cả các phần fade-in trước
11
+ document.querySelectorAll(".fade-in").forEach(el => {
12
+ if (el) el.style.display = "none";
13
+ });
14
+
15
+ // ✅ Sau đó hiện Hồ sơ năng lực
16
+ const hs = document.getElementById("Nl");
17
+ if (hs) {
18
+ hs.style.display = "block";
19
+ hs.scrollIntoView({ behavior: "smooth" });
20
+ }
21
+
22
+ } else {
23
+ alert("Sai tên đăng nhập hoặc mật khẩu!");
24
+ }
25
+ }
26
+ const BACKEND_URL = "https://doluong6-qlht.hf.space";
27
+
28
+ // ==== Hiển thị nội dung theo mục được chọn ====
29
+ function chonMuc(muc) {
30
+ // Ẩn tất cả phần tử có class .fade-in
31
+ document.querySelectorAll(".fade-in").forEach(el => {
32
+ if (el) el.style.display = "none";
33
+ });
34
+
35
+ // Hiển thị mục 'Chờ xử lý'
36
+ if (muc === "pycload") {
37
+ const stat = document.getElementById("pycload");
38
+ if (stat) stat.style.display = "flex";
39
+ hienThiDanhSachPYC();
40
+ }
41
+
42
+ // Hiển thị mục 'Đã hoàn thành'
43
+ if (muc === "pycend") {
44
+ const stat = document.getElementById("pycend");
45
+ if (stat) stat.style.display = "flex";
46
+ hienThiSoPYCHoanThanh();
47
+ }
48
+ if (muc === "pycdt") {
49
+ const stat = document.getElementById("pycdt");
50
+ if (stat) stat.style.display = "flex";
51
+ hienThiDoanhThuPYC();
52
+ }
53
+ if (muc === "pyc") {
54
+ const section = document.getElementById("pyc");
55
+ if (section) section.style.display = "block";
56
+ hienThiDanhSachPYCChuaHoanThanh();
57
+ }
58
+ if (muc === "dttong") {
59
+ const stat = document.getElementById("dttong");
60
+ if (stat) stat.style.display = "flex";
61
+ hienThiDoanhThuTong(); // <--- Hàm này phải được định nghĩa
62
+ }
63
+ if (muc === "dtthang") {
64
+ const stat = document.getElementById("dtthang");
65
+ if (stat) stat.style.display = "block";
66
+ hienThiDoanhThuTheoThang();
67
+ }
68
+ if (muc === "Nl") {
69
+ const section = document.getElementById("Nl");
70
+ if (section) section.style.display = "block";
71
+ }
72
+ if (muc === "dtdichvu") {
73
+ document.getElementById("dtdichvu").style.display = "block";
74
+ hienThiDoanhThuDichVu(); // Gọi API để hiển thị bảng
75
+ }
76
+ if (muc === "dtnoibo") {
77
+ document.getElementById("dtnoibo").style.display = "block";
78
+ hienThiDoanhThuNoiBo();
79
+ }
80
+ if (muc === "tailieu") {
81
+ const section = document.getElementById("tailieu");
82
+ if (section) {
83
+ section.style.display = "block";
84
+ taiDanhSachPDF();
85
+ hienThiPDF();// ✅ Gọi API lấy danh sách PDF
86
+ }
87
+ }
88
+ if (muc === "qt") {
89
+ const section = document.getElementById("qt");
90
+ if (section) {
91
+ section.style.display = "block";
92
+ taiDanhSachQuyTrinh();
93
+ hienThiQT();
94
+ }
95
+ }
96
+ if (muc === "dm") {
97
+ const section = document.getElementById("dm");
98
+ if (section) section.style.display = "block"; // ✅ hiện vùng hiển thị
99
+ fetch(`${BACKEND_URL}/danh_muc_thiet_bi`)
100
+ .then(res => {
101
+ if (!res.ok) throw new Error("Lỗi từ server: " + res.status);
102
+ return res.json();
103
+ })
104
+ .then(data => {
105
+ const table = $('#bangDanhMucThietBi');
106
+ table.empty();
107
+
108
+ if (!Array.isArray(data) || data.length === 0) {
109
+ table.html("<p style='color:red'>Không có dữ liệu để hiển thị.</p>");
110
+ return;
111
+ }
112
+
113
+ if (!$.fn.DataTable.isDataTable('#bangDanhMucThietBi')) {
114
+ table.DataTable({
115
+ data: data,
116
+ columns: Object.keys(data[0] || {}).map(key => ({ title: key, data: key })),
117
+ paging: true,
118
+ searching: true,
119
+ scrollX: true,
120
+ responsive: true,
121
+ autoWidth: false
122
+ });
123
+ } else {
124
+ const dt = table.DataTable();
125
+ dt.clear().rows.add(data).draw();
126
+ }
127
+ })
128
+ .catch(err => {
129
+ console.error("Lỗi khi tải danh mục thiết bị:", err);
130
+ $('#bangDanhMucThietBi').html("<p style='color:red'>Không thể tải dữ liệu.</p>");
131
+ });
132
+ }
133
+ if (muc === "qlhc") {
134
+ const section = document.getElementById("qlhc");
135
+ if (section) {
136
+ section.style.display = "block";
137
+ hienThiHoaChat();
138
+ }
139
+ }
140
+ if (muc === "hsl") {
141
+ const section = document.getElementById("hsl");
142
+ if (section) section.style.display = "block";
143
+ hienThiHSKH();
144
+ }
145
+ }
146
+
147
+
148
+
149
+ async function hienThiDanhSachPYC() {
150
+ try {
151
+ const res = await fetch("/dem_pyc_tat_ca");
152
+ const data = await res.json();
153
+ console.log("DATA ĐẾM PYC:", data);
154
+
155
+ if (data.tong_so_pyc !== undefined) {
156
+ document.getElementById("pycCount").textContent = `${data.tong_so_pyc} phiếu`;
157
+ } else {
158
+ document.getElementById("pycCount").textContent = "Không xác định";
159
+ }
160
+ } catch (err) {
161
+ console.error("Lỗi khi gọi API PYC:", err);
162
+ document.getElementById("pycCount").textContent = "Lỗi";
163
+ }
164
+ }
165
+
166
+ async function hienThiSoPYCHoanThanh() {
167
+ try {
168
+ const res = await fetch(`${BACKEND_URL}/dem_pyc_hoan_thanh`);
169
+ const data = await res.json();
170
+
171
+ if (res.ok && data.tong_so_hoan_thanh !== undefined) {
172
+ document.getElementById("completedRequests").textContent = data.tong_so_hoan_thanh + " phiếu";
173
+ } else {
174
+ document.getElementById("completedRequests").textContent = "Không tải được";
175
+ }
176
+ } catch (err) {
177
+ console.error("Lỗi khi gọi API hoàn thành:", err);
178
+ document.getElementById("completedRequests").textContent = "Lỗi";
179
+ }
180
+ }
181
+ async function hienThiDoanhThuPYC() {
182
+ try {
183
+ const res = await fetch(`${BACKEND_URL}/tong_doanh_thu_pyc`);
184
+ const data = await res.json();
185
+
186
+ console.log("DATA DOANH THU:", data); // ✅ Debug console
187
+
188
+ if (res.ok && data.tong_doanh_thu !== undefined) {
189
+ const formatted = Number(data.tong_doanh_thu).toLocaleString("vi-VN") + " VND";
190
+ document.getElementById("requestRevenue").textContent = formatted;
191
+ } else {
192
+ document.getElementById("requestRevenue").textContent = "Không tải được";
193
+ }
194
+ } catch (err) {
195
+ console.error("Lỗi khi gọi API doanh thu:", err);
196
+ document.getElementById("requestRevenue").textContent = "Lỗi";
197
+ }
198
+ }
199
+
200
+ function enableSubmenuClick() {
201
+ document.querySelectorAll(".menu-item.has-sub").forEach(item => {
202
+ item.addEventListener("click", () => {
203
+ const id = item.getAttribute("data-submenu");
204
+ const submenu = document.getElementById(id);
205
+ const arrow = document.getElementById("arrow-" + id);
206
+
207
+ if (submenu.style.display === "block") {
208
+ submenu.style.display = "none";
209
+ arrow.innerHTML = "►";
210
+ } else {
211
+ submenu.style.display = "block";
212
+ arrow.innerHTML = "▼";
213
+ }
214
+ });
215
+ });
216
+ }
217
+ async function hienThiDanhSachPYCChuaHoanThanh() {
218
+ const container = document.getElementById("pycIncompleteList");
219
+ container.innerHTML = "Đang tải dữ liệu...";
220
+ try {
221
+ const res = await fetch(`${BACKEND_URL}/pyc_chua_hoan_thanh`);
222
+ const data = await res.json();
223
+
224
+ if (!Array.isArray(data) || data.length === 0) {
225
+ container.innerHTML = "Không có dữ liệu.";
226
+ return;
227
+ }
228
+
229
+ let html = "<table class='display' id='tablePYCIncomplete'><thead><tr>";
230
+ for (let key in data[0]) {
231
+ html += `<th>${key}</th>`;
232
+ }
233
+ html += "</tr></thead><tbody>";
234
+
235
+ for (let row of data) {
236
+ html += "<tr>";
237
+ for (let key in row) {
238
+ html += `<td>${row[key]}</td>`;
239
+ }
240
+ html += "</tr>";
241
+ }
242
+
243
+ html += "</tbody></table>";
244
+ container.innerHTML = html;
245
+
246
+ $(document).ready(function () {
247
+ $('#tablePYCIncomplete').DataTable();
248
+ });
249
+ } catch (err) {
250
+ console.error("Lỗi khi tải bảng phiếu chưa hoàn thành:", err);
251
+ container.innerHTML = "Lỗi khi tải dữ liệu.";
252
+ }
253
+ }
254
+ async function hienThiDoanhThuTong() {
255
+ try {
256
+ const res = await fetch(`${BACKEND_URL}/doanh_thu_tong`);
257
+ const data = await res.json();
258
+
259
+ if (res.ok && data.tong_doanh_thu !== undefined) {
260
+ const formatted = Number(data.tong_doanh_thu).toLocaleString("vi-VN") + " VND";
261
+ document.getElementById("tongDoanhThu").textContent = formatted;
262
+ } else {
263
+ document.getElementById("tongDoanhThu").textContent = "Không tải được";
264
+ }
265
+ } catch (err) {
266
+ console.error("Lỗi khi gọi API doanh thu tổng:", err);
267
+ document.getElementById("tongDoanhThu").textContent = "Lỗi";
268
+ }
269
+ }
270
+ async function hienThiDoanhThuTheoThang() {
271
+ const ctx = document.getElementById("chartDoanhThuThang").getContext("2d");
272
+ try {
273
+ const res = await fetch(`${BACKEND_URL}/doanh_thu_theo_thang`);
274
+ const data = await res.json();
275
+
276
+ if (!res.ok || !data || !Array.isArray(data.thang) || !Array.isArray(data.doanh_thu)) {
277
+ throw new Error("Dữ liệu không hợp lệ");
278
+ }
279
+
280
+ // Xóa biểu đồ cũ nếu đã có
281
+ if (window.doanhThuChart) {
282
+ window.doanhThuChart.destroy();
283
+ }
284
+
285
+ // Vẽ biểu đồ cột
286
+ window.doanhThuChart = new Chart(ctx, {
287
+ type: "bar",
288
+ data: {
289
+ labels: data.thang,
290
+ datasets: [{
291
+ label: "Doanh thu (VND)",
292
+ data: data.doanh_thu,
293
+ backgroundColor: "rgba(54, 162, 235, 0.6)",
294
+ borderColor: "rgba(54, 162, 235, 1)",
295
+ borderWidth: 1
296
+ }]
297
+ },
298
+ options: {
299
+ scales: {
300
+ y: {
301
+ beginAtZero: true,
302
+ ticks: {
303
+ callback: function(value) {
304
+ return value.toLocaleString("vi-VN") + " VND";
305
+ }
306
+ }
307
+ }
308
+ },
309
+ plugins: {
310
+ legend: {
311
+ display: false
312
+ },
313
+ tooltip: {
314
+ callbacks: {
315
+ label: function(context) {
316
+ return context.raw.toLocaleString("vi-VN") + " VND";
317
+ }
318
+ }
319
+ }
320
+ }
321
+ }
322
+ });
323
+
324
+ } catch (err) {
325
+ console.error("Lỗi khi gọi API doanh thu theo tháng:", err);
326
+ document.getElementById("chartDoanhThuThang").outerHTML = "<p>Lỗi khi tải biểu đồ.</p>";
327
+ }
328
+ }
329
+ async function hienThiDoanhThuDichVu() {
330
+ try {
331
+ const res = await fetch("/doanh_thu_dich_vu");
332
+ const result = await res.json();
333
+
334
+ console.log("📊 Kết quả từ API doanh_thu_dich_vu:", result);
335
+
336
+ if (!result || result.error) {
337
+ document.querySelector("#anhDoanhThuDichVu").alt = "Lỗi khi tải biểu đồ.";
338
+ $('#bangDoanhThuDichVu').html("<p style='color:red'>Không thể tải dữ liệu.</p>");
339
+ return;
340
+ }
341
+
342
+ const data = result.data || [];
343
+ const imageBase64 = result.image_base64;
344
+
345
+ // Hiển thị ảnh biểu đồ
346
+ const imgElement = document.querySelector("#anhDoanhThuDichVu");
347
+ if (imageBase64) {
348
+ imgElement.src = `data:image/png;base64,${imageBase64}`;
349
+ imgElement.alt = "Biểu đồ doanh thu dịch vụ";
350
+ } else {
351
+ imgElement.alt = "Không có ảnh biểu đồ.";
352
+ }
353
+
354
+ // Hiển thị bảng dữ liệu
355
+ const table = $('#bangDoanhThuDichVu');
356
+ if ($.fn.dataTable.isDataTable('#bangDoanhThuDichVu')) {
357
+ table.DataTable().clear().rows.add(data).draw();
358
+ } else {
359
+ table.DataTable({
360
+ data: data,
361
+ columns: [
362
+ { title: "Dịch vụ", data: "Dịch vụ" },
363
+ { title: "Số lượng", data: "Số lượng" },
364
+ { title: "Tỷ lệ (%)", data: "Tỷ lệ (%)" },
365
+ {
366
+ title: "Tổng doanh thu",
367
+ data: "Tổng doanh thu",
368
+ render: function (data, type, row) {
369
+ if (type === 'display' && typeof data === 'number') {
370
+ return data.toLocaleString('vi-VN') + " ₫";
371
+ }
372
+ return data;
373
+ }
374
+ }
375
+ ],
376
+ paging: false,
377
+ searching: false,
378
+ info: false
379
+ });
380
+ }
381
+
382
+ } catch (error) {
383
+ console.error("❌ Lỗi khi tải dữ liệu doanh thu dịch vụ:", error);
384
+ document.querySelector("#anhDoanhThuDichVu").alt = "Lỗi kết nối tới API.";
385
+ $('#bangDoanhThuDichVu').html("<p style='color:red'>Lỗi kết nối tới máy chủ.</p>");
386
+ }
387
+ }
388
+ async function hienThiDoanhThuNoiBo() {
389
+ try {
390
+ const res = await fetch(`${BACKEND_URL}/doanh_thu_noi_bo`);
391
+ const result = await res.json();
392
+
393
+ console.log("📊 Kết quả doanh thu nội bộ:", result);
394
+
395
+ const data = result.data || [];
396
+ const tong = result.tong || {};
397
+
398
+ // Hiển thị bảng
399
+ const table = $('#bangDoanhThuNoiBo');
400
+ if ($.fn.dataTable.isDataTable('#bangDoanhThuNoiBo')) {
401
+ table.DataTable().clear().rows.add(data).draw();
402
+ } else {
403
+ table.DataTable({
404
+ data: data,
405
+ columns: [
406
+ { title: "Phòng", data: "Phòng" },
407
+ {
408
+ title: "Chuyển đến",
409
+ data: "Chuyển đến",
410
+ render: function (data) {
411
+ return typeof data === 'number' ? data.toLocaleString('vi-VN') + " ₫" : "-";
412
+ }
413
+ },
414
+ {
415
+ title: "Chuyển đi",
416
+ data: "Chuyển đi",
417
+ render: function (data) {
418
+ return typeof data === 'number' ? data.toLocaleString('vi-VN') + " ₫" : "-";
419
+ }
420
+ }
421
+ ],
422
+ paging: false,
423
+ searching: false,
424
+ info: false
425
+ });
426
+ }
427
+
428
+ // Hiển thị tổng
429
+ const cdStr = typeof tong["Tổng chuyển đến"] === 'number' ? tong["Tổng chuyển đến"].toLocaleString('vi-VN') + " ₫" : "-";
430
+ const diStr = typeof tong["Tổng chuyển đi"] === 'number' ? tong["Tổng chuyển đi"].toLocaleString('vi-VN') + " ₫" : "-";
431
+
432
+ document.getElementById("tongDoanhThuNoiBo").innerHTML = `
433
+ <p>Tổng chuyển đến: <strong>${cdStr}</strong></p>
434
+ <p>Tổng chuyển đi: <strong>${diStr}</strong></p>
435
+ `;
436
+
437
+ } catch (error) {
438
+ console.error("❌ Lỗi khi tải doanh thu nội bộ:", error);
439
+ document.getElementById("tongDoanhThuNoiBo").innerHTML = "<p style='color:red'>Không thể tải dữ liệu nội bộ.</p>";
440
+ }
441
+ }
442
+ // Gọi API lấy danh sách PDF từ backend
443
+ async function taiDanhSachPDF() {
444
+ try {
445
+ const res = await fetch(`${BACKEND_URL}/danh_sach_pdf`);
446
+ const data = await res.json();
447
+
448
+ const select = document.getElementById("selectPdf");
449
+ select.innerHTML = '<option value="">-- Chọn tài liệu để xem --</option>';
450
+
451
+ data.pdfs.forEach(file => {
452
+ const option = document.createElement("option");
453
+ option.value = file;
454
+ option.textContent = file;
455
+ select.appendChild(option);
456
+ });
457
+ } catch (err) {
458
+ console.error("Không thể tải danh sách PDF:", err);
459
+ }
460
+ }
461
+
462
+ // Xử lý hiển thị PDF khi chọn trong dropdown
463
+ function hienThiPDF() {
464
+ const select = document.getElementById("selectPdf");
465
+ const fileName = select.value;
466
+ const viewer = document.getElementById("pdfViewer");
467
+ const iframe = document.getElementById("pdfFrame");
468
+ const download = document.getElementById("pdfDownload");
469
+
470
+ if (!fileName) {
471
+ viewer.style.display = "none";
472
+ return;
473
+ }
474
+
475
+ const pdfUrl = `${BACKEND_URL}/xem_pdf?file_name=${encodeURIComponent(fileName)}`;
476
+ iframe.src = pdfUrl;
477
+ download.href = pdfUrl;
478
+ viewer.style.display = "block";
479
+ }
480
+
481
+ async function taiDanhSachQuyTrinh() {
482
+ try {
483
+ const res = await fetch(`${BACKEND_URL}/quy_trinh`);
484
+ const data = await res.json();
485
+
486
+ const select = document.getElementById("selectQT");
487
+ select.innerHTML = `<option value="">-- Chọn tài liệu --</option>`;
488
+
489
+ data.pdfs.forEach(item => {
490
+ let fileName, filePath;
491
+ if (typeof item === "string") {
492
+ fileName = item;
493
+ filePath = `Quy_trinh/${item}`;
494
+ } else {
495
+ fileName = item.name;
496
+ filePath = item.path;
497
+ }
498
+
499
+ const option = document.createElement("option");
500
+ option.value = filePath;
501
+ option.textContent = fileName;
502
+ select.appendChild(option);
503
+ });
504
+ } catch (error) {
505
+ console.error("Lỗi khi tải danh sách quy trình:", error);
506
+ alert("Không thể tải danh sách quy trình.");
507
+ }
508
+ }
509
+ function hienThiQT() {
510
+ const file = document.getElementById("selectQT").value;
511
+ const iframe = document.getElementById("qtFrame");
512
+ const viewer = document.getElementById("qtViewer");
513
+ const download = document.getElementById("qtDownload");
514
+
515
+ if (file) {
516
+ const ext = file.toLowerCase().split('.').pop();
517
+ const url = `${BACKEND_URL}/xem_pdf?file=${encodeURIComponent(file)}`;
518
+
519
+ if (ext === "pdf") {
520
+ iframe.src = url;
521
+ iframe.style.display = "block";
522
+ } else {
523
+ iframe.src = "";
524
+ iframe.style.display = "none";
525
+ alert("Không hỗ trợ xem trực tiếp định dạng này. Vui lòng tải về.");
526
+ }
527
+
528
+ download.href = url + "&download=true";
529
+ viewer.style.display = "block";
530
+ } else {
531
+ viewer.style.display = "none";
532
+ }
533
+ }
534
+ function parseNgayVN(ngayStr) {
535
+ if (!ngayStr || typeof ngayStr !== 'string') return null;
536
+ ngayStr = ngayStr.trim().split(" ")[0];
537
+
538
+ if (/^\d{4}-\d{2}-\d{2}$/.test(ngayStr)) return new Date(ngayStr);
539
+
540
+ const parts = ngayStr.split(/\/|-/);
541
+ if (parts.length === 3) {
542
+ let [d, m, y] = parts;
543
+ if (y.length === 2) y = '20' + y;
544
+ return new Date(`${y}-${m.padStart(2, '0')}-${d.padStart(2, '0')}`);
545
+ }
546
+ return null;
547
+ }
548
+
549
+ async function hienThiHoaChat() {
550
+ try {
551
+ const res = await fetch("/quan_ly_hoa_chat");
552
+ if (!res.ok) throw new Error("Không thể tải dữ liệu hóa chất");
553
+
554
+ const data = await res.json();
555
+ const columns = data.columns;
556
+ const rows = data.rows;
557
+
558
+ const idxHanSD = columns.findIndex(c => c.toUpperCase().includes("HẠN"));
559
+ const idxTinhTrang = columns.findIndex(c => c.toUpperCase().includes("TÌNH TRẠNG"));
560
+
561
+ const table = document.getElementById("bangHoaChat");
562
+ table.innerHTML = "";
563
+
564
+ const thead = table.createTHead();
565
+ const headerRow = thead.insertRow();
566
+ columns.forEach(col => {
567
+ const th = document.createElement("th");
568
+ th.textContent = col;
569
+ th.style.background = "#f2f2f2";
570
+ th.style.padding = "8px";
571
+ th.style.border = "1px solid #ccc";
572
+ headerRow.appendChild(th);
573
+ });
574
+
575
+ const tbody = table.createTBody();
576
+ rows.forEach(row => {
577
+ const tr = tbody.insertRow();
578
+ row.forEach((cell, colIndex) => {
579
+ const td = tr.insertCell();
580
+ const input = document.createElement("input");
581
+ input.type = "text";
582
+ input.value = cell;
583
+ input.classList.add("input-cell");
584
+ input.style.width = "100%";
585
+
586
+ // Chỉ xử lý ô tình trạng
587
+ if (colIndex === idxTinhTrang) {
588
+ input.readOnly = true;
589
+ input.style.fontWeight = "bold";
590
+
591
+ const hanSD = parseNgayVN(row[idxHanSD]);
592
+ let status = "Không xác định";
593
+ let color = "black";
594
+
595
+ if (hanSD instanceof Date && !isNaN(hanSD)) {
596
+ const today = new Date();
597
+ const diffDays = Math.ceil((hanSD - today) / (1000 * 3600 * 24));
598
+
599
+ if (diffDays < 0) {
600
+ status = "HẾT HẠN";
601
+ color = "red";
602
+ } else if (diffDays <= 30) {
603
+ status = `CÒN: ${diffDays} ngày`;
604
+ color = "orange";
605
+ } else {
606
+ status = `CÒN: ${diffDays} ngày`;
607
+ color = "green";
608
+ }
609
+ }
610
+
611
+ input.value = status;
612
+ input.style.color = color;
613
+ }
614
+
615
+ td.appendChild(input);
616
+ });
617
+ });
618
+
619
+ document.getElementById("nutLuuHoaChat").style.display = "block";
620
+ } catch (err) {
621
+ console.error("Lỗi khi tải danh mục hóa chất:", err);
622
+ alert("❌ Không thể tải dữ liệu hóa chất.");
623
+ }
624
+ }
625
+
626
+
627
+
628
+
629
+
630
+ // ==== Lưu dữ liệu đã chỉnh sửa ====
631
+ async function luuHoaChat() {
632
+ const table = document.getElementById("bangHoaChat");
633
+ if (!table) return;
634
+
635
+ const headers = Array.from(table.querySelectorAll("thead th")).map(th => th.textContent.trim());
636
+ const data = [];
637
+
638
+ table.querySelectorAll("tbody tr").forEach(tr => {
639
+ const row = Array.from(tr.querySelectorAll("td input")).map(input => input.value.trim());
640
+ if (row.length === headers.length) {
641
+ data.push(row);
642
+ }
643
+ });
644
+
645
+ console.log("🧪 Gửi:", { columns: headers, data: data });
646
+
647
+ try {
648
+ const res = await fetch(`${BACKEND_URL}/cap_nhat_hoa_chat`, {
649
+ method: "POST",
650
+ headers: { "Content-Type": "application/json" },
651
+ body: JSON.stringify({ columns: headers, data: data })
652
+ });
653
+
654
+ const result = await res.json();
655
+ alert(result.message || "✅ Đã lưu dữ liệu thành công!");
656
+ } catch (err) {
657
+ console.error("❌ Lỗi khi lưu hóa chất:", err);
658
+ alert("❌ Không thể lưu dữ liệu.");
659
+ }
660
+ }
661
+ // Gọi API hiển thị HSKH
662
+ async function hienThiHSKH() {
663
+ try {
664
+ const response = await fetch(`${BACKEND_URL}/hien_thi_hskh`);
665
+ const data = await response.json();
666
+
667
+ if (!data || !data.rows || data.rows.length === 0) {
668
+ console.warn("Không có dữ liệu HSKH.");
669
+ return;
670
+ }
671
+
672
+ const fullIndex = data.columns.indexOf("Full");
673
+
674
+ if ($.fn.DataTable.isDataTable("#bangHSKH")) {
675
+ $('#bangHSKH').DataTable().clear().destroy();
676
+ }
677
+
678
+ let tbodyHtml = "";
679
+ data.rows.forEach(row => {
680
+ tbodyHtml += `<tr>`;
681
+
682
+ for (let j = 0; j < data.columns.length; j++) {
683
+ const col = data.columns[j];
684
+ const cell = row[j] || "";
685
+
686
+ if (["Full", "PYC", "BB", "GCN"].includes(col)) {
687
+ const checked = cell.toLowerCase() === "x" ? "checked" : "";
688
+ const className = col === "Full" ? "checkbox-full" : "checkbox-cell";
689
+ tbodyHtml += `<td><input type="checkbox" class="${className}" ${checked}></td>`;
690
+ } else {
691
+ tbodyHtml += `<td><input class="input-cell" type="text" value="${cell}" /></td>`;
692
+ }
693
+ }
694
+
695
+ tbodyHtml += `</tr>`;
696
+ });
697
+
698
+ $("#bangHSKH tbody").html(tbodyHtml);
699
+
700
+ $("#bangHSKH").DataTable({
701
+ scrollX: true,
702
+ autoWidth: false,
703
+ pageLength: 25,
704
+ lengthMenu: [10, 25, 50, 100],
705
+ ordering: false,
706
+ language: {
707
+ search: "🔍 Tìm kiếm:",
708
+ lengthMenu: "Hiển thị _MENU_ dòng",
709
+ info: "Đang hiển thị _START_ đến _END_ của _TOTAL_ dòng",
710
+ paginate: {
711
+ previous: "←",
712
+ next: "→"
713
+ },
714
+ emptyTable: "Không có dữ liệu"
715
+ }
716
+ });
717
+
718
+ // ✅ Tô màu dòng nếu FULL được tick
719
+ function capNhatMauDong() {
720
+ $("#bangHSKH tbody tr").each(function () {
721
+ const checkbox = $(this).find(`td:eq(${fullIndex}) input[type="checkbox"]`);
722
+ if (checkbox.prop("checked")) {
723
+ $(this).css("background-color", "#d2f8d2"); // Xanh
724
+ } else {
725
+ $(this).css("background-color", "#fde2e2"); // Hồng
726
+ }
727
+ });
728
+ }
729
+
730
+ capNhatMauDong();
731
+
732
+ // ✅ Khi tích/bỏ tick vào cột FULL thì cập nhật màu dòng
733
+ $("#bangHSKH").off("change", "input.checkbox-full")
734
+ .on("change", "input.checkbox-full", function () {
735
+ capNhatMauDong();
736
+ });
737
+
738
+ // ✅ Hiện nút lưu
739
+ document.getElementById("nutLuuHSKH").style.display = "inline-block";
740
+
741
+ } catch (error) {
742
+ console.error("Lỗi khi tải dữ liệu HSKH:", error);
743
+ alert("Không thể tải dữ liệu hồ sơ lưu.");
744
+ }
745
+ }
746
+
747
+ async function luuHSKH() {
748
+ try {
749
+ const headers = [];
750
+ $("#bangHSKH thead tr:last-child th").each(function () {
751
+ headers.push($(this).text().trim());
752
+ });
753
+
754
+ const topHeaders = $("#bangHSKH thead tr:first-child th[rowspan='2']");
755
+ topHeaders.each(function (index) {
756
+ headers.splice(index, 0, $(this).text().trim());
757
+ });
758
+
759
+ const rows = [];
760
+ $("#bangHSKH tbody tr").each(function () {
761
+ const row = [];
762
+ $(this)
763
+ .find("td")
764
+ .each(function (i) {
765
+ const input = $(this).find("input");
766
+ if (input.length > 0) {
767
+ if (input.attr("type") === "checkbox") {
768
+ row.push(input.prop("checked") ? "x" : "");
769
+ } else {
770
+ row.push(input.val());
771
+ }
772
+ } else {
773
+ row.push($(this).text());
774
+ }
775
+ });
776
+ rows.push(row);
777
+ });
778
+
779
+ const response = await fetch(`${BACKEND_URL}/cap_nhat_hskh`, {
780
+ method: "POST",
781
+ headers: {
782
+ "Content-Type": "application/json"
783
+ },
784
+ body: JSON.stringify({
785
+ columns: headers,
786
+ rows: rows
787
+ })
788
+ });
789
+
790
+ if (response.ok) {
791
+ alert("✅ Dữ liệu đã được lưu thành công!");
792
+ } else {
793
+ const err = await response.json();
794
+ alert("❌ L���i khi lưu: " + err.detail);
795
+ }
796
+ } catch (error) {
797
+ console.error("Lỗi khi gửi dữ liệu:", error);
798
+ alert("❌ Không thể gửi dữ liệu để lưu.");
799
+ }
800
+ }
801
+
static/style.css CHANGED
@@ -364,106 +364,3 @@ table.dataTable td {
364
  white-space: normal !important;
365
  word-break: break-word;
366
  }
367
-
368
- #bangHSKH td {
369
- white-space: normal !important; /* ✅ Cho phép xuống dòng */
370
- word-wrap: break-word; /* ✅ Wrap nội dung dài */
371
- vertical-align: top; /* ✅ Căn top cho đẹp */
372
- }
373
-
374
- #bangHSKH input.input-cell {
375
- width: 100%;
376
- padding: 6px;
377
- font-size: 13px;
378
- box-sizing: border-box;
379
- background: #fff;
380
- }
381
-
382
- .checkbox-cell {
383
- width: 16px;
384
- height: 16px;
385
- accent-color: #2b6cb0;
386
- }
387
- #bangHSKH {
388
- width: 100%;
389
- border-collapse: collapse;
390
- font-size: 13px;
391
- background-color: #fff;
392
- font-family: 'Segoe UI', sans-serif;
393
- table-layout: auto !important; /* Sửa từ 'fixed' thành 'auto' */
394
- }
395
-
396
- #bangHSKH th,
397
- #bangHSKH td {
398
- border: 1px solid #ccc;
399
- padding: 6px 8px;
400
- text-align: center;
401
- vertical-align: middle;
402
- white-space: normal !important;
403
- word-break: break-word;
404
- }
405
- #bangHSKH td input.input-cell {
406
- width: 100%;
407
- box-sizing: border-box;
408
- border: none;
409
- background: transparent;
410
- text-align: center;
411
- }
412
- #bangHSKH {
413
- width: 100%;
414
- table-layout: auto !important;
415
- border-collapse: collapse;
416
- font-family: 'Segoe UI', sans-serif;
417
- font-size: 13px;
418
- }
419
-
420
- #bangHSKH th,
421
- #bangHSKH td {
422
- border: 1px solid #ccc;
423
- padding: 6px 8px;
424
- text-align: center;
425
- vertical-align: middle;
426
- white-space: normal;
427
- word-break: break-word;
428
- }
429
-
430
- /* Tạo chiều rộng hợp lý cho các cột */
431
- #bangHSKH th:nth-child(1), #bangHSKH td:nth-child(1) { width: 40px; }
432
- #bangHSKH th:nth-child(2), #bangHSKH td:nth-child(2) { width: 90px; }
433
- #bangHSKH th:nth-child(3), #bangHSKH td:nth-child(3) { width: 200px; }
434
- #bangHSKH th:nth-child(4), #bangHSKH td:nth-child(4) { width: 200px; }
435
- #bangHSKH th:nth-child(5), #bangHSKH td:nth-child(5) { width: 130px; }
436
- #bangHSKH th:nth-child(6), #bangHSKH td:nth-child(6) { width: 80px; }
437
- #bangHSKH th:nth-child(7), #bangHSKH td:nth-child(7) { width: 150px; }
438
- #bangHSKH th:nth-child(8), #bangHSKH td:nth-child(8) { width: 100px; }
439
- #bangHSKH th:nth-child(9), #bangHSKH td:nth-child(9) { width: 80px; }
440
-
441
- /* Cột checkbox */
442
- #bangHSKH th:nth-child(10),
443
- #bangHSKH th:nth-child(11),
444
- #bangHSKH th:nth-child(12),
445
- #bangHSKH th:nth-child(13),
446
- #bangHSKH td:nth-child(10),
447
- #bangHSKH td:nth-child(11),
448
- #bangHSKH td:nth-child(12),
449
- #bangHSKH td:nth-child(13) {
450
- width: 50px;
451
- }
452
-
453
- /* Cột ghi chú */
454
- #bangHSKH th:nth-child(14),
455
- #bangHSKH td:nth-child(14) {
456
- width: 200px;
457
- }
458
-
459
- #bangHSKH td input.input-cell {
460
- width: 100%;
461
- box-sizing: border-box;
462
- border: none;
463
- background: transparent;
464
- text-align: center;
465
- }
466
- /* Ngăn table layout bị vỡ khi dùng input và checkbox */
467
- #bangHSKH {
468
- table-layout: fixed !important;
469
- }
 
364
  white-space: normal !important;
365
  word-break: break-word;
366
  }