Spaces:
Sleeping

doluong6 commited on
Commit
cfdf00e
·
verified ·
1 Parent(s): fc95f92

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +24 -19
static/script.js CHANGED
@@ -539,11 +539,27 @@ async function hienThiHoaChat() {
539
  const columns = data.columns;
540
  const rows = data.rows;
541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  const idxNgayNhap = columns.findIndex(c => c.toUpperCase().includes("NGÀY NHẬP"));
543
  const idxHetHan = columns.findIndex(c => c.toUpperCase().includes("HẾT HẠN"));
544
  const idxTinhTrang = columns.findIndex(c => c.toUpperCase().includes("TÌNH TRẠNG"));
545
-
546
- rows.forEach((row, rowIndex) => {
547
  const tr = tbody.insertRow();
548
  row.forEach((cell, colIndex) => {
549
  const td = tr.insertCell();
@@ -552,18 +568,18 @@ async function hienThiHoaChat() {
552
  input.value = cell;
553
  input.classList.add("input-cell");
554
  input.style.width = "100%";
555
-
556
  if (colIndex === idxTinhTrang) {
557
  input.readOnly = true;
558
  input.style.fontWeight = "bold";
559
-
560
  const ngayNhap = new Date(normalizeDate(row[idxNgayNhap]));
561
  const ngayHetHan = new Date(normalizeDate(row[idxHetHan]));
562
  const today = new Date();
563
-
564
  let text = "Không xác định";
565
  let color = "black";
566
-
567
  if (!isNaN(ngayNhap) && !isNaN(ngayHetHan)) {
568
  const diffDays = Math.floor((ngayHetHan - today) / (1000 * 60 * 60 * 24));
569
  if (diffDays <= 0) {
@@ -576,16 +592,15 @@ async function hienThiHoaChat() {
576
  text = `CÒN: ${diffDays} ngày`;
577
  }
578
  }
579
-
580
  input.value = text;
581
  input.style.color = color;
582
  }
583
-
584
  td.appendChild(input);
585
  });
586
  });
587
 
588
-
589
  const nutLuu = document.getElementById("nutLuuHoaChat");
590
  if (nutLuu) nutLuu.style.display = "block";
591
 
@@ -594,16 +609,6 @@ async function hienThiHoaChat() {
594
  alert("❌ Không thể tải dữ liệu hóa chất.");
595
  }
596
  }
597
- function normalizeDate(dateStr) {
598
- if (!dateStr || typeof dateStr !== "string") return NaN;
599
- const parts = dateStr.split(/\/|-/);
600
- if (parts.length === 3) {
601
- let [d, m, y] = parts.map(p => parseInt(p));
602
- if (y < 1000) y += 2000;
603
- return `${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
604
- }
605
- return dateStr;
606
- }
607
 
608
 
609
 
 
539
  const columns = data.columns;
540
  const rows = data.rows;
541
 
542
+ const table = document.getElementById("bangHoaChat");
543
+ table.innerHTML = ""; // Xóa bảng cũ
544
+
545
+ const thead = document.createElement("thead");
546
+ const trHead = document.createElement("tr");
547
+ columns.forEach(col => {
548
+ const th = document.createElement("th");
549
+ th.textContent = col;
550
+ trHead.appendChild(th);
551
+ });
552
+ thead.appendChild(trHead);
553
+ table.appendChild(thead);
554
+
555
+ const tbody = document.createElement("tbody");
556
+ table.appendChild(tbody);
557
+
558
  const idxNgayNhap = columns.findIndex(c => c.toUpperCase().includes("NGÀY NHẬP"));
559
  const idxHetHan = columns.findIndex(c => c.toUpperCase().includes("HẾT HẠN"));
560
  const idxTinhTrang = columns.findIndex(c => c.toUpperCase().includes("TÌNH TRẠNG"));
561
+
562
+ rows.forEach(row => {
563
  const tr = tbody.insertRow();
564
  row.forEach((cell, colIndex) => {
565
  const td = tr.insertCell();
 
568
  input.value = cell;
569
  input.classList.add("input-cell");
570
  input.style.width = "100%";
571
+
572
  if (colIndex === idxTinhTrang) {
573
  input.readOnly = true;
574
  input.style.fontWeight = "bold";
575
+
576
  const ngayNhap = new Date(normalizeDate(row[idxNgayNhap]));
577
  const ngayHetHan = new Date(normalizeDate(row[idxHetHan]));
578
  const today = new Date();
579
+
580
  let text = "Không xác định";
581
  let color = "black";
582
+
583
  if (!isNaN(ngayNhap) && !isNaN(ngayHetHan)) {
584
  const diffDays = Math.floor((ngayHetHan - today) / (1000 * 60 * 60 * 24));
585
  if (diffDays <= 0) {
 
592
  text = `CÒN: ${diffDays} ngày`;
593
  }
594
  }
595
+
596
  input.value = text;
597
  input.style.color = color;
598
  }
599
+
600
  td.appendChild(input);
601
  });
602
  });
603
 
 
604
  const nutLuu = document.getElementById("nutLuuHoaChat");
605
  if (nutLuu) nutLuu.style.display = "block";
606
 
 
609
  alert("❌ Không thể tải dữ liệu hóa chất.");
610
  }
611
  }
 
 
 
 
 
 
 
 
 
 
612
 
613
 
614