<!--
排名轮播表
-->
<template>
  <div id="widget-scroll-ranking-board" :style="styleObj">
    <dv-scroll-ranking-board :config="options" :style="styleObj"/>
  </div>
</template>
<script>
import Vue from "vue";
import scrollRankingBoard from "@jiaminghi/data-view/lib/components/scrollRankingBoard";
Vue.use(scrollRankingBoard)
export default {
  name: "WidgetScrollRankingBoard",
  props: {
    value: Object,
    ispreview: Boolean,
  },
  data() {
    const {valueFormatter} = this;
    return {
      //返回图标数据
      options: {
        data:[],//表数据
        rowNum:'5',//表行数(显示)
        waitTime: 2000,//轮播时间间隔(ms)
        carousel: 'single',//轮播方式 'single'|'page'
        unit: '',//数值单位
        sort: true,//自动排序
        isValueFormatter: true,//数值格式化
        valueFormatter: valueFormatter//数值格式化

      },
      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.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.setOptionsConfig();
      //数据修改
      this.setOptionsData();
    },
    // 配置修改
    setOptionsConfig() {
      const optionsSetup = this.optionsSetup;
      this.options.rowNum =optionsSetup.rowNum;
      this.options.waitTime =optionsSetup.waitTime;
      this.options.carousel =optionsSetup.carousel;
      this.options.unit =optionsSetup.unit;
      this.options.sort =optionsSetup.sort;
      this.options.isValueFormatter =optionsSetup.isValueFormatter;
      if(this.options.isValueFormatter){
        this.options.valueFormatter= this.valueFormatter;
      }else{
        this.options.valueFormatter=undefined;
      }
    },
    //数据类型
    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;
    },
    valueFormatter ({ value }) {
      // console.warn(arguments)
      const reverseNumber = (value + '').split('').reverse()
      let valueStr = ''

      while (reverseNumber.length) {
        const seg = reverseNumber.splice(0, 3).join('')
        valueStr += seg
        if (seg.length === 3) valueStr += ','
      }
      return valueStr.split('').reverse().join('')
    }
  }
};

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