<template>
  <div class="sale-contract-add">
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="基础内容" name="base">
        <Basic
          ref="basicRef"
          :details-info="baseInfo"
          :disabled="$route.query.type === 'see' ? true : false"
          @exportType="setExportType"
        />
        <GoodsInfo
          ref="goodsInfoRef"
          :export-type="exportType"
          :enterprise-type="enterpriseType"
          :details-info="goodsInfo"
          :disabled="$route.query.type === 'see' ? true : false"
        />
        <OtherInfo
          ref="otherInfoRef"
          :details-info="otherInfo"
          :disabled="$route.query.type === 'see' ? true : false"
        />
      </el-tab-pane>
      <el-tab-pane label="合同条款" name="term">
        <ContractTerms
          ref="contractTermRef"
          :details-info="termsInfo"
          :disabled="$route.query.type === 'see' ? true : false"
        />
      </el-tab-pane>
    </el-tabs>

    <div class="submit-btn">
      <el-button v-if="showDelButton" type="danger" @click="handleDel">删除</el-button>
      <el-button
        @click="
          () => {
            $router.go(-1)
          }
        "
      >返回</el-button>
    </div>
  </div>
</template>

<script>
import { getSalesContractDetail, deleteSaleContractFile } from '@/api/myTrade/salesContract'

export default {
  components: {
    Basic: () => import('./components/Basic.vue'),
    GoodsInfo: () => import('./components/GoodsInfo.vue'),
    ContractTerms: () => import('./components/Terms.vue'),
    OtherInfo: () => import('./components/OtherInfo.vue')
  },
  data() {
    return {
      activeName: 'base',
      curStep: 1,
      showDelButton: false,
      exportType: '',
      enterpriseType: '',
      baseInfo: {},
      goodsInfo: [],
      otherInfo: {},
      termsInfo: {}
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.querySaleContractDetail(this.$route.query.id)
    }
  },
  methods: {
    setExportType(type, enterpriseType) {
      this.exportType = type
      this.enterpriseType = enterpriseType
    },
    async querySaleContractDetail(id) {
      const res = await getSalesContractDetail({ contractId: id })

      if (res.success) {
        this.baseInfo = res.data.salesContract
        this.baseInfo.contractFile = res.data.contractFile

        this.goodsInfo = res.data.salesCommoditys

        this.termsInfo = res.data.contractTermsDto

        this.otherInfo = {
          createBy: res.data.salesContract?.createBy,
          updateBy: res.data.salesContract?.updateBy,
          gmtCreate: res.data.salesContract?.gmtCreate,
          gmtModified: res.data.salesContract?.gmtModified
        }

        // 草稿和审核不通过的合同可以删除
        if (res.data.salesContract.submitStatus === '1' || ['4', '5'].includes(res.data.salesContract.auditStatus)) {
          this.showDelButton = true
        }
      }
    },
    handleDel() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteSaleContractFile({ contractId: this.$route.query.id })

        if (res.success) {
          this.$message.success('删除成功')
          this.$router.go(-1)
        }
      }).catch(() => {})
    }
  }
}
</script>
