/*
 * @Author: shi ZongLiang
 * @Date: 2021-06-25 14:19:32
 * @LastEditors: shi ZongLiang
 * @Description: 更改状态值
 */
export default {
  execute: function(bill, param) {
    debugger
    const masterName = bill.billdata.billTable.masterName;
    let msg;
    if (param.param.updatebillState == 2 && masterName == "CNSFK_H") {
      msg = "确认出纳当前信息？";
    } else if (param.param.updatebillState == 2 && masterName != "CNSFK_H") {
      msg = "确认提交当前信息？";
    } else if (param.param.updatebillState == 3) {
      msg = "确认同意当前信息？";
    } else if (param.param.updatebillState == 4) {
      msg = "确认驳回当前信息？";
    }
    GMS.$hideContainer.$Modal.confirm({
      title: "信息提示",
      content: msg,
      onOk: () => {
        if (param.param && param.param.budgetcontrol) {
          let res = this.getBudgetControl(bill, true, param);
          res.then((data) => {
            if (data.code == 0) {
              let result = this.getBudgetControl(bill, false, param);
              result.then((data1) => {
                if (data1.code == 0) {
                  this.updateBillState(bill, param);
                } else if (data1.code == 1) {
                  GAMS.Common.messagePrompt(data1.msg);
                }
              });
            } else if (data.code == 1) {
              setTimeout(() => {
                GAMS.Common.messagePrompt(data.msg);
              }, 500);
            } else if (data.code == 2) {
              setTimeout(() => {
                this.getModal(bill, param, data);
              }, 500);
            }
          });
        } else {
          this.updateBillState(bill, param);
        }
      },
      onCancel: () => {
        GMS.$hideContainer.$Message["error"]({
          content: "已取消",
        });
      },
    });
  },
  getModal(bill, param, data) {
    GMS.$hideContainer.$Modal.confirm({
      title: "信息提示",
      content: data.msg,
      onOk: () => {
        let result = this.getBudgetControl(bill, false, param);
        result.then((data1) => {
          if (data1.code == 0) {
            this.updateBillState(bill, param);
          } else if (data1.code == 1) {
            GAMS.Common.messagePrompt(data1.msg);
          }
        });
      },
      onCancel: () => {
        GMS.$hideContainer.$Message["error"]({
          content: "已取消",
        });
      },
    });
  },
  getBudgetControl(bill, check, param) {
    let postData = {
      data: bill.getBuillData(),
    };
    let url = `/v1/budget/control/${param.define}/${check}`;
    return new Promise((resolve, reject) => {
      GMS.$http
        .post(url, postData, {
          headers: {
            post: {
              "Content-Type": "application/json",
            },
          },
        })
        .then((res) => {
          resolve(res.data);
        });
    });
  },
  updateBillState(bill, param) {
    let _actions = param.param.actions;
    let define = param.define;
    let postData = {
      data: bill.getBuillData(),
    };
    let url = `/gms/bill/executed/${define}/${_actions}?transmission=total`;
    GMS.$http
      .post(url, postData, {
        headers: {
          post: {
            "Content-Type": "application/json",
          },
        },
      })
      .then((res) => {
        if (res.status == 200 && res.data.code == 0) {
          const masterName = bill.billdata.billTable.masterName;
          if (param.param.updatebillState == 2 && masterName == "CNSFK_H") {
            GMS.vbus.$Message.success("出纳成功");
          } else if (
            param.param.updatebillState == 2 &&
            masterName != "CNSFK_H"
          ) {
            GMS.vbus.$Message.success("提交成功");
          } else if (param.param.updatebillState == 3) {
            GMS.vbus.$Message.success("审批同意");
          } else if (param.param.updatebillState == 4) {
            GMS.vbus.$Message.success("驳回成功");
          }
          // 刷新页面
          GMS.vbus.$emit("custom-query-refresh");
          GMS.vbus.$emit("tab-count-refresh");
          GMS.vbus.$emit("submitSuccess");
        } else if (res.data.code == -1 && res.data.message) {
          let stringOne = res.data.message.split(":")[1];
          let stringResult = stringOne.split(";")[0];
          GMS.vbus.$Message.error(stringResult);
        } else {
          res.data.message && GMS.vbus.$Message.error(res.data.message);
        }
      });
  },
};
