<template>
  <div>
<!--    <avue-form class="trading-form" :option="option" ref="form" v-model="form"> </avue-form>-->
    <div class="dialogButton" :style="{padding: batch ? '16px' : '4px'}">
      <el-button v-if="!batch" size="small" :loading="buttonLoading" @click="$emit('gotoRecord', false)">上一笔</el-button>
      <el-button v-if="!batch" size="small" :loading="buttonLoading" @click="$emit('gotoRecord', true)">下一笔</el-button>

      <el-button v-if="!batch" size="small" :loading="buttonLoading" @click="$emit('choceCheckFun', false)">关闭</el-button>
      <el-button type="primary" :loading="buttonLoading" size="small" @click="approved(1)">审批通过</el-button>
      <el-button type="warning" :loading="buttonLoading" size="small" @click="submit(2)">审批驳回</el-button>

      <el-input
        style="width: 16em;margin-left: 1em;margin-right: 1em;"
        v-model="form.auditMsg"
        size="small"
        autocomplete="off"
        clearable
        placeholder="审批意见"
      >
      </el-input>

      <el-button v-if="batch" size="small" :loading="buttonLoading" @click="$emit('choceCheckFun', false)">关闭</el-button>
    </div>
    <mail-component ref="mail" @choceCheckFun="$emit('choceCheckFun', true)"></mail-component>
  </div>
</template>

<script>
import { approvePass, batchOperation } from "@/api/approveDataH/main";
import { offset } from "@/api/basicData/salesOrder";
import mailComponent from "./component/mail-component.vue";
export default {
  components: {
    mailComponent
  },
  name: "check",
  props: {
    batch:{
      type: Boolean,
      deburred: true,
    },
    checkData: {
      type: Object
    },
    choceCheckFun: {
      type: Function
    },
    gotoRecord: {
      type: Function
    },
    checkDetail: {
      type: Boolean
    },
    idList: {
      type: Array
    },
    objMail: {
      type: Object
    },
  },
  data() {
    return {
      form: {},
      list: [],
      currencyDic: [],
      buttonLoading: false,
      corpId: "",
      option: {
        emptyBtn: false,
        submitBtn: false,
        labelWidth: 120,
        menuSpan: 8,
        column: [
          {
            label: "审批人",
            prop: "auditName",
            span: 24,
            disabled: true,
            rules: [
              {
                required: false,
                message: " ",
                trigger: "blur"
              }
            ]
          },
          {
            label: "审批日期",
            prop: "auditOpTime",
            type: "datetime",
            span: 24,
            disabled: true,
            rules: [
              {
                required: false,
                message: " ",
                trigger: "blur"
              }
            ]
          },
          {
            label: "审批意见",
            prop: "auditMsg",
            type: "textarea",
            rows: 5,
            span: 24,
            rules: [
              {
                required: false,
                message: " ",
                trigger: "blur"
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    let date = new Date();
    let strDate =
      date.getFullYear() +
      "-" +
      (date.getMonth() + 1) +
      "-" +
      date.getDate() +
      " " +
      date.getHours() +
      ":" +
      date.getMinutes() +
      ":" +
      date.getSeconds();
    this.$set(this.form, "auditOpTime", strDate);
    this.$set(this.form, "auditName", this.$store.getters.userInfo.user_name);
  },
  methods: {
    approved(operate) {
      if (this.checkData.checkType == "FFSQ") {
        this.$refs.mail.openDialog(this.checkData,this.form,this.objMail)
      } else {
        this.submit(operate);
      }
    },
    submit(operate) {
      // this.$confirm("是否确认此操作!", "提示", {
      //   confirmButtonText: "确认",
      //   cancelButtonText: "取消",
      //   type: "warning",
      // }).then(() => {
      let cacheRow = this.checkData
      // console.log('cacheRow', cacheRow);
      // this.$emit("choceCheckFun", false, cacheRow);
      //
      // return

      if(operate===1 && !this.form.auditMsg){
        this.form.auditMsg = '同意！'
      }

      this.checkData.operate = operate;
      if (this.idList.length !== 0) {
        //批量
        this.buttonLoading = true;
        batchOperation(this.idList.join(","), operate, this.form.auditMsg)
          .then(res => {
            if (res.data.success) {
              this.$message.success("操作成功!");
              this.$emit("operationType");
            }
          })
          .finally(() => {
            this.buttonLoading = false;
          });
      } else {
        //单个
        this.buttonLoading = true;
        this.checkData.auditName = this.form.auditName;
        this.checkData.auditMsg = this.form.auditMsg;
        this.checkData.auditOpTime = this.form.auditOpTime;
        if (this.checkData.checkType === "YPJ-XSJH" && operate === 1) {
          offset({ id: this.checkData.billId })
            .then(res => {
              if (res.data.data == "操作成功") {
                this.checkData = this.detailData.check;
                this.checkDialog = true;
              } else {
                this.$confirm(res.data.data, {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                })
                  .then(() => {
                    approvePass(this.checkData)
                      .then(res => {
                        if (res.data.success) {
                          this.$message.success("操作成功!");
                          this.$emit("choceCheckFun", false, cacheRow);
                        }
                        if (!this.checkDetail) {
                          //关闭付费申请页面
                          let tag = this.$store.getters.tagList.filter(ele => ele.label === "付费申请")[0];
                          this.$store.commit("DEL_TAG", tag);
                          this.$store.commit("PQ_OUT_DETAIL");
                          let tag1 = this.$store.getters.tagList.filter(ele => ele.label === "审批数据")[0];
                          this.$store.commit("DEL_TAG", tag1);
                          this.$router.push({
                            path: "/approveDataH/index",
                          });
                        }
                      })
                      .finally(() => {
                        this.buttonLoading = false;
                      });
                  })
                  .catch(() => {
                    this.buttonLoading = false;
                  });
              }
            })
            .catch(() => {
              this.buttonLoading = false;
            });
        } else {
          approvePass(this.checkData)
            .then(res => {
              if (res.data.success) {
                this.$message.success("操作成功!");
                this.$emit("choceCheckFun", false, cacheRow);
              }
              if (!this.checkDetail) {
                //关闭付费申请页面
                let tag = this.$store.getters.tagList.filter(ele => ele.label === "付费申请")[0];
                this.$store.commit("DEL_TAG", tag);
                this.$store.commit("PQ_OUT_DETAIL");
                let tag1 = this.$store.getters.tagList.filter(ele => ele.label === "审批数据")[0];
                this.$store.commit("DEL_TAG", tag1);
                this.$router.push({
                  path: "/approveDataH/index"
                });
              }
            })
            .finally(() => {
              this.buttonLoading = false;
            });
        }
      }
      // })
    }
  }
};
</script>

<style lang="scss" scoped>
.trading-form ::v-deep .el-form-item {
  margin-bottom: 8px !important;
}
</style>
