Update static/script.js
Browse files- static/script.js +52 -33
static/script.js
CHANGED
|
@@ -575,7 +575,7 @@ async function hienThiHoaChat() {
|
|
| 575 |
const tbody = table.createTBody();
|
| 576 |
rows.forEach(row => {
|
| 577 |
const tr = tbody.insertRow();
|
| 578 |
-
let backgroundColor = "";
|
| 579 |
|
| 580 |
row.forEach((cell, colIndex) => {
|
| 581 |
const td = tr.insertCell();
|
|
@@ -585,7 +585,6 @@ async function hienThiHoaChat() {
|
|
| 585 |
input.classList.add("input-cell");
|
| 586 |
input.style.width = "100%";
|
| 587 |
|
| 588 |
-
// Xử lý ô tình trạng
|
| 589 |
if (colIndex === idxTinhTrang) {
|
| 590 |
input.readOnly = true;
|
| 591 |
input.style.fontWeight = "bold";
|
|
@@ -594,39 +593,28 @@ async function hienThiHoaChat() {
|
|
| 594 |
let color = "black";
|
| 595 |
|
| 596 |
if (cell && cell.toString().startsWith("CÒN")) {
|
| 597 |
-
// Nếu đã có trạng thái, chỉ tô màu dòng
|
| 598 |
const match = cell.match(/(\d+)/);
|
| 599 |
if (match) {
|
| 600 |
const days = parseInt(match[1]);
|
| 601 |
if (days < 0) {
|
| 602 |
-
backgroundColor = "#f8d7da";
|
| 603 |
-
input.style.color = "red";
|
| 604 |
} else if (days <= 160) {
|
| 605 |
-
backgroundColor = "#fff3cd";
|
| 606 |
-
input.style.color = "orange";
|
| 607 |
} else {
|
| 608 |
-
backgroundColor = "#d4edda";
|
| 609 |
-
input.style.color = "green";
|
| 610 |
}
|
| 611 |
}
|
| 612 |
} else {
|
| 613 |
-
// Nếu chưa có trạng thái, tính và cập nhật
|
| 614 |
if (hanSD instanceof Date && !isNaN(hanSD)) {
|
| 615 |
const today = new Date();
|
| 616 |
const diffDays = Math.ceil((hanSD - today) / (1000 * 3600 * 24));
|
| 617 |
|
| 618 |
if (diffDays < 0) {
|
| 619 |
-
input.value = "HẾT HẠN";
|
| 620 |
-
input.style.color = "red";
|
| 621 |
-
backgroundColor = "#f8d7da";
|
| 622 |
} else if (diffDays <= 160) {
|
| 623 |
-
input.value = `CÒN: ${diffDays} ngày`;
|
| 624 |
-
input.style.color = "orange";
|
| 625 |
-
backgroundColor = "#fff3cd";
|
| 626 |
} else {
|
| 627 |
-
input.value = `CÒN: ${diffDays} ngày`;
|
| 628 |
-
input.style.color = "green";
|
| 629 |
-
backgroundColor = "#d4edda";
|
| 630 |
}
|
| 631 |
}
|
| 632 |
}
|
|
@@ -635,38 +623,68 @@ async function hienThiHoaChat() {
|
|
| 635 |
td.appendChild(input);
|
| 636 |
});
|
| 637 |
|
| 638 |
-
// ✅ Tô màu dòng nếu có màu xác định
|
| 639 |
if (backgroundColor) tr.style.backgroundColor = backgroundColor;
|
| 640 |
});
|
| 641 |
|
| 642 |
document.getElementById("nutLuuHoaChat").style.display = "block";
|
| 643 |
-
// 🔸 Hiển thị nút trích xuất
|
| 644 |
const btnExport = document.getElementById("nutTrichXuat160");
|
| 645 |
btnExport.style.display = "inline-block";
|
| 646 |
|
| 647 |
-
//
|
| 648 |
btnExport.onclick = function () {
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
rows.forEach(row => {
|
| 652 |
const hanSD = parseNgayVN(row[idxHanSD]);
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
const today = new Date();
|
| 655 |
const diffDays = Math.ceil((hanSD - today) / (1000 * 3600 * 24));
|
| 656 |
if (diffDays <= 160) {
|
| 657 |
-
const rowData = row.map(
|
| 658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
}
|
| 660 |
}
|
| 661 |
});
|
| 662 |
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
};
|
| 671 |
} catch (err) {
|
| 672 |
console.error("Lỗi khi tải danh mục hóa chất:", err);
|
|
@@ -675,6 +693,7 @@ async function hienThiHoaChat() {
|
|
| 675 |
}
|
| 676 |
|
| 677 |
|
|
|
|
| 678 |
// ==== Lưu dữ liệu đã chỉnh sửa ====
|
| 679 |
async function luuHoaChat() {
|
| 680 |
const table = document.getElementById("bangHoaChat");
|
|
|
|
| 575 |
const tbody = table.createTBody();
|
| 576 |
rows.forEach(row => {
|
| 577 |
const tr = tbody.insertRow();
|
| 578 |
+
let backgroundColor = "";
|
| 579 |
|
| 580 |
row.forEach((cell, colIndex) => {
|
| 581 |
const td = tr.insertCell();
|
|
|
|
| 585 |
input.classList.add("input-cell");
|
| 586 |
input.style.width = "100%";
|
| 587 |
|
|
|
|
| 588 |
if (colIndex === idxTinhTrang) {
|
| 589 |
input.readOnly = true;
|
| 590 |
input.style.fontWeight = "bold";
|
|
|
|
| 593 |
let color = "black";
|
| 594 |
|
| 595 |
if (cell && cell.toString().startsWith("CÒN")) {
|
|
|
|
| 596 |
const match = cell.match(/(\d+)/);
|
| 597 |
if (match) {
|
| 598 |
const days = parseInt(match[1]);
|
| 599 |
if (days < 0) {
|
| 600 |
+
backgroundColor = "#f8d7da"; input.style.color = "red";
|
|
|
|
| 601 |
} else if (days <= 160) {
|
| 602 |
+
backgroundColor = "#fff3cd"; input.style.color = "orange";
|
|
|
|
| 603 |
} else {
|
| 604 |
+
backgroundColor = "#d4edda"; input.style.color = "green";
|
|
|
|
| 605 |
}
|
| 606 |
}
|
| 607 |
} else {
|
|
|
|
| 608 |
if (hanSD instanceof Date && !isNaN(hanSD)) {
|
| 609 |
const today = new Date();
|
| 610 |
const diffDays = Math.ceil((hanSD - today) / (1000 * 3600 * 24));
|
| 611 |
|
| 612 |
if (diffDays < 0) {
|
| 613 |
+
input.value = "HẾT HẠN"; input.style.color = "red"; backgroundColor = "#f8d7da";
|
|
|
|
|
|
|
| 614 |
} else if (diffDays <= 160) {
|
| 615 |
+
input.value = `CÒN: ${diffDays} ngày`; input.style.color = "orange"; backgroundColor = "#fff3cd";
|
|
|
|
|
|
|
| 616 |
} else {
|
| 617 |
+
input.value = `CÒN: ${diffDays} ngày`; input.style.color = "green"; backgroundColor = "#d4edda";
|
|
|
|
|
|
|
| 618 |
}
|
| 619 |
}
|
| 620 |
}
|
|
|
|
| 623 |
td.appendChild(input);
|
| 624 |
});
|
| 625 |
|
|
|
|
| 626 |
if (backgroundColor) tr.style.backgroundColor = backgroundColor;
|
| 627 |
});
|
| 628 |
|
| 629 |
document.getElementById("nutLuuHoaChat").style.display = "block";
|
|
|
|
| 630 |
const btnExport = document.getElementById("nutTrichXuat160");
|
| 631 |
btnExport.style.display = "inline-block";
|
| 632 |
|
| 633 |
+
// === ✅ Trích xuất XLSX có màu
|
| 634 |
btnExport.onclick = function () {
|
| 635 |
+
const wb = XLSX.utils.book_new();
|
| 636 |
+
const wsData = [columns];
|
| 637 |
+
const wsStyles = [];
|
| 638 |
+
|
| 639 |
+
let soDongCanhBao = 0;
|
| 640 |
|
| 641 |
rows.forEach(row => {
|
| 642 |
const hanSD = parseNgayVN(row[idxHanSD]);
|
| 643 |
+
const tinhTrang = row[idxTinhTrang] || "";
|
| 644 |
+
|
| 645 |
+
if (
|
| 646 |
+
hanSD instanceof Date &&
|
| 647 |
+
!isNaN(hanSD) &&
|
| 648 |
+
!tinhTrang.toString().startsWith("CÒN")
|
| 649 |
+
) {
|
| 650 |
const today = new Date();
|
| 651 |
const diffDays = Math.ceil((hanSD - today) / (1000 * 3600 * 24));
|
| 652 |
if (diffDays <= 160) {
|
| 653 |
+
const rowData = row.map(cell => (cell || "").toString().replace(/[\n\r]+/g, " ").trim());
|
| 654 |
+
wsData.push(rowData);
|
| 655 |
+
soDongCanhBao++;
|
| 656 |
+
|
| 657 |
+
let fillColor = diffDays < 0 ? 'FFFFCCCC' : 'FFFFFF99';
|
| 658 |
+
wsStyles.push({ fillColor });
|
| 659 |
}
|
| 660 |
}
|
| 661 |
});
|
| 662 |
|
| 663 |
+
if (soDongCanhBao === 0) {
|
| 664 |
+
alert("✅ Không có hóa chất nào cần cảnh báo mới (≤160 ngày và chưa cảnh báo).");
|
| 665 |
+
return;
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
const ws = XLSX.utils.aoa_to_sheet(wsData);
|
| 669 |
+
|
| 670 |
+
wsStyles.forEach((style, i) => {
|
| 671 |
+
const rowIndex = i + 1;
|
| 672 |
+
columns.forEach((_, colIndex) => {
|
| 673 |
+
const addr = XLSX.utils.encode_cell({ r: rowIndex, c: colIndex });
|
| 674 |
+
if (!ws[addr]) return;
|
| 675 |
+
ws[addr].s = {
|
| 676 |
+
fill: {
|
| 677 |
+
patternType: "solid",
|
| 678 |
+
fgColor: { rgb: style.fillColor }
|
| 679 |
+
}
|
| 680 |
+
};
|
| 681 |
+
});
|
| 682 |
+
});
|
| 683 |
+
|
| 684 |
+
XLSX.utils.book_append_sheet(wb, ws, "Cảnh báo hóa chất");
|
| 685 |
+
const now = new Date();
|
| 686 |
+
const fileName = `hoa_chat_canh_bao_${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.xlsx`;
|
| 687 |
+
XLSX.writeFile(wb, fileName);
|
| 688 |
};
|
| 689 |
} catch (err) {
|
| 690 |
console.error("Lỗi khi tải danh mục hóa chất:", err);
|
|
|
|
| 693 |
}
|
| 694 |
|
| 695 |
|
| 696 |
+
|
| 697 |
// ==== Lưu dữ liệu đã chỉnh sửa ====
|
| 698 |
async function luuHoaChat() {
|
| 699 |
const table = document.getElementById("bangHoaChat");
|