<template>
  <div class="apply-invoicing">
    <apply-invoicing-content ref="applyInvoicingContent" :deatils-info="deatilsInfo" :disabled="disabled" />
    <div class="submit-btn">
      <el-button @click="() => { $router.push({ name: 'mySettlement', query: {type: 'InvoicesManagement'} })}">关闭</el-button>
      <el-button v-if="!disabled" type="primary" @click="onSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import { saleInvoiceDoAdd, saleInvoiceGetDetails, saleInvoiceDoUpdate } from '@/api/mySettlement/invoicesManagement'
export default {
  name: 'MySettlementInvoicesManagementApplyInvoicing',
  components: {
    ApplyInvoicingContent: () => import('./components/ApplyInvoicingContent')
  },
  data() {
    return {
      deatilsInfo: {},
      disabled: false
    }
  },
  created() {
    const { id, type } = this.$route.query
    if (id) {
      this.saleInvoiceGetDetails()
    }
    if (type === 'see') {
      this.disabled = true
    }
  },
  methods: {
    saleInvoiceGetDetails() {
      const { id } = this.$route.query
      saleInvoiceGetDetails({ id }).then(data => {
        this.deatilsInfo = data.data
      })
    },
    onSubmit() {
      this.$refs.applyInvoicingContent.getParams().then((data) => {
        const params = {
          Loading: true,
          ...data
        }
        const { id } = this.$route.query
        if (id) {
          saleInvoiceDoUpdate(params).then(() => {
            this.$message.success('修改开票成功')
            this.$router.push({
              name: 'mySettlement',
              query: {
                type: 'InvoicesManagement'
              }
            })
          })
        } else {
          saleInvoiceDoAdd(params).then(() => {
            this.$message.success('申请开票成功')
            this.$router.push({
              name: 'mySettlement',
              query: {
                type: 'InvoicesManagement'
              }
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.apply-invoicing {

}
</style>
