let mixin = {};
import settingConfig from "@/settings";

function getTarget(t) {
  return t.parentVue;
}

mixin = {
  name: "wfStartDialog",
  props: {
    dataId: [String, Number],
    code: [String, Number],
    serviceId: String,
    startParam: Object,
    visible: Boolean,
    parentVue: Object,
    wfName: String,
  },
  data() {
    return {
      showDialog: false,
      wfStartForm: {},
      wfDefItems: [],
    };
  },
  computed: {
    current_prefix() {
      return this.serviceId ? "/" + this.serviceId : "";
    },
  },
  async mounted() {
    await this.initWfInfo(this.code);
    // this.showDialog = true;
  },
  methods: {
    async initWfInfo(code) {
      let that = getTarget(this);
      var wfDefItems = null;
      await that.$http({
        url: this.current_prefix + "/wf_obj_config_item/list",
        data: {
          objTypeCode: code,
          enabled: true
        },
        method: "post",
        //contentType: 'application/json;charset=utf-8',
        async: false,
        isLoading: true,
        error: (error) => {
          this.close();
        },
        success: (resultMsg) => {
          wfDefItems = resultMsg.objx || [];
        },
      });

      if (!wfDefItems || wfDefItems.length == 0) {
        that.$baseAlert(this.$t2('无法启动，没有流程单据', 'components.wf.startWarnMsg1'));
        this.close();
        return false;
      }

      (this.wfStartForm = {
        wfCode: code,
      }),
        (this.wfDefItems = wfDefItems);
      this.$set(this.wfStartForm, "modelId", wfDefItems[0].modelId);
      if (wfDefItems.length == 1) {
        if (settingConfig.withoutConfrimByOneWf === true) {
          this.subWfStartForm();
        } else {
          let wfStartOperateName =
            settingConfig.wfStartOperateName ||
            settingConfig.wfStartButtonName ||
            "启动流程";
          let startTip = "您确定要" + wfStartOperateName + "吗？";
          that.$baseConfirm(this.$t2(startTip, 'components.wf.startTip'))
            .then(() => {
              this.subWfStartForm();
            }).catch(error => {
            this.close();
          });
        }
      } else {
        this.showDialog = true;
      }
    },
    subWfStartForm() {
      let that = getTarget(this);
      var modelId = this.wfStartForm.modelId;
      if (!modelId) {
        that.$baseAlert(this.$t2('请选择流程模版', 'components.wf.startWarnMsg2'));
        this.close();
        return false;
      }
      this.showDialog = false;

      var objId = this.dataId;

      let startParam = this.startParam || {};

      var data = JSON.stringify({
        modelId: modelId,
        objTypeCode: this.code,
        objId: objId,
        formData: startParam,
        // name:this.wfName
      });
      that.$http({
        url: this.current_prefix + "/wf/start",
        data: data,
        method: "post",
        headers: {
          "Content-Type": "application/json;charset=utf-8",
        },
        isLoading: true,
        success: (resultMsg) => {
          that.$message({
            message: resultMsg.content,
            type: "success",
            duration: 500,
            onClose: (t) => {
              that.$baseReload();
              if (resultMsg.objx) {
                that.$baseAlert(resultMsg.objx)
              }
            },
          });
        },
      });
      this.close();
    },
    close() {
      this.$destroy();
      this.$el.parentElement.removeChild(this.$el);
    },
  },
};

export default mixin;
