import * as echarts from 'echarts';
import elementResizeDetectorMaker from 'element-resize-detector';
export default {
  props: {
    id: {
      type: String,
      default: 'bar'
    },
    data: {
      type: Array,
      default: function () {
        return [
          {
            name: '1月',
            value: '10',
            data: []
          },
          {
            name: '2月',
            value: '19',
            data: []
          }
        ];
      }
    },
    unit: {
      type: String,
      default: '单位：万元'
    },
    interval: {
      type: Number,
      default: null
    },
    serverName: {
      type: String,
      default: '面积'
    },
    color: {
      type: Array,
      default: function () {
        return ['rgba(15, 252, 252, 1)', 'rgba(53, 197, 124, 1)']
      }
    },
    barBorderRadius: {
      type: Array,
      default: function () {
        return [0, 0, 0, 0]
      }
    }
  },
  data () {
    return {
      chart: null,
      rotate: 0
    };
  },
  watch: {
    data: {
      handler: function (val) {
        this.$nextTick(function () {
          setTimeout(() => {
            this.initChart();
          }, 2000)
        });
      },
      immediate: true,
      deep: true
    }
  },
  create () {

  },
  mounted () {
    // setTimeout(()=>{
    //   this.initChart();
    // },2000)
  },
  computed: {
  },
  methods: {
    // 设置监听器 页面尺寸变化重新绘制图表
    initResizeCallBack () {
      const erd = elementResizeDetectorMaker();
      erd.listenTo(document.getElementById(this.id), () => {
        this.$nextTick(() => {
          if (document.getElementById(this.id).offsetWidth > 500){
            this.rotate = 60;
          }else{
            this.rotate = 0;
          }
          this.chart.resize();
          this.chart.setOption({
            // 新的配置项，例如：
            xAxis: {
              axisLabel: {
                rotate: this.rotate
              },
            }
          });
        });
      });
    },
    initChart () {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chartSetOption();
    },
    chartSetOption () {
      let xAxisData = [];
      let data = [];
      this.data[0]?.data.forEach(child =>{
        data.push({
          name: child.dataNum1,
          barWidth: 20,
          type: 'bar',
          stack: 'Ad',
          emphasis: {
            focus: 'series'
          },
          data: []
        })
      })
      this.data.forEach(item => {
        xAxisData.push(item.deptName);
        item.data.forEach(child =>{
          data.forEach(son =>{
            if (child.dataNum1 == son.name ){
              son.data.push(child.dataNum2)
            }
          })
        })
      });
      const option = {
        color: ["#2195fe","#04e26f","#f7cc3a","#e23ff7","#f77e3f"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            lineStyle: {
              opacity: 0,
            },
          }
        },
        legend: {
          top: '0%',
          right: '2%',
          textStyle: {
            color: 'rgba(210, 238, 255, 1)',
          },
        },
        grid: {
          left: "5%",
          right: "2%",
          bottom: "5%",
          top: "15%",
          containLabel: true,
          z: 22,
        },
        xAxis: [
          {
            splitArea: {
              show: false,
              areaStyle: {
                color: ['RGBA(13, 31, 64, 1)']
              }
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: ['rgba(18, 40, 83, 1)'],
                width: 100
              }
            },
            type: "category",
            gridIndex: 0,
            data: xAxisData,//xAxisData
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              lineStyle: {
                color: "#0c3b71",
              },
            },
            axisLabel: {
              show: true,
              rotate: this.rotate,
              color: 'rgba(185, 211, 235, 1)'
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: this.unit,
            nameTextStyle: {
              color: 'rgba(185, 211, 235, 1)'
            },
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "rgba(185, 211, 235, 1)",
              },
            },
            axisLine: {
              lineStyle: {
                color: "#27b4c2",
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "#11366e",
              },
            },
          },
          {
            type: "value",
            gridIndex: 0,
            max: 100,
            splitNumber: 12,
            splitLine: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            }
          },
        ],
        series: data
      };
      this.chart.setOption(option);
      this.initResizeCallBack();
    }
  }
};
