import emitter from '../../../../../../components/xform/utils/emitter';
import i18n from '../../../../../../components/xform/utils/i18n';
import fieldMixin from '../../../../../../components/xform/form-designer/form-widget/field-widget/fieldMixin';

import {deepClone} from '../../../../../../components/xform/utils/util';

import {use} from 'echarts/core';
import {CanvasRenderer} from 'echarts/renderers';
import {PieChart} from 'echarts/charts';
import {TitleComponent, TooltipComponent, LegendComponent} from 'echarts/components';
import {THEME_KEY} from 'vue-echarts';
import {extendDeeply} from "@base/utils/index.js";

use([CanvasRenderer, PieChart, TitleComponent, TooltipComponent, LegendComponent]);

let modules = {};
modules = {
  name: 'echart-pie-widget',
  componentName: 'FieldWidget', //必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin, i18n],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: ''
    }
  },
  provide: {
    [THEME_KEY]: 'light'
  },
  data() {
    return {
      requestAccess: null,
      serieClone: {},
      showChart: true,
      colors:[]
    };
  },
  computed: {
    pHeightStyle() {
      let echartHeight = this.field.options.echartHeight;
      if (echartHeight) {
        if (isNaN(echartHeight)) {
          if (echartHeight == 'auto' || echartHeight.slice(-1) == '%') {
            return '100%';
          }
        }
      }
      return '';
    },
    heightStyle() {
      let echartHeight = this.field.options.echartHeight;
      if (echartHeight) {
        let height = echartHeight;
        if (!isNaN(echartHeight)) {
          height = echartHeight + 'px';
        }
        return height;
      }
      return '';
    },
    chartOption() {
      return this.field.options.echarPieOption
    }
  },
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  async created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */

    this.registerToRefList();
    this.initEventHandler();

    this.handleOnCreated();
    this.initOption();
    this.serieClone = deepClone(this.field.options.echarPieOption.series[0]);
    // this.loadData();
  },

  mounted() {
    this.handleOnMounted();
  },

  beforeDestroy() {
    this.unregisterFromRefList();
  },

  methods: {
    initOption() {
      let echartConfig = this.handleCustomEvent(this.field.options.echartConfig) || {};
      this.field.options.echarPieOption = extendDeeply(this.field.options.echarPieOption, echartConfig);
    },
    /*async loadData() {
      if (this.designState) {
        return;
      }
      await this.initAccess();

      let requestAccess = this.requestAccess;
      if (!requestAccess) {
        return;
      }
      var a, l, s;
      (l = this.getGlobalDsv() || {}),
        (s = new Object({})),
        overwriteObj(s, l),
        (s['widgetName'] = this.field.options.name),
        (s['requestAccess'] = requestAccess),
        (a = requestAccess.dsModel);
      // r = i.sent,
      runDataSourceRequest(a, s, this.getFormRef(), !1, this.$message).then(res => {
        this.field.options.echarPieOption.series[0].data = res || [];
      });
    },
    async initAccess() {
      if (this.field.options.dsEnabled && this.field.options.accessCode) {
        this.field.options.echarPieOption.series[0].data = [];
        return this.getFormRef().$http({
          url: SUPPORT_PREFIX + `/report_requestaccess/getByCode`,
          method: `post`,
          data: {
            stringOne: this.field.options.accessCode
          },
          isLoading: true,
          // modalStrictly: true,
          success: res => {
            let requestAccess = res.objx || {};

            let dsModel = {};
            if (requestAccess.accessViewContent) {
              dsModel = JSON.parse(requestAccess.accessViewContent);
            }
            if (!dsModel.headers) dsModel.headers = [];
            if (!dsModel.params) dsModel.params = [];
            if (!dsModel.data) dsModel.data = [];
            if (dsModel.dataHandlerCode == null) dsModel.dataHandlerCode = '';
            if (dsModel.configHandlerCode == null) dsModel.configHandlerCode = '';
            if (dsModel.errorHandlerCode == null) dsModel.errorHandlerCode = '';

            requestAccess.dsModel = dsModel;
            this.requestAccess = requestAccess;
          }
        });
      } else {
        return null;
      }
    },*/
    loadDataDefaultHandle() {
      let reportTemplate = this.getFormRef().reportTemplate;
      let formCode = reportTemplate.formCode;
      let formScriptEnabled = this.field.options.formScriptEnabled || false;
      let scriptCode = this.field.options.formScriptCode
      if (!scriptCode) return;
      let accessParam = this.handleCustomEvent(this.field.options.formScriptParam)
      return this.formHttp({
        scriptCode: scriptCode,
        data: {
          formCode: formCode,
          formVersion: reportTemplate.formVersion,
          taBm: this.fieldKeyName,
          data: {
            ...accessParam
          }
        },
        callback: res => {
          let rows = res.objx || [];
          this.setValue(rows)
        }
      });
    },
    setValue(rows) {
      this.showChart = false;
      this.field.options.echarPieOption.series[0].data = rows || [];
      this.$nextTick(() => {
        this.showChart = true;
      })
    },
    getChart() {
      return this.$refs.chart;
    }
  }
};
export default modules
