Spaces:
Sleeping

doluong6 commited on
Commit
6a18ed2
·
verified ·
1 Parent(s): 146b225

Upload script.js

Browse files
Files changed (1) hide show
  1. static/script.js +276 -0
static/script.js ADDED
@@ -0,0 +1,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
+ 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
+