import { connect } from 'herculex';
import request from './../../../core/utils/request';
const apiUr = '/api/v1/component/richText/detail';
Component(connect({})({
  data: {
    richText: '',
  },
  props: {
    componentData: {},
  },
  didMount() {
    const {
      componentUUID,
      componentExtInfo,
    } = this.props.componentData;
    const {
      remoteUrl,
      templateExportJsonAllow,
    } = componentExtInfo;
    const allow = templateExportJsonAllow || 'N';
    /*
    * N 为本地获取
    * Y 为远端获取
    * */
    if (allow === 'N') this.getLocalData(componentUUID);
    if (allow === 'Y' && remoteUrl) this.getCloudData(remoteUrl);
  },
  methods: {
    setRichText(data) {
      const { abRichText } = data || {};
      this.setData({ richText: abRichText });
    },
    async getLocalData(uuid) {
      const options = {
        url: apiUr,
        method: 'GET',
        data: { componentUuid: uuid },
      };
      const [err, data] = await request(options);
      if (!err) {
        this.setRichText(data);
      }
    },
    getCloudData(remoteUrl) {
      my.request({
        url: remoteUrl,
        method: 'GET',
        success: (e) => this.setRichText(e.data),
      });
    },
  },
}));