<!--
 * @LastEditors: gcz
-->
<!-- https://blog.csdn.net/zyz13883658166/article/details/124430938 -->
<template>
  <div ref="wrap">
    <!-- <div>{{$store.state.addr.mapLevel}}</div> -->
    <div class="ntitle">
      <span v-if="$store.state.addr.mapLevel=='province'">{{$store.state.addr.selectAddr}}各地州(市){{option.title.text}}</span>
      <span v-if="$store.state.addr.mapLevel=='city'">{{$store.state.addr.selectAddr}}各县{{option.title.text}}</span>
      <span v-if="$store.state.addr.mapLevel=='district'">{{$store.state.addr.selectAddr}}各乡镇{{option.title.text}}</span>
      <span v-if="$store.state.addr.mapLevel=='street'">{{$store.state.addr.selectAddr}}{{streetTitleText}}{{option.title.text}}</span>
    </div>
    <!-- <div class="addr">{{$store.state.addr.selectAddr}}</div> -->
    <div :id="mid" class="echarts" :style="style"></div>
  </div>
  
</template>
<script>
import * as echarts from "echarts";
const idGen = () => {
  return new Date().getTime();
};
export default {
  name: "HelloEcharts",
  props: {
    height: {
      type: String,
      default: null,
    },
    width: {
      type: String,
      default: "100%",
    },
    option: {
      type: Object,
      default: null,
    },
    mid: {
      type: String,
      default: null,
    },
    streetTitleText: {
      type: String,
      default: '各村',
    },
  },
  data() {
    return {
      uuid: null,
      myChart: null,
      domHeight:null,
    };
  },
  created() {
    this.uuid = idGen();
    // console.log('this.option',this.option);
    
  },
  watch: {
    // '$store.state.addr.mapLevel': {
    //   handler:function (newVal, oldVal) {
    //       console.log('newVal',newVal);
    //       console.log('oldVal',oldVal);
    //   },
    //   immediate: true
    // },
    width() {
      //如果实例可用
      if (this.myChart) {
        this.myChart.resize({
          Animation: {
            duration: 300,
          },
        });
      }
    },
    option() {
      // console.log('option',console.log('this.option',JSON.parse(JSON.stringify(this.option))));
      if (this.myChart) {
        //   notMerge这个方法，是表示配置不重复，但是目前这个分代码没有生效
        this.myChart.setOption(this.option),
          {
            notMerge: true,
          };
      }
    },
  },
  computed: {
    style() {
      return {
        height: this.height?this.height:this.domHeight-48 +'px',
        width: this.width,
      };
    },
  },
  methods: {
    intChar(charUuid){
      // console.log('intChar charUuid',charUuid);
      // console.log('this.uuid 前',this.uuid);
      // // this.uuid = idGen();
      // console.log('this.uuid 后',this.uuid);
      echarts.init(document.getElementById(charUuid)).clear();
      this.myChart = echarts.init(document.getElementById(charUuid));
      this.myChart.setOption(this.option);
    },
    resizeChar(charUuid){
      console.log('charUuid',charUuid);
      this.myChart = echarts.init(document.getElementById(charUuid));
      let obj = this.$refs.wrap;
      let h = window.getComputedStyle(obj).height;
      this.myChart.resize({height:h.slice(0, -2)-48+'px'})
      console.log('this.myChart h',h);

    }
  },
  mounted() {
    // console.log('height',this.height);
    let obj = this.$refs.wrap;
    let h = window.getComputedStyle(obj).height;
    // console.log('h',h);
    this.domHeight = h.slice(0, -2);
    // console.log('domHeight',this.domHeight);
    // console.log('style',this.style);
    // console.log('echarts mounted');
    // echarts.init(document.getElementById('echarts')).dispose();//销毁前一个实例
    this.$nextTick(()=>{
      this.myChart = echarts.init(document.getElementById(this.mid));
      this.myChart.setOption(this.option);

    })
    
  },
};
</script>
<style lang='scss' scoped>

</style>