Spaces:
Runtime error

dl6 / static /script.js
doluong6's picture
Upload script.js
3ef6b8b verified
Raw History Blame Contribute Delete
8.02 kB
// Đă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");
}