<template>
  <div class="registered-capital">
    <el-form
      ref="ruleForm"
      :rules="rules"
      :model="ruleForm"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item
        label="指标名称"
        style="width: 100%"
      >
        <el-input v-model="title" disabled placeholder="请输入指标名称" />
      </el-form-item>
      <el-form-item
        label="注册资本"
      >
        <el-input v-model="defaultDatailsInfo.registeredCapital" disabled placeholder="请输入注册资本" />
      </el-form-item>
      <el-form-item
        label="标准分值"
      >
        <el-input v-model="defaultDatailsInfo.capitalStandardScore" disabled placeholder="请输入标准分值" />
      </el-form-item>
      <el-form-item
        label="测算分值"
      >
        <el-input v-model="defaultDatailsInfo.capitalMeasurementScore" disabled placeholder="请输入测算分值" />
      </el-form-item>
      <el-form-item
        label="问题"
        style="width: 100%"
      >
        <el-input v-model="ruleForm.capitalQuestion" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.capitalFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="capitalFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="准入结论"
        style="width: 100%;"
        prop="capitalConclusion"
      >
        <el-select v-model="ruleForm.capitalConclusion" placeholder="请选择准入结论" :disabled="disabled">
          <el-option
            v-for="item in [{ value: 1, label: '是'}, { value: 0, label: '否' }]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bus from '@/utils/eventBus'
export default {
  components: {
    UnifiedEditor: () => import('@/components/UnifiedEditor/index.vue')
  },
  inject: ['datailsInfo', 'disabled', 'submitType'],
  data() {
    return {
      title: '注册资本',
      ruleForm: {
        capitalQuestion: '',
        capitalFeedback: '',
        capitalConclusion: ''
      },
      capitalFile: [],
      rules: {
        capitalConclusion: [
          { required: true, message: '请选择准入结论', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        if (newV.value === undefined) return
        const { capitalQuestion, capitalFeedback, capitalConclusion, capitalFile } = newV.value
        this.ruleForm = {
          capitalQuestion,
          capitalFeedback,
          capitalConclusion: newV.value.capitalConclusion || ''
        }
        console.log(this.ruleForm)
        this.capitalFile = capitalFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('registeredCapitalParmas', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          capitalFileIds: fileList.map(item => item.id)
        }
        if (this.submitType() === 'temp') {
          resolve(params)
        } else {
          this.$refs.ruleForm.validate(valid => {
            if (valid) {
              resolve(params)
            } else {
              reject()
              return false
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.registered-capital {

}
</style>
