export default {
  data() {
    return {
      showStartBtn: false,
      hasWf: false,
      wfInfo: {},
      // currentTask: {},
      taskParam: {},
      wfOption: {},
      showModifyDialog: false,
      modifyDialogOption: {},

      rejectNodes: [],
      rejectDialogVisible: false,
      rejectForm: {
        rejectSubmitModel: null,
        nodeId: null,
        opinion: null,
      },

      formLabelWidth: "120px",
      agreeDialogVisible: false,
      agreeForm: {
        opinion: this.$t1('同意'),
        nodeId: null,
      },
      agreeNodes: [],

      changeWfDialogVisible: false,
      changeWfForm: {},
      changeUserRows: [],
      showUserDialog: false,
      wfUserParamForTransfer: {},//转办

    }
  },
  methods: {
    initCurrentPrefix() {
      let url = this.billJson.getUrl;
      let urlArr = url.split('/');
      let serviceId = urlArr[1]
      this.current_prefix = serviceId ? "/" + serviceId : "";
    },
    async initWf(option) {
      this.wfOption = option;
      let hasWf = false;
      let wfInfo, currentTask, taskParam = {};
      await this.initWfInfo(option).then(res => {
        if (res && res.type == 'success') {
          if (res.objx && res.objx.procInstId) {
            hasWf = true;
            wfInfo = res.objx;
            this.initCurrentPrefix();
            this.initWfUserParam();
          }
        }
      });

      if (hasWf) {
        if (wfInfo.taskId) {
          await this.getTaskParams(option, wfInfo.taskId).then(res2 => {
            if (res2.type == 'success') {
              taskParam = res2.objx;
              taskParam.objId = option.objId;
            }
          });
        }
      }
      this.hasWf = hasWf;
      this.wfInfo = wfInfo || {};
      this.taskParam = taskParam;
    },
    async initWfInfo(option) {
      let id = option.objId;
      let code = option.wfCode;
      return this.$http({
        url: this.getCurrentPrefix(option.serviceId) + '/wf/getWfInfo',
        method: `post`,
        data: {
          objTypeCode: code,
          objId: id
        },
        isLoading: true,
        modalStrictly: true
      });
    },
    async getTaskParams(option, taskId) {
      return this.$http({
        url: this.getCurrentPrefix(option.serviceId) + '/wf/getTaskVariables',
        method: `post`,
        data: {
          stringOne: taskId
        },
        isLoading: true,
        modalStrictly: true
      });
    },
    getCurrentPrefix(serviceId) {
      let currentPrefix;
      if (serviceId) {
        currentPrefix = serviceId.split('/')[0] == '/' ? serviceId : ('/' + serviceId);
      } else {
        currentPrefix = '';
      }
      return currentPrefix;
    },
    openModifyDialog() {

      this.showModifyDialog = true;
      this.modifyDialogOption = {
        code: this.taskParam.appBillCode,
        dataId: this.dataId
      };

      /*let wfOption = this.wfOption;
      this.$refs.modifyDialog.init({
        // code: wfOption.code,
        code: "test003_md",
        dataId: this.dataId
      })*/
    },

    async rejectWfTask(operateType) {
      let that = this.getTarget(this);
      var procInstId = this.wfInfo.procInstId;
      var taskId = this.wfInfo.taskId;
      var destinations = [];

      await that.$http({
        url: this.current_prefix + "/wf/getPreIncomeNodes",
        data: {
          stringOne: taskId,
        },
        method: "post",
        //contentType: 'application/json;charset=utf-8',
        async: false,
        isLoading: true,
        success: (resultMsg) => {
          destinations = (resultMsg.objx || []).reverse();
        },
      });

      if (destinations == null) {
        return false;
      }
      if (destinations.length == 0) {
        that.$message({
          message: this.$t1('无法驳回，没有目标节点'),
          type: "error",
          duration: 2 * 1000,
        });
        return false;
      }
      this.operateType = operateType;

      this.rejectForm.rejectSubmitModel = this.wfInfo.toRejectNode == false ? 2 : null
      this.rejectForm.nodeId = destinations[0].nodeId;
      this.rejectForm.opinion = "";
      this.rejectNodes = destinations;
      this.rejectDialogVisible = true;
    },
    subRejectForm() {
      let that = this.getTarget(this);
      var procInstId = this.wfInfo.procInstId;
      var taskId = this.wfInfo.taskId;

      if (!this.rejectForm.nodeId) {
        that.$message({
          message: this.$t1('请选择驳回节点'),
          type: "error",
          duration: 2 * 1000,
        });
        return false;
      }

      if (!this.rejectForm.opinion) {
        that.$message({
          message: this.$t1('请输入驳回意见'),
          type: "error",
          duration: 2 * 1000,
        });
        return false;
      }

      var data = JSON.stringify({
        taskId: taskId,
        uuid: this.wfInfo.uuid,
        nodeId: this.rejectForm.nodeId,
        suggestion: this.rejectForm.opinion,
        rejectSubmitModel: this.rejectForm.rejectSubmitModel
      });
      this.rejectDialogVisible = false;
      that.$http({
        url: this.current_prefix + "/wf/reject",
        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) => {
              this.reloadContent();
              // this.closeWinOnCheck(2);
            },
          });
        },
      });
    },
    openAgreeDialog(operateType) {
      this.operateType = operateType;
      let that = this.getTarget(this);
      this.agreeForm = {
        opinion: this.$t1('同意'),
        nodeId: null,
      };
      this.agreeNodes = [];
      that.$http({
        url: this.current_prefix + "/wf/getNextOutgoingNodes",
        data: {
          stringOne: this.wfInfo.taskId,
        },
        method: "post",
        async: false,
        isLoading: true,
        success: (res) => {
          let nodes = res.objx || [];
          /*if (tq == true) {
            nodes.reverse();
          }*/
          if (nodes.length) {
            this.agreeForm.nodeId = nodes[0].nodeId;
          }
          this.agreeNodes = nodes;
          this.agreeDialogVisible = true;
        },
      });
    },
    subAgreeForm() {

      let done = () => {
        this.subAgreeHandle();
      }
      /*
      let wfConfig = this.wfConfig;
      if (wfConfig.onBeforeAgree) {
        wfConfig.onBeforeAgree(done)
      } else {
        done();
      }*/
      done();
    },
    subAgreeHandle() {
      let that = this.getTarget(this);
      let nodeId = null;
      if (this.agreeNodes.length) {
        if (!this.agreeForm.nodeId) {
          that.$message({
            message: this.$t1('请选择节点'),
            type: "error",
            duration: 2 * 1000,
          });
          return;
        }
        nodeId = this.agreeForm.nodeId;
      }

      var procInstId = this.wfInfo.procInstId;
      var taskId = this.wfInfo.taskId;
      var memo = this.agreeForm.opinion || this.$t1('同意');
      var data = JSON.stringify({
        taskId: taskId,
        uuid: this.wfInfo.uuid,
        suggestion: memo,
        nodeId: nodeId,
      });
      this.agreeDialogVisible = false;
      that.$http({
        url: this.current_prefix + "/wf/submit",
        data: data,
        method: "post",
        headers: {
          "Content-Type": "application/json;charset=utf-8",
        },
        isLoading: true,
        success: (resultMsg) => {
          if (resultMsg.objx && resultMsg.objx.code == "999999") {
            this.errorSubmit({
              taskId: taskId,
              uuid: this.wfInfo.uuid,
              suggestion: memo,
              errorMsg: resultMsg.objx.errorMsg
            });
          } else {
            that.$message({
              message: resultMsg.content,
              type: "success",
              duration: 500,
              onClose: (t) => {
                this.reloadContent();
                // this.closeWinOnCheck(1);
              },
            });
          }
        },
      });
    },
    errorSubmit(data) {
      let that = this.getTarget(this);
      that.$http({
        url: this.current_prefix + "/wf/errorSubmit",
        data: data,
        method: "post",
        isLoading: true,
        success: (resultMsg) => {
          that.$message({
            message: resultMsg.content,
            type: "success",
            duration: 500,
            onClose: (t) => {
              this.reloadContent();
              // this.closeWinOnCheck(1);
            },
          });
        },
      });
    },
    openChangeWfForm(operateType) {
      this.operateType = operateType;
      this.changeWfDialogVisible = true;
      this.changeUserRows = [];
      this.changeWfForm = {
        userIds: [],
        userNickNames: null,
        opinion: null,
      };
    },
    subChangeWfForm() {
      let that = this.getTarget(this);
      var procInstId = this.wfInfo.procInstId;
      var taskId = this.wfInfo.taskId;

      var userIds = this.changeWfForm.userIds || [];
      var opinion = this.changeWfForm.opinion || null;

      if (!userIds.length) {
        that.$message({
          message: this.$t1('请选择转办人'),
          type: "error",
          duration: 2000,
          onClose: (t) => {
          },
        });
        return false;
      }

      var data = JSON.stringify({
        taskId: taskId,
        uuid: this.wfInfo.uuid,
        transferTo: userIds,
        suggestion: opinion,
      });
      this.changeWfDialogVisible = false;
      that.$http({
        url: this.current_prefix + "/wf/transfer",
        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) => {
              this.reloadContent();
              // this.closeWinOnCheck(4);
            },
          });
        },
      });
    },
    userConfirm(rows) {
      if (rows.length > 0) {
        let changeUserRows = this.changeUserRows;
        let ids = changeUserRows.map((changeUserRow) => changeUserRow.id);
        let addRows = rows.filter((row) => !ids.includes(row.id));
        changeUserRows.push(...addRows);

        let row = rows[0];
        this.changeWfForm.userIds = changeUserRows.map(
          (changeUserRow) => changeUserRow.id
        );
        this.changeWfForm.userNickNames = changeUserRows
          .map((changeUserRow) => changeUserRow.nickName)
          .join(",");
      }
    },
    initWfUserParam(callback) {
      this.initTransferUserParam();//转办
    },
    //
    initTransferUserParam(callback) {
      //初始化转办用户参数
      let that = this.getTarget(this);
      return that.$http({
        url: USER_PREFIX + "/wf_diy_attribute/getValue",
        method: `post`,
        data: {attributeKey: "param1"},
        isLoading: true,
        modalStrictly: true,
        success: (res) => {
          this.wfUserParamForTransfer = res.objx || {};
          callback && callback();
        },
      });
    },
    getTarget() {
      return this;
    },
    reloadContent() {
      this.$emit('reload');
    }

  }
}
