import Vue from 'vue';
import settingConfig from '@/settings.js'

let configUtil = {
  getWfObj(path) {
    return require(`../../../components/mobile/wf${path}`).default;
  },
  Vue,
  settingConfig
};
export default {
  data() {
    return {
      wfInfo: {},
      taskParam: {},
      hasWf: false,
      showWfStartBtn: false,
      showWfModifyBtn: false,
      wfOption: {}
    }
  },
  methods: {
    _getBillInfo: async function (option) {
      let defaultOption = {
        url: null,
        method: `post`,
        data: {},
        isLoading: true,
        modalStrictly: true,
        success: null
      };
      let opt = Object.assign({}, defaultOption, option);
      delete opt.wfConfig;
      return this.$commonHttp({
        ...opt,
        success: (res, response) => {
          option.success && option.success(res, response);
          if (option.wfConfig) {
            let wfConfig = option.wfConfig(res);
            if (wfConfig) {
              let urlArr = opt.url.split('/');
              let wf_prefix = "/" + urlArr[1]
              let opt2 = Object.assign({}, wfConfig, {
                serviceId: urlArr[1],
                wf_prefix,
                parentVue: this,
              });


              this.wfOption = opt2;

              let hasWf = false;
              let wfInfo, currentTask, taskParam;
              this.initWfInfo(opt2, {
                success: res => {
                  if (res.objx && res.objx.procInstId) {
                    hasWf = true;
                    wfInfo = res.objx || {};
                    this.wfInfo = wfInfo;
                    this.hasWf = hasWf;
                    if (wfInfo.taskId) {
                      this.getTaskParams(opt2, wfInfo.taskId, {
                        success: res2 => {
                          if (res2.objx) {
                            taskParam = res2.objx;
                            taskParam.objId = opt2.objId;
                            let showWfModifyBtn = wfConfig.showModifyBtn != null ? wfConfig.showModifyBtn : true;
                            this.showWfModifyBtn = showWfModifyBtn && hasWf && taskParam && taskParam.mobileurl && wfInfo.toSubmit;
                            this.taskParam = taskParam;
                          }
                        }
                      })
                    }
                  }
                  if (!hasWf) {
                    let showWfStartBtn = wfConfig.showStartBtn != null ? wfConfig.showStartBtn : true;
                    this.showWfStartBtn = showWfStartBtn
                  } else {

                  }
                }
              })

              /*//初始化流程
              setTimeout(() => {
                this.$refs.wfCardPane.init({
                  objId: this.dataId, //单据ID
                  wfCode: 'sampleOrder', //流程编码
                  showStartBtn: this.order.status === 0 && !this.customerFlag, //是否放出启动按钮,流程已启动时此参数失效
                  parentVue: this,
                  wfConfig: opt2,
                  serviceId: urlArr[1]
                })
              },20);*/
            }
          }
        }
      });
    },
    initWfInfo(option, httpConfig) {

      let that = this;
      let id = option.objId;
      let code = option.wfCode;
      return that.$http({
        url: this.wfOption.wf_prefix + '/wf/getWfInfo',
        method: `post`,
        data: {
          objTypeCode: code,
          objId: id
        },
        isLoading: true,
        modalStrictly: true,
        ...httpConfig
      });
    },
    getTaskParams(option, taskId, httpConfig) {
      let that = this;
      return that.$http({
        url: this.wfOption.wf_prefix + '/wf/getTaskVariables',
        method: `post`,
        data: {
          stringOne: taskId
        },
        isLoading: true,
        modalStrictly: true,
        ...httpConfig
      });
    },
    openStartWfDialog() {
      let that = this;
      let option = this.wfOption;
      let done = () => {
        let a = configUtil.Vue.extend(configUtil.getWfObj('/wfStartDialog.vue'));
        let instance = new a({
          i18n: that._i18n,
          propsData: {
            code: option.wfCode,
            dataId: option.objId,
            serviceId: option.serviceId,
            startParam: option.startParam,
            parentVue: that,
            visible: true,
          }
        });
        this.$el.append(instance.$mount().$el)
      }
      if (option.onStart) {
        option.onStart(done)
      } else {
        done();
      }
    },

    openWfModifyDialog() {
      let that = this;
      let wfInfo = this.wfInfo;
      let paramData = this.taskParam
      let a = configUtil.Vue.extend(configUtil.getWfObj('/wfModifyDialog.vue'));
      let instance = new a({
        i18n: that._i18n,
        propsData: {
          paramData: paramData,
          parentVue: that,
          visible: true,
          wfInfo: wfInfo
        }
      });
      this.$el.append(instance.$mount().$el)
    }

  }
}
