<template>
  <div class="cooperation-box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item label="本次拟合作金额（万元）" prop="cooperationAmount">
        <cy-amount-input
          v-model="ruleForm.cooperationAmount"
          placeholder="请输入本次拟合作金额（万元）"
          unit-name="CNY"
          is-unit
        />
      </el-form-item>
      <el-form-item label="月供货峰值（万元）" prop="peakValue">
        <cy-amount-input
          v-model="ruleForm.peakValue"
          placeholder="请输入月供货峰值（万元）"
          unit-name="CNY"
          is-unit
        />
      </el-form-item>
      <el-form-item label="预估账期(月)" prop="estimatedAccountingPeriod">
        <el-input
          v-model="ruleForm.estimatedAccountingPeriod"
          placeholder="请输入预估账期（月）"
        />
      </el-form-item>
      <el-form-item label="本次用款高峰值（万元）">
        <cy-amount-input
          v-model="highPeakUsage"
          disabled
          placeholder="请输入本次用款高峰值（万元）"
          unit-name="CNY"
          is-unit
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    details: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        cooperationAmount: '',
        peakValue: '',
        estimatedAccountingPeriod: ''
      },
      rules: {
        cooperationAmount: [
          { required: true, message: '请输入本次拟合作金额', trigger: 'blur' }
        ],
        peakValue: [
          { required: true, message: '请输入月供货峰值', trigger: 'blur' }
        ],
        estimatedAccountingPeriod: [
          { required: true, message: '请输入预估账期', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    highPeakUsage() {
      if (this.ruleForm.peakValue && this.ruleForm.estimatedAccountingPeriod) {
        return this.ruleForm.peakValue * this.ruleForm.estimatedAccountingPeriod
      } else {
        return 0
      }
    }
  },
  watch: {
    details: {
      handler(newV) {
        if (newV && Object.keys(newV)) {
          const details = JSON.parse(JSON.stringify(newV))
          for (const k in this.ruleForm) {
            if (details[k] !== '' && details[k] !== null && details[k] !== undefined) {
              this.ruleForm[k] = details[k]
            }
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    validate(submitType) {
      return new Promise((resolve, reject) => {
        const highPeakUsage = this.ruleForm.peakValue * this.ruleForm.estimatedAccountingPeriod
        if (submitType === 'submit') {
          this.$refs.ruleFormRef.validate(valid => {
            if (valid) {
              return resolve({ ...this.ruleForm, highPeakUsage })
            } else {
              reject()
              return false
            }
          })
        } else {
          resolve({ ...this.ruleForm, highPeakUsage })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.cooperation-box {
.el-form {
  padding: 10px 20px;
}
}
</style>
