Spaces:
Sleeping

doluong6 commited on
Commit
0cbad6e
·
verified ·
1 Parent(s): 2750b17

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +30 -28
static/script.js CHANGED
@@ -539,13 +539,14 @@ async function hienThiHoaChat() {
539
  const columns = data.columns;
540
  const rows = data.rows;
541
 
542
- const idxNgayHetHan = columns.findIndex(c => c.toUpperCase().includes("HẾT HẠN"));
 
543
  const idxTinhTrang = columns.findIndex(c => c.toUpperCase().includes("TÌNH TRẠNG"));
544
 
545
  const table = document.getElementById("bangHoaChat");
546
  table.innerHTML = "";
547
 
548
- // === Header ===
549
  const thead = table.createTHead();
550
  const headerRow = thead.insertRow();
551
  columns.forEach(col => {
@@ -557,7 +558,7 @@ async function hienThiHoaChat() {
557
  headerRow.appendChild(th);
558
  });
559
 
560
- // === Body ===
561
  const tbody = table.createTBody();
562
  rows.forEach((row, rowIndex) => {
563
  const tr = tbody.insertRow();
@@ -569,44 +570,45 @@ async function hienThiHoaChat() {
569
  input.classList.add("input-cell");
570
  input.style.width = "100%";
571
 
572
- // ==== Cập nhật TÌNH TRẠNG ====
573
  if (colIndex === idxTinhTrang) {
574
  input.readOnly = true;
575
  input.style.fontWeight = "bold";
576
 
577
- const ngayHetHanRaw = row[idxNgayHetHan];
578
- const today = new Date();
579
- const hsd = new Date(ngayHetHanRaw);
580
- const diffTime = hsd - today;
581
- const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
582
-
583
- let statusText = "Không xác định";
584
- let statusColor = "black";
585
-
586
- if (!isNaN(diffDays)) {
587
- if (diffDays <= 0) {
588
- statusText = "HẾT HẠN";
589
- statusColor = "red";
590
- input.style.backgroundColor = "#fdd";
591
- } else if (diffDays <= 60) {
592
- statusText = `CÒN: ${diffDays} NGÀY`;
593
- statusColor = "orange";
594
- input.style.backgroundColor = "#ffe5b4";
595
  } else {
596
- statusText = `CÒN: ${diffDays} NGÀY`;
597
- input.style.backgroundColor = "#eaffea";
598
  }
599
- }
600
 
601
- input.value = statusText;
602
- input.style.color = statusColor;
 
 
 
 
603
  }
604
 
605
  td.appendChild(input);
606
  });
607
  });
608
 
609
- // Hiện nút lưu
610
  const nutLuu = document.getElementById("nutLuuHoaChat");
611
  if (nutLuu) nutLuu.style.display = "block";
612
 
 
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 idxNgayHetHan = columns.findIndex(c => c.toUpperCase().includes("HẠN"));
544
  const idxTinhTrang = columns.findIndex(c => c.toUpperCase().includes("TÌNH TRẠNG"));
545
 
546
  const table = document.getElementById("bangHoaChat");
547
  table.innerHTML = "";
548
 
549
+ // Tạo header
550
  const thead = table.createTHead();
551
  const headerRow = thead.insertRow();
552
  columns.forEach(col => {
 
558
  headerRow.appendChild(th);
559
  });
560
 
561
+ // Tạo body
562
  const tbody = table.createTBody();
563
  rows.forEach((row, rowIndex) => {
564
  const tr = tbody.insertRow();
 
570
  input.classList.add("input-cell");
571
  input.style.width = "100%";
572
 
573
+ // Nếu là ô TÌNH TRẠNG
574
  if (colIndex === idxTinhTrang) {
575
  input.readOnly = true;
576
  input.style.fontWeight = "bold";
577
 
578
+ try {
579
+ const ngayNhapStr = row[idxNgayNhap]?.toString().slice(0, 10);
580
+ const hetHanStr = row[idxNgayHetHan]?.toString().slice(0, 10);
581
+ const ngayHet = new Date(hetHanStr);
582
+ const today = new Date();
583
+
584
+ const daysLeft = Math.ceil((ngayHet - today) / (1000 * 3600 * 24));
585
+ let text = "";
586
+ let color = "";
587
+
588
+ if (isNaN(daysLeft)) {
589
+ text = "Không xác định";
590
+ } else if (daysLeft <= 0) {
591
+ text = "HẾT HẠN";
592
+ color = "red";
593
+ } else if (daysLeft <= 30) {
594
+ text = `CÒN: ${daysLeft} NGÀY`;
595
+ color = "orange";
596
  } else {
597
+ text = `CÒN: ${daysLeft} NGÀY`;
 
598
  }
 
599
 
600
+ input.value = text;
601
+ input.style.color = color;
602
+
603
+ } catch (e) {
604
+ input.value = "Không xác định";
605
+ }
606
  }
607
 
608
  td.appendChild(input);
609
  });
610
  });
611
 
 
612
  const nutLuu = document.getElementById("nutLuuHoaChat");
613
  if (nutLuu) nutLuu.style.display = "block";
614