Spaces:
Sleeping

doluong6 commited on
Commit
e90dc4f
·
verified ·
1 Parent(s): 9048636

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +25 -30
static/script.js CHANGED
@@ -539,10 +539,9 @@ async function hienThiHoaChat() {
539
  const columns = data.columns;
540
  const rows = data.rows;
541
 
542
- // ✅ Tìm đúng index theo tên cột thực tế (có \n)
543
- const idxNgayNhap = columns.findIndex(c => c.includes("NHẬP KHO"));
544
- const idxNgayHetHan = columns.findIndex(c => c.includes("HẾT HẠN"));
545
- const idxTinhTrang = columns.findIndex(c => c.includes("TÌNH TRẠNG"));
546
 
547
  const table = document.getElementById("bangHoaChat");
548
  table.innerHTML = "";
@@ -551,7 +550,7 @@ async function hienThiHoaChat() {
551
  const headerRow = thead.insertRow();
552
  columns.forEach(col => {
553
  const th = document.createElement("th");
554
- th.textContent = col.replace(/\n/g, " ");
555
  th.style.background = "#f2f2f2";
556
  th.style.padding = "8px";
557
  th.style.border = "1px solid #ddd";
@@ -569,37 +568,32 @@ async function hienThiHoaChat() {
569
  input.classList.add("input-cell");
570
  input.style.width = "100%";
571
 
572
- // ✅ Nếu là cột TÌNH TRẠNG, tự động cập nhật
573
  if (colIndex === idxTinhTrang) {
574
  input.readOnly = true;
575
  input.style.fontWeight = "bold";
576
 
577
- try {
578
- const ngayHetStr = row[idxNgayHetHan]?.toString().slice(0, 10);
579
- const ngayHet = new Date(ngayHetStr);
580
- const today = new Date();
581
- const diffTime = ngayHet.getTime() - today.getTime();
582
- const daysLeft = Math.floor(diffTime / (1000 * 3600 * 24));
583
-
584
- let statusText = "Không xác định";
585
- let color = "black";
586
-
587
- if (!isNaN(daysLeft)) {
588
- if (daysLeft <= 0) {
589
- statusText = "HẾT HẠN";
590
- color = "red";
591
- } else if (daysLeft <= 30) {
592
- statusText = `CÒN: ${daysLeft} NGÀY`;
593
- color = "orange";
594
- } else {
595
- statusText = `CÒN: ${daysLeft} NGÀY`;
596
- }
597
  }
598
-
599
- input.value = statusText;
600
- input.style.color = color;
601
- } catch (e) {
602
  input.value = "Không xác định";
 
603
  }
604
  }
605
 
@@ -617,6 +611,7 @@ async function hienThiHoaChat() {
617
  }
618
 
619
 
 
620
  // ==== Lưu dữ liệu đã chỉnh sửa ====
621
  async function luuHoaChat() {
622
  const table = document.getElementById("bangHoaChat");
 
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 idxHanSD = 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
  const table = document.getElementById("bangHoaChat");
547
  table.innerHTML = "";
 
550
  const headerRow = thead.insertRow();
551
  columns.forEach(col => {
552
  const th = document.createElement("th");
553
+ th.textContent = col;
554
  th.style.background = "#f2f2f2";
555
  th.style.padding = "8px";
556
  th.style.border = "1px solid #ddd";
 
568
  input.classList.add("input-cell");
569
  input.style.width = "100%";
570
 
571
+ // Tính lại tình trạng tự động
572
  if (colIndex === idxTinhTrang) {
573
  input.readOnly = true;
574
  input.style.fontWeight = "bold";
575
 
576
+ const ngayNhap = new Date(row[idxNgayNhap]);
577
+ const ngayHetHan = new Date(row[idxHanSD]);
578
+ const today = new Date();
579
+
580
+ if (ngayNhap instanceof Date && !isNaN(ngayNhap) && ngayHetHan instanceof Date && !isNaN(ngayHetHan)) {
581
+ const timeDiff = (ngayHetHan - today) / (1000 * 3600 * 24); // ngày còn lại
582
+ const daysLeft = Math.floor(timeDiff);
583
+
584
+ if (daysLeft < 0) {
585
+ input.value = "HẾT HẠN";
586
+ input.style.color = "red";
587
+ } else if (daysLeft <= 30) {
588
+ input.value = `CÒN: ${daysLeft} NGÀY`;
589
+ input.style.color = "orange";
590
+ } else {
591
+ input.value = `CÒN: ${daysLeft} NGÀY`;
592
+ input.style.color = "black";
 
 
 
593
  }
594
+ } else {
 
 
 
595
  input.value = "Không xác định";
596
+ input.style.color = "gray";
597
  }
598
  }
599
 
 
611
  }
612
 
613
 
614
+
615
  // ==== Lưu dữ liệu đã chỉnh sửa ====
616
  async function luuHoaChat() {
617
  const table = document.getElementById("bangHoaChat");