<!--
  胶囊柱图
-->
<template>
  <dv-capsule-chart :config="options" :style="styleObj"/>
</template>
<script>
import capsuleChart from "@jiaminghi/data-view/lib/components/capsuleChart";
import Vue from "vue";
Vue.use(capsuleChart)
export default {
  name: "WidgetCapsuleChart",
  props: {
    value: Object,
    ispreview: Boolean,
  },
  data() {
    return {
      //返回图标数据
      options: {
        colors: [],//颜色
        data:[],//数据
        unit: '',//单位
        showValue: true//显示数值
      },
      optionsStyle: {}, // 样式
      optionsData: {}, // 数据
      optionsCollapse: {}, // 图标属性
      optionsSetup: {}
    };
  },
  computed: {
    styleObj() {
      return {
        position: this.ispreview ? "absolute" : "static",
        width: this.optionsStyle.width + "px",
        height: this.optionsStyle.height + "px",
        left: this.optionsStyle.left + "px",
        top: this.optionsStyle.top + "px",
        background: this.optionsSetup.background
      };
    }
  },
  watch: {
    value: {
      handler(val) {
        this.optionsStyle = val.position;
        this.optionsData = val.data;
        this.optionsCollapse = val.collapse;
        this.optionsSetup = val.setup;
        this.editorOptions();
      },
      deep: true
    }
  },
  // mounted() {
  //   this.optionsStyle = this.value.position;
  //   this.optionsData = this.value.data;
  //   this.optionsCollapse = this.value.setup;
  //   this.optionsSetup = this.value.setup;
  //   this.editorOptions();
  // },
  created() {
    this.optionsStyle = this.value.position;
    this.optionsData = this.value.data;
    this.optionsCollapse = this.value.collapse;
    this.optionsSetup = this.value.setup;
    this.editorOptions();
  },
  methods: {
    // 修改图标options属性
    editorOptions() {
      //颜色值修改
      this.setOptionsColor();
      //数据修改
      this.setOptionsData();
    },
    // 颜色修改
    setOptionsColor() {
      const optionsSetup = this.optionsSetup;
      const customColor = optionsSetup.customColor;
      if (!customColor) return;
      const arrColor = [];
      for (let i = 0; i < customColor.length; i++) {
        arrColor.push(customColor[i].color);
      }
      this.options.colors = arrColor;
      this.options = Object.assign({}, this.options);
      this.options.unit =optionsSetup.unit;
      this.options.showValue =optionsSetup.showValue;
    },
    setOptionsData() {
      const optionsData = this.optionsData; // 数据类型 静态 or 动态
      console.log("optionsData.dataType",optionsData.dataType);
      optionsData.dataType == "staticData"
        ? this.staticDataFn(optionsData.staticData)
        : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
    },
    staticDataFn(val) {
      // console.log("设置静态数据",this.options)
      const staticData = typeof val == "string" ? JSON.parse(val) : val;
      this.options.data=staticData;
    },
    dynamicDataFn(val, refreshTime) {
      if (!val) return;
      if (this.ispreview) {
        this.getEchartData(val);
        this.flagInter = setInterval(() => {
          this.getEchartData(val);
        }, refreshTime);
      } else {
        this.getEchartData(val);
      }
    },
    getEchartData(val) {
      const data = this.queryEchartsData(val);
      data.then(res => {
         this.options.data = res;
        // console.log("WidgetCapsuleChart2222====",res);
        // this.staticDataFn(res);
      });
    }
  }
};

</script>
<style scoped lang="scss">
.echarts {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
