<template>
  <div class="sale-contract-add">
    <el-steps :active="curStep" simple style="width: 50%; margin: 0 auto;">
      <el-step title="基础内容" />
      <el-step title="合同条款" />
    </el-steps>

    <template v-if="curStep === 1">
      <Basic
        ref="basicRef"
        :details-info="baseInfo"
        :disabled="$route.query.type === 'see' ? true : false"
        @exportType="setExportType"
      />
      <GoodsInfo
        ref="goodsInfoRef"
        :export-type="exportType"
        :details-info="goodsInfo"
        :enterprise-type="enterpriseType"
        :disabled="$route.query.type === 'see' ? true : false"
      />
    </template>
    <template v-if="curStep === 2">
      <ContractTerms
        ref="contractTermRef"
        :details-info="termsInfo"
        :disabled="$route.query.type === 'see' ? true : false"
      />
    </template>

    <div class="submit-btn">
      <el-button
        @click="
          () => {
            $router.go(-1)
          }
        "
      >取消</el-button>
      <el-button v-if="curStep === 2" type="primary" @click="handlePreStep">上一步</el-button>
      <el-button type="primary" @click="save('temp')">保存</el-button>
      <el-button v-if="curStep === 1" type="primary" @click="handleNextStep">下一步</el-button>
      <el-button v-if="curStep === 2" type="primary" @click="submitClick">提交</el-button>
    </div>

    <contract-dialog
      :visible="showContract"
      :contract-id="salesContractId"
      @close="showContract = false"
    />
  </div>
</template>

<script>
import {
  saveSalesContract,
  saveSalesContractTerms,
  getSalesContractDetail
} from '@/api/myTrade/salesContract'
// import { exportFile } from '@/utils'

export default {
  components: {
    Basic: () => import('./components/Basic.vue'),
    GoodsInfo: () => import('./components/GoodsInfo.vue'),
    ContractTerms: () => import('./components/Terms.vue'),
    ContractDialog: () => import('./components/contractDialog.vue')
  },
  data() {
    return {
      curStep: 1,
      exportType: '',
      enterpriseType: '',
      firstStepTempId: '',
      baseInfo: {},
      goodsInfo: [],
      termsInfo: {},
      showContract: false,
      salesContractId: ''
    }
  },
  watch: {
    $route: {
      handler(val) {
        if (val.query.id) {
          this.querySaleContractDetail(val.query.id)
        }
      },
      immediate: true
    }
  },
  methods: {
    submitClick() {
      this.getSecondStep('submit')
    },
    setExportType(type, enterpriseType) {
      this.exportType = type
      this.enterpriseType = enterpriseType
    },
    save(type) {
      if (this.curStep === 1) {
        this.getFirstStep(type)
      } else {
        this.getSecondStep(type)
      }
    },
    async getFirstStep(type) {
      const baseInfo = await this.$refs.basicRef.getParams(type)
      const goodsInfo = await this.$refs.goodsInfoRef.getParams(type)
      const params = {
        Loading: true,
        salesContract: baseInfo,
        salesCommoditys: goodsInfo
      }
      if (type === 'temp') {
        params.salesContract.submitStatus = 1
      } else {
        params.salesContract.submitStatus = 3
      }

      if (params.salesContract.templateType === 'Y') {
        params.salesContract.templateType = true
      } else {
        params.salesContract.templateType = false
      }

      if (this.$route.query.id) {
        params.salesContract.id = this.$route.query.id // 编辑主键id
      }

      const res = await saveSalesContract(params)

      if (res.success) {
        this.$message.success('保存成功')
        if (params.salesContract.submitStatus === 3) {
          this.curStep = 2
          this.firstStepTempId = res.data
        } else {
          this.$router.go(-1)
        }
      }
    },
    async getSecondStep(type) {
      const contractTerms = await this.$refs.contractTermRef.getParams(type)
      const params = contractTerms
      params.Loading = true

      if (type === 'temp') {
        params.submitStatus = 1
      } else {
        params.submitStatus = 2
      }

      if (this.$route.query.id) {
        params.id = this.$route.query.id // 编辑主键id
      }

      if (this.firstStepTempId) {
        params.contractId = this.firstStepTempId // 第一步保存 返回id
      }

      const res = await saveSalesContractTerms(params)

      if (type === 'temp') {
        this.$message.success('保存成功')
      } else {
        if (res.data) {
          this.salesContractId = this.firstStepTempId
          this.showContract = true
        } else {
          this.$router.go(-1)
        }
      }
    },
    handlePreStep() {
      this.curStep = 1
    },
    handleNextStep() {
      this.getFirstStep('submit')
    },
    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.$forceUpdate()
      }
    }
  }
}
</script>
