import { getUUID } from "../query-actions/util/lib";
import SelectApprovers from '../components-control/select-approvers.vue'
export default {
  execute: function (bill, param) {
    if (bill) {
      let taskId = bill.getGlobalParam('reviewParam').taskId;
      let selectedApprovers = []
      window.GMS.$http.get(`/bpm/bill/selectApprover/ApproverList/${taskId}`).then((res) => {
        if (res.data.code != 200) {
          this.executeApproval(bill, param)
          return
        }
        const nodeData = res.data.ApproverVo
        const optionList = [...nodeData.approvers]
        GMS.$hideContainer.$Modal.confirm({
          width: 700,
          render: (h) => {
            return h(
              SelectApprovers,
              {
                props: {
                  optionList,
                },
                on: {
                  'on-change': (val) => {
                    selectedApprovers = val
                  }
                }
              }
            )
          },
          onOk: () => {
            window.GMS.$http.post(
              '/bpm/bill/selectApprover/addApprovers',
              {
                ...nodeData,
                approvers: selectedApprovers,
              }
            ).then(() => {
              this.executeApproval(bill, param)
            }).catch(() => {
              GMS.$hideContainer.$Message.error('选择审批人发生错误')
            })
          },
          onCancel: () => {
          }
        });
      }).catch(() => {
        GMS.$hideContainer.$message.error('获取审批人列表失败');
      })
    } else {
      alert('缺少参数');
    }
  },
  executeApproval: function(bill, param) {
    if (param.param.isNeedSave) {
      let saveParam = {
        actions: "bill.Basic.save",
        afterAction: "",
        confirmTitle: "",
        customSaveAction: "",
        noCheckCardDepressPeriod: true,
        noCheckSubTable: true,
        viewName: ""
      }
      bill.executeServerAction(saveParam, function (code, data) {
        if (code === 200) {
          this.approval(bill, param);
          bill.setControlsSate(param["_control_name_"], "loading", false)
        } else {
          bill.setControlsSate(param["_control_name_"], "loading", false)
          GMS.$hideContainer.$message.error("操作异常请联系管理员");
        }
      }.bind(this));
    } else {
      this.approval(bill, param);
    }
  },
  approval: function (bill, param) {
    let billId = bill.getMasterData().getValue('id');
    let taskId = bill.getGlobalParam('reviewParam').taskId;
    let comments = bill.getGlobalParam('reviewParam').suggestions ? bill.getGlobalParam('reviewParam').suggestions : "";
    GMS.$http.post('/gms/workflow/task/completed/' + taskId + '?result=1&comments=' + comments).then(
      function (response) {
        if (response.status == 200 || response.statusText == 'OK') {
          // console.log('审批成功');
          bill.setControlsSate(param["_control_name_"], "loading", false)
          GMS.$hideContainer.$Message.success('审批成功！')
          
          if (
            window.osConfig.appOpenMode &&
            window.osConfig.appOpenMode == "apploader"
          ) {
            GMS.$hideContainer.$portalAPI.emit('nros-tag-close')
            GMS.$hideContainer.$portalAPI.emit(
              "open-new-page", //打开一个新的页签
              getUUID(),
              "runner-bill-form",
              {
                templateName: param.param.viewName,
                id: billId,
                state:'readOnly'
              },
              {
                openWay: "FUNCTAB",
                title: "单据详情",
              }
            );
          }
          else {
            GMS.$hideContainer.$router.push({
              path: "/showInnerBillForm/" + param.param.viewName + "/" + billId
            })
          }
        }
      }.bind(this)
    )
      .catch(
        function (error) {
          bill.setControlsSate(param["_control_name_"], "loading", false)
        }.bind(this)
      );
  }
}