<!--
动态环图
-->
<template>
<!--  <div id="widget-active-ring-chart" :style="styleObj">-->
    <dv-active-ring-chart :config="options" :style="styleObj"/>
<!--  </div>-->
</template>
<script>
import Vue from "vue";
import activeRingChart from "@jiaminghi/data-view/lib/components/activeRingChart";
Vue.use(activeRingChart)
export default {
  name: "WidgetActiveRingChart",
  props: {
    value: Object,
    ispreview: Boolean,
  },
  data() {
    return {
      //返回图标数据
      options: {
        radius:'75%',//环半径
        activeRadius:'85%',//环半径（动态）
        data:[],//环数据
        lineWidth:40,//环数据
        activeTimeGap:3000,//切换间隔(ms)
        color:[],//环颜色
        digitalFlopStyle: {
            fontSize: 30,
            fill: '#3de7c9'
        },//数字翻牌器样式
        digitalFlopToFixed	:0,//数字翻牌器小数位数
        digitalFlopUnit	:'',//数字翻牌器单位
        animationFrame	:50,//动效帧数
        showOriginValue	:false//显示原始值
      },
      optionsStyle: {}, // 样式
      optionsData: {}, // 数据
      optionsCollapse: {}, // 图标属性
      optionsSetup: {}
    };
  },
  computed: {
    styleObj() {
      const style ={
        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
      };
      return style;
    },
  },
  watch: {
    value: {
      handler(val) {
        this.optionsStyle = val.position;
        this.optionsData = val.data;
        this.optionsSetup = val.setup;
        this.editorOptions();
      },
      deep: true
    }
  },
  created() {
    this.optionsStyle = this.value.position;
    this.optionsData = this.value.data;
    this.optionsSetup = this.value.setup;
    this.editorOptions();
  },
  methods: {
    // 修改图标options属性
    editorOptions() {
      //数据修改
      this.setOptionsData();
      //环颜色修改
      this.setOptionsColor();
      //数字翻牌器样式
      this.setFontStyle();
      //基础数据修改
      this.setOptionsConfig();
    },
    // 颜色修改
    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.color = arrColor;
    },
    //字体修改
    setFontStyle() {
      const optionsSetup = this.optionsSetup;
      const fontStyle = {};
      fontStyle.fill=optionsSetup.fill;
      fontStyle.fontSize=optionsSetup.fontSize;
      fontStyle.fontWeight=optionsSetup.fontWeight;
      this.options.digitalFlopStyle = fontStyle;
    },
    // 配置修改
    setOptionsConfig() {
      const optionsSetup = this.optionsSetup;
      this.options.radius = optionsSetup.radius+"%";
      this.options.activeRadius = optionsSetup.activeRadius+"%";
      this.options.lineWidth = optionsSetup.lineWidth;
      this.options.activeTimeGap = optionsSetup.activeTimeGap;
      this.options.digitalFlopToFixed = optionsSetup.digitalFlopToFixed;
      this.options.digitalFlopUnit = optionsSetup.digitalFlopUnit;
      this.options.animationFrame = optionsSetup.animationFrame;
      this.options.showOriginValue = optionsSetup.showOriginValue;
      //改变options
      // console.log("配置修改：",this.styleObj);
      this.options={...this.options};
    },
    //数据类型
    setOptionsData() {
      const optionsData = this.optionsData; // 数据类型 静态 or 动态
      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.renderingFn(res);
      });
    },
    renderingFn(val) {
      this.options.data=val;
    }

  }
};

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