<template>
	<Card title="群塔报警对比分析">
		<div class="num" v-if="towerAlarmData">{{ towerAlarmData.alarmNum }}</div>
		<div class="styleImg"></div>

		<div id="echartsLeftCenter" style="width: 100%; height: 100%"></div>
	</Card>
</template>

<script lang="ts" setup>
import { onMounted, ref, onBeforeUnmount } from "vue";
import * as echarts from "echarts";
import Card from "@/components/card.vue";
import { getDistinguishedNumApi } from "@/api/modules/tower";
import { GlobalStore } from "@/stores";
const store = GlobalStore();
// let timer = ref(null);
// //角度，用来做简单的动画效果的
// let angle = ref(0);
// let value = ref(55.33);
let trafficWay = ref([
	{
		name: "回转限位",
		value: 12,
		radio: 0.13
	},
	{
		name: "风速",
		value: 52,
		radio: 0.01
	},
	{
		name: "力矩",
		value: 15,
		radio: 3.9
	},
	{
		name: "倾斜",
		value: 56,
		radio: 10.5
	},
	{
		name: "塔吊间碰撞",
		value: 17,
		radio: 0.4
	},
	{
		name: "吊钩限位",
		value: 19,
		radio: 85.3
	}
]);
let totalData = ref([
	{
		name: "回转限位",
		value: 12
	},
	{
		name: "风速",
		value: 52
	},
	{
		name: "力矩",
		value: 15
	},
	{
		name: "倾斜",
		value: 56
	},
	{
		name: "塔吊间碰撞",
		value: 17
	},
	{
		name: "吊钩限位",
		value: 19
	}
]);
let data = ref([]);
let color = ref(["#82FBEA", "#EEA959", "#4CC4F8 ", "#E0D838", "#EC6266", "#6375C7"]);
let seriesOption = ref([] as any);
let option = ref([] as any);

function initOption() {
	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.value,
		center: ["25%", "50%"],

		title: {
			text: "报警总数",
			top: "125",

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

function _pie3() {
	let dataArr = [];
	for (let 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;
}
let dataName = ref([] as any);
let towerAlarmData = ref(null as any);
const getTowerNumAndAlarmList = async () => {
	const res: any = await getDistinguishedNumApi({
		projectSn: store.sn
	});
	if (res.result) {
		let projectWorkerTypeOption: any = [];
		res.result.typeNumList.forEach(item => {
			// 只取有数据的工种
			towerAlarmData.value = res.result;
			// console.log("塔吊左中圆环图", res.result);
			// dataName.value.push(item.enterpriseName);
			if (item.num > 0) {
				projectWorkerTypeOption.push({
					value: item.num,
					name: item.typeName
				});
				dataName.value.push(item.typeName);
			}
		});
		trafficWay.value = projectWorkerTypeOption;
		totalData.value = projectWorkerTypeOption;
		initOption();
	}

	for (let i = 0; i < totalData.value.length; i++) {
		data.value.push({
			value: totalData.value[i].value,
			name: totalData.value[i].name,
			itemStyle: {
				normal: {
					borderWidth: 0,
					shadowBlur: 2,
					borderColor: color.value[i],
					shadowColor: color.value[i]
				}
			}
		});
	}
};

onMounted(async () => {
	// initOption();
	await getTowerNumAndAlarmList();
	draw();
});

function draw() {
	console.log("塔吊左侧圆环图", option.value);
	let echartsTest = echarts.init(document.getElementById("echartsLeftCenter"));
	echartsTest.setOption(option.value, true);
}

onBeforeUnmount(() => {
	// clearInterval(timer);
});
</script>

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