<template>
  <div>
    <avue-form class="trading-form" :option="option" ref="form" v-model="form">
    </avue-form>
    <div class="dialogButton">
      <el-button size="small" :loading="buttonLoading" @click="$emit('choceCheckFun')">关闭</el-button>
      <el-button type="warning" :loading="buttonLoading" size="small" @click="submit(2)">审批驳回</el-button>
      <el-button type="primary" :loading="buttonLoading" size="small" @click="submit(1)">审批通过</el-button>
    </div>
  </div>
</template>

<script>
  import { approvePass,batchOperation } from "@/api/approveDataL/main";
  import {offset} from "@/api/basicData/salesOrder";

  export default {
    name: "check",
    props: {
      checkData: {
        type: Object
      },
      choceCheckFun:{
        type: Function
      },
      checkDetail:{
        type: Boolean
      },
      idList:{
        type: Array
      }
    },
    data() {
      return {
        form: {},
        list: [],
        currencyDic: [],
        buttonLoading:false,
        corpId: "",
        option: {
          emptyBtn: false,
          submitBtn: false,
          labelWidth: 120,
          menuSpan: 8,
          column: [
            {
              label: '审批人',
              prop: 'auditName',
              span: 12,
              disabled:true,
              rules: [
                {
                  required: false,
                  message: ' ',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '审批日期',
              prop: 'auditOpTime',
              type: "datetime",
              span: 12,
              disabled:true,
              rules: [
                {
                  required: false,
                  message: ' ',
                  trigger: 'blur'
                }
              ]
            },
            {
              label: '审批意见',
              prop: 'auditMsg',
              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: {
      submit(operate){
        this.$confirm("是否确认此操作!", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
            console.log(operate)
          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")
                      }
                      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: "/approveData/index",
                          query: {check:'refresh'},
                        });
                      }
                    }).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")
                }
                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: "/approveData/index",
                    query: {check:'refresh'},
                  });
                }
              }).finally(()=>{
                this.buttonLoading = false
              })
            }
          }
        })
      }
    }
  }
</script>

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