<template>
  <div class="audit-conclusion">
    <cy-info-title style="margin-top: 20px">企业财务情况</cy-info-title>
    <el-form ref="ruleForm" :model="ruleForm" label-position="top" :rules="rules" class="rule-form-orange">
      <el-form-item label="主营业务收入" prop="mainBusinessIncome">
        <cy-amount-input
          v-model="ruleForm.mainBusinessIncome"
          :is-unit="true"
          unit-name="元"
          placeholder="请输入主营业务收入"
          :disabled="disabled"
          @blur="calcMainBusinessProfit"
        >
        </cy-amount-input>
      </el-form-item>
      <el-form-item label="主营业务成本" prop="mainBusinessCost">
        <cy-amount-input
          v-model="ruleForm.mainBusinessCost"
          :is-unit="true"
          unit-name="元"
          placeholder="请输入主营业务成本"
          :disabled="disabled"
          @blur="calcMainBusinessProfit"
        >
          <template slot="append">元</template>
        </cy-amount-input>
      </el-form-item>
      <el-form-item label="毛利率" prop="mainBusinessProfit">
        <el-input v-model="ruleForm.mainBusinessProfit" placeholder="请输入毛利率" disabled>
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="动产融资登记" prop="name">
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="ruleForm.fileStorageList"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        mainBusinessIncome: '',
        mainBusinessCost: '',
        mainBusinessProfit: ''
      },
      rules: {
        mainBusinessIncome: [
          { required: true, message: '请输入主营业务收入', trigger: 'blur' }
        ],
        mainBusinessCost: [
          { required: true, message: '请输入主营业务成本', trigger: 'blur' }
        ],
        mainBusinessProfit: [
          { required: true, message: '请输入毛利率', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    detailsInfo: {
      handler(newV) {
        if (newV === undefined) return
        this.ruleForm = JSON.parse(JSON.stringify(newV))
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    calcMainBusinessProfit() {
      const { mainBusinessIncome, mainBusinessCost } = this.ruleForm
      if (mainBusinessIncome && mainBusinessCost) {
        this.ruleForm.mainBusinessProfit = ((Number(mainBusinessIncome) - Number(mainBusinessCost)) / Number(mainBusinessIncome) * 100).toFixed(2)
      } else {
        this.ruleForm.mainBusinessProfit = ''
      }
    },
    getParams(type) {
      return new Promise((resolve, reject) => {
        const list = this.$refs.fileStorage.getFileLists.map(item => item.id)
        if (type === 'temp') {
          resolve({ ...this.ruleForm, files: list })
        } else {
          this.$refs.ruleForm.validate(valid => {
            if (valid) {
              resolve({ ...this.ruleForm, files: list })
            } else {
              reject()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
