<template>
  <div ref="echart" :class="name" :style="{ height: height, width: width }"></div>
</template>

<script setup>
import * as echarts from "echarts";
import useResize from "@/hooks/useResize"; // hook
const store = useStore();
const { proxy } = getCurrentInstance(); // 获取实例中的proxy

let echart;
let echartInstance;

const props = defineProps({
  // 数据
  data: {
    type: Object,
    default: {
		max: 3000,
		value: 0,
		name: "name",
		show: false,
		unit: ""
	},
  },
  // 高度
  height: {
    type: [Number, String],
    default: "300px",
  },
  // 宽度
  width: {
    type: [Number, String],
    default: "100%",
  },
  name:  {
  		type: String,
  		default: 'echart'
  	},
});

const { data, name } = toRefs(props);
const data1 = reactive({
	option: {
		tooltip: {
			formatter: '{a} <br/>{b} '+data.value.name+': {c}'+data.value.unit
		},
		series: [{
			name: '实时数据',
			min: 0,
			max: 360,
			splitNumber:12,
			type: 'gauge',
			center: ['50%', '55%'],
			radius: '80%',
			startAngle:90,
			endAngle: -269.999999,
			detail: {
				show: false
			},
			data: [{
				value: data.value.value
			}],
			axisLine: {
				lineStyle: {
					color: [[1, '#1e90ff']],
					width: 5
				}
			},
			axisTick: {
				distance: 2,
				length: 5,
				lineStyle: {
					color: (store.state.settings.sideTheme.indexOf("dark") != -1)?"#fff":"#000"
				},
				formatter: function(e) {
					switch (e + "") {
				 		case "0":
				 			return "0";
				 		case "30":
				 			return "30";
				 		case "60":
				 			return "60";
				 		case "90":
				 			return "90";
				 		case "120":
				 			return "120";
				 		case "150":
				 			return "150";
				 		case "180":
				 			return "180";
				 		case "210":
				 			return "210";
				 		case "240":
				 			return "240";
				 		case "270":
				 			return "270";
				 		case "300":
				 			return "300";
				 		case "330":
				 			return "330";
				 		default:
				 			return "";
				 	}
				}
			},
			pointer: {
				width: 5,
				length: "50%",
			},
			splitLine: {
				distance: 2,
				length: 8,
				lineStyle: {
					width: 2,
					color: (store.state.settings.sideTheme.indexOf("dark") != -1)?"#fff":"#000"
				}
			},
			axisLabel: {
				color: (store.state.settings.sideTheme.indexOf("dark") != -1)?"#fff":"#000",
				fontSize: 10,
				distance: 10
			}
		}]
	}
});

const { option } = toRefs(data1);

// 观察 data ，重新绘制
watch(data, (newValue) => {
    // option.value.yAxis.data = newValue;
    option.value.series[0].data[0].value = newValue.value;
  },{ deep: true }
);
watch(option, (newValue) => {
    echartInstance.setOption(newValue, true);
  },{ deep: true }
);
watch(() => store.state.settings.sideTheme, (newValue) => {
	let _ais = (newValue.indexOf("dark") != -1)?"#fff":"#000";
	echartInstance.setOption({
		series:[{
			axisLabel: {color: _ais},
			splitLine: { lineStyle: {color: _ais}},
			axisTick: { lineStyle: {color: _ais}}
		}]
	});
});
onMounted(() => {
	// echart = proxy.$refs.echart;
	// echartInstance = echarts.init(echart, "macarons");
	// echartInstance.setOption(option.value, true);
	nextTick(()=>{
		echartInstance = echarts.init(document.getElementsByClassName(name.value)[0]);
		echartInstance.setOption(option.value, true);
	})
});

function resize() {
	echartInstance.resize();
}

// 暴露函数 （供hook调用）
defineExpose({
  resize,
});

useResize();
</script>