import Utils from "~/utils";

export default {
  methods: {
		// 表单提交
    async handleSubmit(rulePath) {
      let result;
      if(rulePath){
        result = await this.executeRule(rulePath);

        if (result.respCode == SUCCESS && Object.keys(result.fieldErrors).length == 0) {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
          });
          this.$store.dispatch("TagsView/delView", this.$route)
          this.$router.history.push("/taskList", () => {
            if(rulePath == 'bopbut.sav'){
              this.$store.commit("setTaskListTabVal", 'bopsel')
            }
            else if(rulePath == 'cfabut.sav'){
              this.$store.commit("setTaskListTabVal", 'cfasel')
            }
            //修改待复核界面刷新状态为需要刷新
            this.$store.commit("setLoadingFreshReview", true)
          });
        }else  if(result.fieldErrors && Object.keys(result.fieldErrors).length > 0 ){
          const tab = this.showBackendErrors(result.fieldErrors)
          if (tab) {
            // tab切换之后,需出发tab-click的事件
            this.tabClick(tab);
          }
          this.$notify({
            title: "错误",
            message: "检核失败!",
            type: "error",
          });
        }
        else{
          this.$notify({
            title: "失败",
            message: "保存失败",
            type: "error",
          });
         
        }
      }else{
        result = await this.save();
        if (result.respCode == SUCCESS && Object.keys(result.fieldErrors).length == 0) {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
          });
          this.$store.dispatch("TagsView/delView", this.$route)
          this.$router.history.push("/taskList", () => {
            this.$store.commit("setTaskListTabVal", 'trnrel')
            //修改待复核界面刷新状态为需要刷新
            this.$store.commit("setLoadingFreshReview", true)
          });
        }else  if(result.fieldErrors && Object.keys(result.fieldErrors).length > 0 ){
          const tab = this.showBackendErrors(result.fieldErrors)
          if (tab) {
            // tab切换之后,需出发tab-click的事件
            this.tabClick(tab);
          }
          this.$notify({
            title: "错误",
            message: "检核失败!",
            type: "error",
          });
        }
        else{
          this.$notify({
            title: "失败",
            message: "保存失败",
            type: "error",
          });
         
        }
      }
      
    },

    async handleConfirm() {
      let result = await this.confirm();
      if (result.respCode == SUCCESS && Object.keys(result.fieldErrors).length == 0) {
        this.$notify({
          title: "成功",
          message: "保存成功",
          type: "success",
        });
        this.$store.dispatch("TagsView/delView", this.$route)
        this.$router.history.push("/taskList", () => {
          this.$store.commit("setTaskListTabVal", 'sptrel')
          //修改待复核界面刷新状态为需要刷新
          this.$store.commit("setLoadingFreshReview", true)
        });
      }else  if(result.fieldErrors && Object.keys(result.fieldErrors).length > 0 ){
        const tab = this.showBackendErrors(result.fieldErrors)
        if (tab) {
          // tab切换之后,需出发tab-click的事件
          this.tabClick(tab);
        }
        this.$notify({
          title: "错误",
          message: "检核失败!",
          type: "error",
        });
      }
      else{
        this.$notify({
          title: "失败",
          message: "确认失败",
          type: "error",
        });
       
      }
    },

		// 表单暂存
    async handleStash() {
      // 汇入汇款交易必须输入收款人信息才能暂存
      if(this.trnName == 'cptadv'){
        let extkey = this.model.cpdgrp.pye.pts.extkey;
        if(extkey.replace(/(^\s*)|(\s*$)/g, '').length == 0){
        this.$notify({
          title: "失败",
          message: "必须输入收款人信息才能暂存",
          type: "error",
        });
        return;
        }
      }
      let result = await this.pedding();
      if (result.respCode == SUCCESS) {
        this.$notify({
          title: "成功",
          message: "暂存成功",
          type: "success",
        });
        this.$store.dispatch("TagsView/delView", this.$route)
        this.$router.history.push("/taskList", () => {
          this.$store.commit("setTaskListTabVal", 'sptbrk')
          //修改草稿箱界面刷新状态为需要刷新
          this.$store.commit("setLoadingFreshPending", true)
        });
      } else {
        this.$notify({
          title: "失败",
          message: "暂存失败",
          type: "error",
        });
      }
    },
		// 表单校验
    async handleCheck(rulePath) {
      // this.$refs['modelForm'].validate((valid) => {
      //   console.log('valid', valid)
      // })
      let result;
      if(rulePath){
        result = await this.executeRule(rulePath);
      }else{
        result = await this.checkAll();
        
      }
      //  let result = await this.checkAll();
      if (result.respCode == SUCCESS) {
        const fieldErrors = result.fieldErrors;
        this.updateModel(result.data);
        if(fieldErrors && Object.keys(fieldErrors).length == 0){
            // 清除之前的校验状态
            this.getRoot().$refs.modelForm.clearValidate();
            this.$notify({
              title: "成功",
              message: "校验成功",
              type: "success",
            });
            return
        }
        const tab = this.showBackendErrors(fieldErrors)
        if (tab) {
          // tab切换之后,需出发tab-click的事件
          if (tab.name !== this.tabVal) {
            this.isChecking = true
            this.myTabClick ? this.myTabClick(tab) : this.tabClick(tab);
          }
          this.$notify({
            title: "错误",
            message: "校核失败",
            type: "error",
          });
          return
        }
      } else {
        this.$notify.error({ title: "错误", message: result.respMsg });
      }
    },
    async handlePass(data) {
      this.$store.state.Transaction.operateFuns[data.operateId]["pass"]()
    },
    async handleRefuse(data) {
      this.$store.state.Transaction.operateFuns[data.operateId]["refuse"]()
    },
  },
};