<template>
	<Card title="部门管理人员出勤分析">
		<div class="num">{{ max }}</div>
		<div class="styleImg"></div>
		<div id="echartsRight" style="width: 100%; height: 100%"></div>
	</Card>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, onBeforeUnmount } from "vue";
import * as echarts from "echarts";
import { getworkerAttendanceTrendApi } from "@/api/modules/labor";
import Card from "@/components/card.vue";
import { GlobalStore } from "@/stores";
const store = GlobalStore();

let totalData = ref([
	{
		name: "XX有限公司1",
		value: 12,
		radio: 0.13
	},
	{
		name: "XX有限公司2",
		value: 52,
		radio: 0.01
	},
	{
		name: "XX有限公司3",
		value: 15,
		radio: 3.9
	},
	{
		name: "XX有限公司4",
		value: 56,
		radio: 10.5
	},
	{
		name: "XX有限公司5",
		value: 17,
		radio: 0.4
	},
	{
		name: "XX有限公司6",
		value: 19,
		radio: 85.3
	}
]);
let trafficWay = ref([
	{
		name: "XX有限公司1",
		value: 12
	},
	{
		name: "XX有限公司2",
		value: 52
	},
	{
		name: "XX有限公司3",
		value: 15
	},
	{
		name: "XX有限公司4",
		value: 56
	},
	{
		name: "XX有限公司5",
		value: 17
	},
	{
		name: "XX有限公司6",
		value: 19
	}
]);
let color = ref(["#82FBEA", "#EEA959", "#4CC4F8 ", "#E0D838", "#EC6266", "#6375C7"]);

let option = ref({} as any);
let seriesOption = ref({} as any);
let data = ref([] as any);
let dataName = ref([] as any);

function _pie3() {
	let dataArr = [];
	for (var i = 0; i < 100; i++) {
		if (i % 2 === 0) {
			dataArr.push({
				name: (i + 1).toString(),
				value: 10,
				itemStyle: {
					normal: {
						color: "#5A5DE0",
						borderWidth: 4,
						borderColor: "#87E0FB"
					}
				}
			});
		} else {
			dataArr.push({
				name: (i + 1).toString(),
				value: 20,
				itemStyle: {
					normal: {
						color: "rgba(0,0,0,0)",
						borderWidth: 0,
						borderColor: "rgba(0,0,0,0)"
					}
				}
			});
		}
	}
	return dataArr;
}
// //获取圆上面某点的坐标(x0,y0表示坐标，r半径，angle角度)
// function getCirlPoint(x0, y0, r, angle) {
// 	let x1 = x0 + r * Math.cos((angle * Math.PI) / 180);
// 	let y1 = y0 + r * Math.sin((angle * Math.PI) / 180);
// 	return {
// 		x: x1,
// 		y: y1
// 	};
// }

let max = ref(0 as Number);
//获取部门管理人员分析数据
// let timer = null
const getProjectWorkerList = async () => {
	const res: any = await getworkerAttendanceTrendApi({
		projectSn: store.sn
	});
	console.log("部门管理人员分析数据", res);
	let projectWorkerTypeOption: any = [];
	if (res.result) {
		res.result.projectCompanyWorkTotalList.forEach(item => {
			// 只取有数据的工种
			if (item.totalPersonNum > 0) {
				// 计算工人总和
				max.value = max.value + item.totalPersonNum;
				dataName.value.push(item.enterpriseName);
				projectWorkerTypeOption.push({
					value: item.totalPersonNum,
					name: item.enterpriseName
				});
			}
		});
		// 接口成功获取新值
		totalData.value = projectWorkerTypeOption;
		trafficWay.value = projectWorkerTypeOption;
		// optionData.value = getData(data);
		// 初始化option
		initOption();
		console.log(totalData.value, "调用接口后绘图", data.value);
	}
	// 处理data
	for (let i = 0; i < trafficWay.value.length; i++) {
		data.value.push({
			value: trafficWay.value[i].value,
			name: trafficWay.value[i].name,
			itemStyle: {
				normal: {
					borderWidth: 0,
					shadowBlur: 2,
					borderColor: color.value[i],
					shadowColor: color.value[i]
				}
			}
		});
	}
};

function initOption() {
	console.log("data", data.value);

	seriesOption.value = [
		{
			name: "",
			type: "pie",
			center: ["25%", "50%"],
			clockWise: false,
			radius: [70, 60],
			hoverAnimation: false,
			itemStyle: {
				normal: {
					label: {
						show: false
					}
				}
			},
			data: data.value
		},
		{
			type: "pie",
			center: ["25%", "50%"],

			radius: [50, 50],
			zlevel: 2,
			silent: true,
			label: {
				normal: {
					show: false
				}
			},
			labelLine: {
				normal: {
					show: false
				}
			},
			data: _pie3()
		}
	];

	option.value = {
		// backgroundColor: "#0A2E5D",
		color: color,
		center: ["25%", "50%"],

		title: {
			text: "出勤总人数",
			top: "125",

			textAlign: "center",
			left: "24%",
			textStyle: {
				color: "#fff",
				fontSize: 12,
				fontWeight: "400"
			}
		},
		tooltip: {
			show: true
		},
		legend: {
			show: true,
			icon: "rect",
			itemGap: 20,
			itemWidth: 15,
			itemHeight: 15,
			orient: "vertical",
			x: "right",
			data: dataName.value,
			right: 400,
			left: 260,
			bottom: 30,
			align: "left",
			textStyle: {
				color: "#fff"
			}
		},
		toolbox: {
			show: false
		},
		series: seriesOption.value
	};
}

onMounted(async () => {
	await getProjectWorkerList();
	draw();
});
onBeforeUnmount(() => {
	// clearInterval(timer);
});

// 图表绘制
function draw() {
	let echartsTest = echarts.init(document.getElementById("echartsRight"));
	echartsTest.setOption(option.value);
	console.log('右中出勤绘图数据',option.value);
}
</script>

<style lang="scss" scoped>
.num {
	font-family: sadigitalNumber;
	font-size: calc(100vw * 24 / 1920);
	color: #fff;
	position: absolute;
	top: 52.5%;
	left: 80%;
	z-index: 9;
}
.styleImg {
	left: 75.5%;
	top: 46%;
	width: 10%;
	position: absolute;
	height: 20%;
	background: url("@/assets/images/dustNoise/motionEffect.webp") no-repeat;
	background-size: 100% 100%;
}
</style>
