<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: {
		xAxis: [],
		data:[]
	}
  },
  // 高度
  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: {
			trigger: 'axis'
		},
		legend: {
			show: false,
			textStyle: {
				color: "#000"
			}
		},
		grid: {
			top: '16%',
			left: '1%',
			right: '1%',
			bottom: '2%',
			containLabel: true
		},
		xAxis: {
			type: 'category',
			boundaryGap: true,
			splitLine: {
				show: true,
				lineStyle: {
					color: '#c6c6c6'
				}
			},
			axisLabel: {
				fontSize: 12,
				color:"#323232",
				showMaxLabel: true
			},
			data: data.value.xAxis
		},
		yAxis: [{
			name: data.value.name,
			type: 'value',
			nameTextStyle:{
				color:'#B05409'  
			},
			axisLabel: {
				fontSize: 12,
				color:"#B05409",
				showMaxLabel: true
			},
			axisLine: {
				show: true,
				lineStyle: {
					color: ['#B05409']
				}
			},
			splitLine: {
				show: true,
				lineStyle: {
					color: ['#c6c6c6']
				}
			}
		},{
			name: data.value["name2"],
			type: 'value',
			nameTextStyle:{
				color:'#5470C6'  
			},
			axisLabel: {
				fontSize: 12,
				color:"#5470C6",
				showMaxLabel: true
			},
			axisLine: {
				show: true,
				lineStyle: {
					color: ['#5470C6']
				}
			},
			splitLine: {
				show: true,
				lineStyle: {
					color: ['#c6c6c6']
				}
			}
		}],
		series: data.value.data
	}
});

const { option } = toRefs(data1);

// 观察 data ，重新绘制
watch(data, (newValue) => {
    option.value.series = newValue;
},{ deep: true });
watch(option, (newValue) => {
    echartInstance.setOption(newValue, true);
},{ deep: true });
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>