Spaces:
Runtime error

File size: 8,017 Bytes
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d520560
3ef6b8b
d520560
 
 
 
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bd43e
3ef6b8b
 
 
 
 
 
 
 
d520560
3ef6b8b
d520560
 
3ef6b8b
 
d520560
 
 
3ef6b8b
 
56bd43e
 
 
 
 
 
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bd43e
3ef6b8b
56bd43e
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bd43e
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bd43e
 
 
 
3ef6b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bd43e
3ef6b8b
 
 
 
 
 
 
 
 
56bd43e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Đăng nhập đơn giản
function login() {
  const u = document.getElementById("username").value;
  const p = document.getElementById("password").value;
  if (u === "admin" && p === "1234") {
    document.getElementById("login").style.display = "none";
    document.getElementById("dashboard").style.display = "block";
    hienThiTongQuan();
  } else {
    alert("Sai tên đăng nhập hoặc mật khẩu!");
  }
}

// Gọi API tổng quan khi đăng nhập thành công
function hienThiTongQuan() {
  hienThiDanhSachPYC();
  hienThiSoPYCHoanThanh();
  hienThiDoanhThuPYC();
}

async function hienThiDanhSachPYC() {
  try {
    const res = await fetch("/dem_pyc_tat_ca");
    const data = await res.json();

    // Nếu API trả về số đang xử lý trong key 'tong_dang_xu_ly'
    if (data.tong_dang_xu_ly !== undefined) {
      document.getElementById("pycCount").textContent = data.tong_dang_xu_ly;
    }
    // Nếu bạn đã gán luôn vào 'tong_so_pyc' thì dùng dòng dưới:
    else if (data.tong_so_pyc !== undefined) {
      document.getElementById("pycCount").textContent = data.tong_so_pyc;
    } else {
      document.getElementById("pycCount").textContent = "Không tải được";
    }
  } catch (err) {
    console.error("Lỗi khi gọi API PYC:", err);
    document.getElementById("pycCount").textContent = "Lỗi";
  }
}


async function hienThiSoPYCHoanThanh() {
  try {
    const res = await fetch("/dem_pyc_hoan_thanh");
    const data = await res.json();

    if (res.ok && data.tong_so_hoan_thanh !== undefined) {
      document.getElementById("completedRequests").textContent = data.tong_so_hoan_thanh;
    } else {
      document.getElementById("completedRequests").textContent = "Không tải được";
    }
  } catch (err) {
    console.error("Lỗi khi gọi API hoàn thành:", err);
    document.getElementById("completedRequests").textContent = "Lỗi";
  }
}


// Gọi API /tong_doanh_thu_pyc
async function hienThiDoanhThuPYC() {
  try {
    const res = await fetch("/tong_doanh_thu_pyc");
    const data = await res.json();
    if (res.ok && data.tong_doanh_thu !== undefined) {
      const formatted = Number(data.tong_doanh_thu).toLocaleString("vi-VN") + " VND";
      document.getElementById("requestRevenue").textContent = formatted;
    } else {
      document.getElementById("requestRevenue").textContent = "Không tải được";
    }
  } catch (err) {
    console.error("Lỗi khi gọi API doanh thu:", err);
    document.getElementById("requestRevenue").textContent = "Lỗi";
  }
}

function chonMuc(muc) {
  const bang = document.getElementById("bangChiTiet");
  const tieuDe = document.getElementById("tieuDeBang");
  const noiDung = document.getElementById("noiDungBang");
  const tongQuan = document.getElementById("tongQuan");

  tongQuan.style.display = "none";
  bang.style.display = "none";
  document.getElementById("doanhThuTheoDichVu").style.display = "none";

  if (muc === "tongquan") {
    tongQuan.style.display = "flex";
    hienThiTongQuan();

  } else if (muc === "pyc") {
    bang.style.display = "block";
    tieuDe.textContent = "Phiếu yêu cầu chưa hoàn thành";
    hienThiBangTuAPI("/pyc_chua_hoan_thanh", noiDung);

  } else if (muc === "dt") {
    bang.style.display = "block";
    tieuDe.textContent = "Doanh thu theo dịch vụ";
    noiDung.innerHTML = "";
    document.getElementById("doanhThuTheoDichVu").style.display = "block";

    setTimeout(() => {
      veBieuDoDichVu();
      hienThiBangTuAPI("/so_luong_dich_vu", document.getElementById("bangSoLuongDichVu"));
      hienThiBangTuAPI("/khach_hang_theo_dich_vu", document.getElementById("bangKhachHangTheoDichVu"));
    }, 100);

  } else if (muc === "dt_thang") {
    bang.style.display = "block";
    tieuDe.textContent = "Doanh thu theo tháng";
    noiDung.innerHTML = `<canvas id="doanhThuThangChart" height="100"></canvas>`;
    setTimeout(() => veBieuDoDoanhThuThang(), 0);

  } else if (muc === "nb") {
    bang.style.display = "block";
    tieuDe.textContent = "Doanh thu nội bộ";
    noiDung.innerHTML = `<canvas id="chartTongHopDoanhThu" height="100"></canvas>`;
    setTimeout(() => veBieuDoTongHopDoanhThu(), 0);

  } else {
    bang.style.display = "block";
    tieuDe.textContent = "🚧 Tính năng đang phát triển";
    noiDung.innerHTML = "Vui lòng quay lại sau.";
  }
}


// Hàm render bảng động
async function hienThiBangTuAPI(api, noiDung) {
  noiDung.innerHTML = "Đang tải dữ liệu...";
  try {
    const res = await fetch(api);
    const data = await res.json();

    if (!Array.isArray(data) || data.length === 0) {
      noiDung.innerHTML = "Không có dữ liệu.";
      return;
    }

    let html = "<table class='display' id='tableAuto'><thead><tr>";
    for (let key in data[0]) {
      html += `<th>${key}</th>`;
    }
    html += "</tr></thead><tbody>";

    for (let row of data) {
      html += "<tr>";
      for (let key in row) {
        html += `<td>${row[key]}</td>`;
      }
      html += "</tr>";
    }

    html += "</tbody></table>";
    noiDung.innerHTML = html;

    $(document).ready(function () {
      $('#tableAuto').DataTable();
    });
  } catch (err) {
    console.error("Lỗi khi tải bảng:", err);
    noiDung.innerHTML = "Lỗi khi tải dữ liệu.";
  }
}
async function veBieuDoDoanhThuThang() {
  try {
    const response = await fetch("https://doluong6-qlht.hf.space/doanh_thu_theo_thang"); // Đổi URL nếu backend khác
    const data = await response.json();

    if (data.error) {
      console.error("Lỗi từ API:", data.error);
      return;
    }

    const ctx = document.getElementById("doanhThuThangChart").getContext("2d");

    new Chart(ctx, {
      type: "bar",
      data: {
        labels: data.labels,
        datasets: [{
          label: "Doanh thu (VNĐ)",
          data: data.values,
          backgroundColor: "rgba(54, 162, 235, 0.7)",
          borderColor: "rgba(54, 162, 235, 1)",
          borderWidth: 1
        }]
      },
      options: {
        responsive: true,
        scales: {
          y: {
            beginAtZero: true,
            ticks: {
              callback: function(value) {
                return value.toLocaleString("vi-VN") + " VNĐ";
              }
            }
          }
        },
        plugins: {
          legend: {
            display: false
          },
          tooltip: {
            callbacks: {
              label: function(context) {
                return context.raw.toLocaleString("vi-VN") + " VNĐ";
              }
            }
          }
        }
      }
    });

  } catch (error) {
    console.error("Lỗi khi gọi API hoặc vẽ biểu đồ:", error);
  }
}


async function veBieuDoDichVu() {
  const canvas = document.getElementById("chartDichVu");
  if (!canvas) {
    console.error("Không tìm thấy canvas chartDichVu");
    return;
  }

  const ctx = canvas.getContext("2d");

  try {
    const res = await fetch("/ty_le_dich_vu");
    const data = await res.json();

    const chartData = {
      labels: ["Kiểm định", "Hiệu chuẩn", "Đo thử nghiệm"],
      datasets: [{
        label: "Tỷ lệ dịch vụ",
        data: [
          data.Kiem_dinh || 0,
          data.Hieu_chuan || 0,
          data.Do_thu_nghiem || 0
        ],
        backgroundColor: [
          "#4CAF50", "#2196F3", "#FFC107"
        ],
        hoverOffset: 10
      }]
    };

    new Chart(ctx, {
      type: 'doughnut',
      data: chartData,
      options: {
        responsive: true,
        plugins: {
          legend: { position: 'bottom' },
          tooltip: {
            callbacks: {
              label: (context) => `${context.label}: ${context.raw.toFixed(2)}%`
            }
          }
        }
      }
    });

  } catch (error) {
    console.error("Lỗi API /ty_le_dich_vu:", error);
  }
}
function toggleSubmenu(id) {
  const submenu = document.getElementById(id);
  const parent = submenu.parentElement;
  parent.classList.toggle("open");
}