<template>
  <div class="customer-disburse-edit">
    <edit-content ref="editCont" :details-info="detailsInfo"></edit-content>
    <div class="submit-btn">
      <el-button style="width: 150px;" @click="() => { $router.go(-1) }">取消</el-button>
      <el-button type="primary" style="width: 150px;" @click="onSubmit('temp')">暂存</el-button>
      <el-button type="primary" style="width: 150px;" @click="onSubmit('submit')">提交</el-button>
    </div>
  </div>
</template>

<script>
import { getcustomerDisburseInfoEditDetails, customerDisburseInfoSubmit } from '@/api/capitalManage/customerDisburseInfo/index.js'
import EditContent from './components/EditContent.vue'

export default {
  components: {
    EditContent
  },
  props: {},
  data() {
    return {
      detailsInfo: {
        downstreamDetailList: [],
        upstreamDetailList: [],
        loanDateList: []
      }
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id } = this.$route.query
      if (id) {
        getcustomerDisburseInfoEditDetails({ id }).then(({ data }) => {
          this.detailsInfo = data
        })
      }
    },
    // 提交
    onSubmit(type) {
      this.$refs.editCont.getParams().then((params) => {
        const { upstreamDetailList, downstreamDetailList, attachmentFileIds } = params
        if (type === 'submit') {
          for (let i = 0; i < upstreamDetailList.length; i++) {
            // upstreamInvoiceFileIds
            const { upstreamPurchContractFileIds, upstreamSettlementFileIds } = upstreamDetailList[i]
            if (!upstreamPurchContractFileIds.length) {
              this.$message.warning('请上传上游采购合同后提交')
              return
            }
            if (!upstreamSettlementFileIds.length) {
              this.$message.warning('请上传上游结算单后提交')
              return
            }
            // if(!upstreamInvoiceFileIds.length) {
            //   this.$message.warning('请上传上游发票后提交')
            //   return
            // }
          }

          for (let j = 0; j < downstreamDetailList.length; j++) {
            // downstreamInvoiceFileIds
            const { downstreamSalesContractFileIds, downstreamSettlementFileIds } = downstreamDetailList[j]
            if (!downstreamSalesContractFileIds.length) {
              this.$message.warning('请上传下游销售合同后提交')
              return
            }
            if (!downstreamSettlementFileIds.length) {
              this.$message.warning('请上传下游结算单后提交')
              return
            }
            // if(!downstreamInvoiceFileIds.length) {
            //   this.$message.warning('请上传下游发票后提交')
            //   return
            // }
          }

          if (!attachmentFileIds.length) {
            this.$message.warning('请上传合同及附件后提交')
            return
          }
        }
        params.submit = type === 'submit' ? 1 : 0
        params.Loading = true
        customerDisburseInfoSubmit(params).then(() => {
          if (type === 'submit') {
            this.$message.success('客户支用信息编辑成功')
            this.$router.push({ path: '/capitalManage/customerDisburseInfo' })
          } else {
            this.$message.success('客户支用信息暂存成功')
            this.$router.push({ path: '/capitalManage/customerDisburseInfo' })
          }
        })
      })
    }
  }
}
</script>
<style scoped lang="scss"></style>
