Spaces:
Sleeping

doluong6 commited on
Commit
58b6b71
·
verified ·
1 Parent(s): 22d3ef3

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +121 -245
static/script.js CHANGED
@@ -1,276 +1,152 @@
1
- // Đăng nhập đơn giản
2
  function login() {
3
  const u = document.getElementById("username").value;
4
  const p = document.getElementById("password").value;
5
  if (u === "admin" && p === "1234") {
6
  document.getElementById("login").style.display = "none";
7
  document.getElementById("dashboard").style.display = "block";
8
- hienThiTongQuan();
9
  } else {
10
  alert("Sai tên đăng nhập hoặc mật khẩu!");
11
  }
12
  }
13
 
14
- // Gọi API tổng quan khi đăng nhập thành công
15
- function hienThiTongQuan() {
16
- hienThiDanhSachPYC();
17
- hienThiSoPYCHoanThanh();
18
- hienThiDoanhThuPYC();
19
- }
20
-
21
- async function hienThiDanhSachPYC() {
22
- try {
23
- const res = await fetch("/dem_pyc_tat_ca");
24
- const data = await res.json();
25
-
26
- // Nếu API trả về số đang xử lý trong key 'tong_dang_xu_ly'
27
- if (data.tong_dang_xu_ly !== undefined) {
28
- document.getElementById("pycCount").textContent = data.tong_dang_xu_ly;
29
- }
30
- // Nếu bạn đã gán luôn vào 'tong_so_pyc' thì dùng dòng dưới:
31
- else if (data.tong_so_pyc !== undefined) {
32
- document.getElementById("pycCount").textContent = data.tong_so_pyc;
33
- } else {
34
- document.getElementById("pycCount").textContent = "Không tải được";
35
- }
36
- } catch (err) {
37
- console.error("Lỗi khi gọi API PYC:", err);
38
- document.getElementById("pycCount").textContent = "Lỗi";
39
- }
40
- }
41
-
42
-
43
- async function hienThiSoPYCHoanThanh() {
44
- try {
45
- const res = await fetch("/dem_pyc_hoan_thanh");
46
- const data = await res.json();
47
-
48
- if (res.ok && data.tong_so_hoan_thanh !== undefined) {
49
- document.getElementById("completedRequests").textContent = data.tong_so_hoan_thanh;
50
- } else {
51
- document.getElementById("completedRequests").textContent = "Không tải được";
52
- }
53
- } catch (err) {
54
- console.error("Lỗi khi gọi API hoàn thành:", err);
55
- document.getElementById("completedRequests").textContent = "Lỗi";
56
- }
57
- }
58
-
59
-
60
- // Gọi API /tong_doanh_thu_pyc
61
- async function hienThiDoanhThuPYC() {
62
- try {
63
- const res = await fetch("/tong_doanh_thu_pyc");
64
- const data = await res.json();
65
- if (res.ok && data.tong_doanh_thu !== undefined) {
66
- const formatted = Number(data.tong_doanh_thu).toLocaleString("vi-VN") + " VND";
67
- document.getElementById("requestRevenue").textContent = formatted;
68
- } else {
69
- document.getElementById("requestRevenue").textContent = "Không tải được";
70
- }
71
- } catch (err) {
72
- console.error("Lỗi khi gọi API doanh thu:", err);
73
- document.getElementById("requestRevenue").textContent = "Lỗi";
74
  }
75
  }
76
 
 
77
  function chonMuc(muc) {
78
- const bang = document.getElementById("bangChiTiet");
79
- const tieuDe = document.getElementById("tieuDeBang");
80
- const noiDung = document.getElementById("noiDungBang");
81
- const tongQuan = document.getElementById("tongQuan");
82
-
83
- tongQuan.style.display = "none";
84
- bang.style.display = "none";
85
- document.getElementById("doanhThuTheoDichVu").style.display = "none";
86
-
87
- if (muc === "tongquan") {
88
- tongQuan.style.display = "flex";
89
- hienThiTongQuan();
90
-
91
- } else if (muc === "pyc") {
92
- bang.style.display = "block";
93
- tieuDe.textContent = "Phiếu yêu cầu chưa hoàn thành";
94
- hienThiBangTuAPI("/pyc_chua_hoan_thanh", noiDung);
95
 
96
- } else if (muc === "dt") {
97
- bang.style.display = "block";
98
- tieuDe.textContent = "Doanh thu theo dịch vụ";
99
- noiDung.innerHTML = "";
100
- document.getElementById("doanhThuTheoDichVu").style.display = "block";
101
-
102
- setTimeout(() => {
103
- veBieuDoDichVu();
104
- hienThiBangTuAPI("/so_luong_dich_vu", document.getElementById("bangSoLuongDichVu"));
105
- hienThiBangTuAPI("/khach_hang_theo_dich_vu", document.getElementById("bangKhachHangTheoDichVu"));
106
- }, 100);
107
-
108
- } else if (muc === "dt_thang") {
109
- bang.style.display = "block";
110
- tieuDe.textContent = "Doanh thu theo tháng";
111
- noiDung.innerHTML = `<canvas id="doanhThuThangChart" height="100"></canvas>`;
112
- setTimeout(() => veBieuDoDoanhThuThang(), 0);
113
-
114
- } else if (muc === "nb") {
115
- bang.style.display = "block";
116
- tieuDe.textContent = "Doanh thu nội bộ";
117
- noiDung.innerHTML = `<canvas id="chartTongHopDoanhThu" height="100"></canvas>`;
118
- setTimeout(() => veBieuDoTongHopDoanhThu(), 0);
119
-
120
- } else {
121
- bang.style.display = "block";
122
- tieuDe.textContent = "🚧 Tính năng đang phát triển";
123
- noiDung.innerHTML = "Vui lòng quay lại sau.";
124
  }
125
- }
126
 
127
-
128
- // Hàm render bảng động
129
- async function hienThiBangTuAPI(api, noiDung) {
130
- noiDung.innerHTML = "Đang tải dữ liệu...";
131
- try {
132
- const res = await fetch(api);
133
- const data = await res.json();
134
-
135
- if (!Array.isArray(data) || data.length === 0) {
136
- noiDung.innerHTML = "Không có dữ liệu.";
137
- return;
138
- }
139
-
140
- let html = "<table class='display' id='tableAuto'><thead><tr>";
141
- for (let key in data[0]) {
142
- html += `<th>${key}</th>`;
143
- }
144
- html += "</tr></thead><tbody>";
145
-
146
- for (let row of data) {
147
- html += "<tr>";
148
- for (let key in row) {
149
- html += `<td>${row[key]}</td>`;
150
  }
151
- html += "</tr>";
152
- }
153
-
154
- html += "</tbody></table>";
155
- noiDung.innerHTML = html;
156
-
157
- $(document).ready(function () {
158
- $('#tableAuto').DataTable();
159
- });
160
- } catch (err) {
161
- console.error("Lỗi khi tải bảng:", err);
162
- noiDung.innerHTML = "Lỗi khi tải dữ liệu.";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  }
164
  }
165
- async function veBieuDoDoanhThuThang() {
166
- try {
167
- const response = await fetch("https://doluong6-qlht.hf.space/doanh_thu_theo_thang"); // Đổi URL nếu backend khác
168
- const data = await response.json();
169
-
170
- if (data.error) {
171
- console.error("Lỗi từ API:", data.error);
172
- return;
173
- }
174
-
175
- const ctx = document.getElementById("doanhThuThangChart").getContext("2d");
176
 
177
- new Chart(ctx, {
178
- type: "bar",
179
- data: {
180
- labels: data.labels,
181
- datasets: [{
182
- label: "Doanh thu (VNĐ)",
183
- data: data.values,
184
- backgroundColor: "rgba(54, 162, 235, 0.7)",
185
- borderColor: "rgba(54, 162, 235, 1)",
186
- borderWidth: 1
187
- }]
188
- },
189
- options: {
190
- responsive: true,
191
- scales: {
192
- y: {
193
- beginAtZero: true,
194
- ticks: {
195
- callback: function(value) {
196
- return value.toLocaleString("vi-VN") + " VNĐ";
197
- }
198
- }
199
- }
200
- },
201
- plugins: {
202
- legend: {
203
- display: false
204
- },
205
- tooltip: {
206
- callbacks: {
207
- label: function(context) {
208
- return context.raw.toLocaleString("vi-VN") + " VNĐ";
209
- }
210
- }
211
- }
212
- }
213
- }
214
- });
215
 
216
- } catch (error) {
217
- console.error("Lỗi khi gọi API hoặc vẽ biểu đồ:", error);
218
- }
219
  }
220
 
 
 
 
 
 
 
 
 
221
 
222
- async function veBieuDoDichVu() {
223
- const canvas = document.getElementById("chartDichVu");
224
- if (!canvas) {
225
- console.error("Không tìm thấy canvas chartDichVu");
226
- return;
227
- }
228
-
229
- const ctx = canvas.getContext("2d");
230
 
231
- try {
232
- const res = await fetch("/ty_le_dich_vu");
233
- const data = await res.json();
234
 
235
- const chartData = {
236
- labels: ["Kiểm định", "Hiệu chuẩn", "Đo thử nghiệm"],
237
- datasets: [{
238
- label: "Tỷ lệ dịch vụ",
239
- data: [
240
- data.Kiem_dinh || 0,
241
- data.Hieu_chuan || 0,
242
- data.Do_thu_nghiem || 0
243
- ],
244
- backgroundColor: [
245
- "#4CAF50", "#2196F3", "#FFC107"
246
- ],
247
- hoverOffset: 10
248
- }]
249
- };
250
 
251
- new Chart(ctx, {
252
- type: 'doughnut',
253
- data: chartData,
254
- options: {
255
- responsive: true,
256
- plugins: {
257
- legend: { position: 'bottom' },
258
- tooltip: {
259
- callbacks: {
260
- label: (context) => `${context.label}: ${context.raw.toFixed(2)}%`
261
- }
262
- }
263
- }
 
 
 
 
 
264
  }
265
- });
266
-
267
- } catch (error) {
268
- console.error("Lỗi API /ty_le_dich_vu:", error);
269
- }
270
- }
271
- function toggleSubmenu(id) {
272
- const submenu = document.getElementById(id);
273
- const parent = submenu.parentElement;
274
- parent.classList.toggle("open");
275
  }
276
-
 
1
+ // ==== Đăng nhập đơn giản ====
2
  function login() {
3
  const u = document.getElementById("username").value;
4
  const p = document.getElementById("password").value;
5
  if (u === "admin" && p === "1234") {
6
  document.getElementById("login").style.display = "none";
7
  document.getElementById("dashboard").style.display = "block";
8
+ // Có thể gọi tổng quan hoặc để trống
9
  } else {
10
  alert("Sai tên đăng nhập hoặc mật khẩu!");
11
  }
12
  }
13
 
14
+ // ==== Mở/đóng submenu ====
15
+ function toggleSubmenu(menuId) {
16
+ const submenu = document.getElementById(menuId);
17
+ const arrow = document.getElementById(`arrow-${menuId}`);
18
+ if (submenu.style.display === "block") {
19
+ submenu.style.display = "none";
20
+ arrow.innerHTML = "►";
21
+ } else {
22
+ submenu.style.display = "block";
23
+ arrow.innerHTML = "▼";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
  }
26
 
27
+ // ==== Hiển thị nội dung theo mục đã chọn ====
28
  function chonMuc(muc) {
29
+ // Ẩn tất cả các phần .fade-in
30
+ document.querySelectorAll(".fade-in").forEach(el => el.style.display = "none");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ // Reset bảng chi tiết
33
+ if (document.getElementById("bangChiTiet")) {
34
+ document.getElementById("bangChiTiet").style.display = "none";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  }
 
36
 
37
+ switch (muc) {
38
+ // === Phiếu yêu cầu ===
39
+ case "pycload":
40
+ document.getElementById("pycload").style.display = "block";
41
+ document.getElementById("bangPYCChoXuLy").style.display = "block";
42
+ hienThiDanhSachPYC();
43
+ hienThiBangPYCChoXuLy();
44
+ break;
45
+
46
+ case "pycend":
47
+ document.getElementById("pycend").style.display = "block";
48
+ hienThiSoPYCHoanThanh();
49
+ break;
50
+
51
+ case "pycdt":
52
+ document.getElementById("pycdt").style.display = "block";
53
+ hienThiDoanhThuPYC();
54
+ break;
55
+
56
+ case "pyc":
57
+ if (document.getElementById("pyc")) {
58
+ document.getElementById("pyc").style.display = "block";
59
+ hienThiDanhSachPYCKhongHoanThanh();
60
  }
61
+ break;
62
+
63
+ // === Doanh thu ===
64
+ case "dttong":
65
+ case "dtthang":
66
+ case "dtdichvu":
67
+ case "dthoadon":
68
+ case "dtnoibo":
69
+ document.getElementById("bangChiTiet").style.display = "block";
70
+ document.getElementById("tieuDeBang").innerText = "Chi tiết " + muc;
71
+ document.getElementById("noiDungBang").innerText = "Đang tải dữ liệu...";
72
+
73
+ // Gọi hàm tương ứng (cần tự định nghĩa)
74
+ if (muc === "dtdichvu") {
75
+ document.getElementById("doanhThuTheoDichVu").style.display = "block";
76
+ veBieuDoDichVu();
77
+ }
78
+ break;
79
+
80
+ // === Nhân sự ===
81
+ case "thao":
82
+ case "huy":
83
+ case "duong":
84
+ case "cuong":
85
+ case "vinh":
86
+ document.getElementById("bangChiTiet").style.display = "block";
87
+ document.getElementById("tieuDeBang").innerText = `Nhân sự: ${muc}`;
88
+ document.getElementById("noiDungBang").innerHTML = `<p>Đang tải dữ liệu của nhân sự ${muc}...</p>`;
89
+ break;
90
+
91
+ // === Tài liệu ===
92
+ case "Nl":
93
+ document.getElementById("bangChiTiet").style.display = "block";
94
+ document.getElementById("tieuDeBang").innerText = "Hồ sơ năng lực";
95
+ document.getElementById("noiDungBang").innerHTML = "<p>Hiển thị tài liệu hồ sơ năng lực...</p>";
96
+ break;
97
+
98
+ default:
99
+ alert("Mục không xác định: " + muc);
100
  }
101
  }
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ // ==== Ví dụ các hàm gọi API giả (bạn thay bằng API thật) ====
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
+ function hienThiDanhSachPYC() {
106
+ document.getElementById("pycCount").innerText = "5 phiếu";
 
107
  }
108
 
109
+ function hienThiBangPYCChoXuLy() {
110
+ const tbody = document.querySelector("#tablePYCChoXuLy tbody");
111
+ tbody.innerHTML = `
112
+ <tr><td>Khách hàng A</td><td>PYC-001.xlsx</td></tr>
113
+ <tr><td>Khách hàng B</td><td>PYC-002.xlsx</td></tr>
114
+ `;
115
+ $('#tablePYCChoXuLy').DataTable(); // DataTables
116
+ }
117
 
118
+ function hienThiSoPYCHoanThanh() {
119
+ document.getElementById("completedRequests").innerText = "8 phiếu";
120
+ }
 
 
 
 
 
121
 
122
+ function hienThiDoanhThuPYC() {
123
+ document.getElementById("requestRevenue").innerText = "150 triệu VND";
124
+ }
125
 
126
+ function hienThiDanhSachPYCKhongHoanThanh() {
127
+ document.getElementById("pyc").innerHTML = "<p>Danh sách phiếu chưa hoàn thành đang được cập nhật...</p>";
128
+ }
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ function veBieuDoDichVu() {
131
+ const ctx = document.getElementById("chartDichVu").getContext("2d");
132
+ const data = {
133
+ labels: ["Hiệu chuẩn", "Kiểm định", "Khác"],
134
+ datasets: [{
135
+ label: "Tỷ lệ dịch vụ",
136
+ data: [50, 30, 20],
137
+ backgroundColor: ["#4CAF50", "#2196F3", "#FFC107"],
138
+ hoverOffset: 10
139
+ }]
140
+ };
141
+ const config = {
142
+ type: 'doughnut',
143
+ data: data,
144
+ options: {
145
+ responsive: true,
146
+ plugins: {
147
+ legend: { position: 'bottom' }
148
  }
149
+ }
150
+ };
151
+ new Chart(ctx, config);
 
 
 
 
 
 
 
152
  }