<template>
  <div class="purchase-contract-add">

    <!-- 基础信息 -->
    <cy-info-title> 基础信息 </cy-info-title>
    <basic-info ref="basicInfo" :info="detail" />

    <!-- 合同信息 -->
    <cy-info-title style="margin-top: 20px;"> 合同信息 </cy-info-title>
    <contract-info ref="contractInfo" :info="detail" />

    <!-- 质保金 -->
    <cy-info-title style="margin-top: 20px;"> 质保金 </cy-info-title>
    <retention-money ref="retentionInfo" :info="detail" />

    <!-- 价格规则 -->
    <cy-info-title style="margin-top: 20px;"> 价格规则 </cy-info-title>
    <price-rules ref="priceRule" :info="detail" />

    <!-- 结算与付款 -->
    <cy-info-title style="margin-top: 20px;"> 结算与付款 </cy-info-title>
    <settle-payment ref="settlePay" :info="detail" />

    <!-- 供应商发货人 -->
    <personnel-information ref="personInfo" :info="detail" style="margin-top: 20px;" />

    <!-- 货物信息 -->
    <cargo-information style="margin-top: 20px;" :info="detail" ref="goodsList" @validateEmpty="validateEmpty" :canChoooseGoods="canChoooseGoods" />

    <div class="submit-btn">
      <el-button @click="$router.go(-1)">取消</el-button>
      <el-button type="primary" @click="save(0)">暂存</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </div>
  </div>
</template>

<script>
import { getDeatail, submitInfo } from '@/api/myTrade/purchaseContractManagement/purchaseContractManagement'
export default {
  name: 'PurchaseContractAdd',
  components: {
    BasicInfo: () => import('./components/ContractBaseInfo'),
    ContractInfo: () => import('./components/ContractInfo'),
    RetentionMoney: () => import('./components/RetentionMoney'),
    PriceRules: () => import('./components/PriceRules'),
    SettlePayment: () => import('./components/SettlePayment'),
    PersonnelInformation: () => import('./components/PersonnelInformation.vue'),
    CargoInformation: () => import('./components/CargoInformation.vue')
  },
  data() {
    return {
      canChoooseGoods: false,
      isLoading: false,
      detail: {
        purchContractBasicInfo: {},
        purchContractBuyerSellerList: [],
        purchContractGoodsInfoList: [],
        purchContractPriceRule: {},
        purchContractRetentionInfo: {},
        purchContractSettlePayment: {},
        mathId: ''
      }
    }
  },
  mounted() {
    if(this.$route.query.infoId) {
      this.isLoading = true
      getDeatail({ id: this.$route.query.infoId }).then(({ data }) => {
        this.detail = { mathId: Math.random(), ...data }
        // if(this.detail.projectInformationId) {
        //   this.canChoooseGoods = true
        // }
        this.isLoading = false
      }, () => {
        this.isLoading = false
      })
    }
  },
  methods: {
    getParams() {
      let data = {
        basicInfo: { ...this.$refs.basicInfo.ruleForm },
        purchContractBasicInfo: { ...this.$refs.contractInfo.ruleForm },
        purchContractRetentionInfo: { ...this.$refs.retentionInfo.ruleForm },
        purchContractPriceRule: { ...this.$refs.priceRule.ruleForm },
        purchContractSettlePayment: { ...this.$refs.settlePay.ruleForm },
        purchContractBuyerSellerList: [ ...this.$refs.personInfo.ruleForm.personInfoList ],
        purchContractGoodsInfoList: [ ...this.$refs.goodsList.businessInfo.list ]
      }
      if(this.$route.query.infoId) {
        data.businessKey = this.$route.query.infoId
      }
      return data
    },
    save(flag) {
      let data = this.getParams()
      data.commit = flag
      data.Loading = true
      this.isLoading = true
      submitInfo(data).then(() => {
        this.$message.success('提交成功')
        this.isLoading = false
        this.$router.go(-1)
      }, () => {
        this.isLoading = false
      })
    },
    confirm() {
      let p1 = this.$refs.basicInfo.validate()
      let p2 = this.$refs.contractInfo.validate()
      let p3 = this.$refs.priceRule.validate()
      let p4 = this.$refs.settlePay.validate()
      let p5 = this.$refs.personInfo.validate()
      let p6 = this.$refs.goodsList.validate()
      let p7 = this.$refs.retentionInfo.validate()
      Promise.all([p1, p2, p3, p4, p5, p6, p7]).then(() => {
        this.save(1)
      })
    },
    validateEmpty() {
      this.canChoooseGoods = false
      this.$refs.priceRule.validate().then(() => {
        this.canChoooseGoods = true
      }, () => {
        this.$message.error('价格规则不能为空')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.purchase-contract-add {

}
</style>
