<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 emit = defineEmits();
const { proxy } = getCurrentInstance(); // 获取实例中的proxy
let echart;
let echartInstance;

const props = defineProps({
  // 数据
  data: {
    type: Object,
    default: {
		xAxis: [],
		data:[],
		show: true,
		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: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				crossStyle: {
					color: '#999'
				}
			}
		},
		grid: {
			top: '6%',
			left: '2%',
			right: '2%',
			bottom: '1%',
			containLabel: true
		},
		xAxis: {
			type: 'category',
			axisLabel: {
				color: "#000",
				fontSize: 10
			},
			axisTick: {
				alignWithLabel: true
			},
			data: data.value.xAxis
		},
		yAxis: {
			max: 100,
			min: -100,
			splitNumbers: 3,
			type: 'value',
			axisLabel: {
				show: data.value.show,
				fontSize: 12,
				showMaxLabel: true,
				formatter: function (value) {
					if (value === 0) {
						return value;
					} else {
						return ''
					}
				}
			},
			axisLine: {
			    show: false
			},
			splitLine: {
				show: false,
				lineStyle: {
					color: ['#aaa']
				}
			}
		},
		visualMap: {
			top: 10,
			right: 10,
			show: false,
			pieces: [{
				gt: -50,
				lte: 50,
				color: "#218066"
			}],
			outOfRange: {
				color: "#D93434"
			}
		},
		series: [{
			data: data.value.data,
			type: 'bar',
			barWidth: "40%",
			markLine : {
				symbol:'none',
				lineStyle: {
					normal: {
						type: 'solid',
						color: '#616161'
					}
				},
				label: {
					position: 'start',
					color: '#767676'
				},
				data : [
					{ yAxis: -50 ,name:'min'},
					{ yAxis: 50 ,name:'max'},
				]
			},
			label: {
			    normal: {
			        show: !data.value.show,
			        position: 'top',
			        color: "#000",
					fontSize:10,
			        formatter: function (params) {
			            var index = params.dataIndex;
			            return data.value.data[index] + data.value.unit
			        }
			    }
			}
		}]
	}
});

const { option } = toRefs(data1);

// 观察 data ，重新绘制
watch(data, (newValue) => {
    option.value.series[0].data = newValue.data;
	option.value.xAxis.data = newValue.xAxis;
},{ 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);
		echartInstance.on('click', params => {
			emit("clickBarTr", parseInt(params.dataIndex)+1);
		})
	})
});

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

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

useResize();
</script>