<template>
  <div class="age-box">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      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-date-picker
          v-model="defaultDatailsInfo.startDate"
          type="date"
          disabled
          placeholder="请选择成立日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item
        label="成立年限"
      >
        <el-input
          v-model="defaultDatailsInfo.establishmentPeriod"
          disabled
          placeholder="请输入成立年限"
        />
      </el-form-item>
      <el-form-item
        label="标准分值"
      >
        <el-input v-model="defaultDatailsInfo.lifeStandardScore" disabled placeholder="请输入标准分值" />
      </el-form-item>
      <el-form-item
        label="测算分值"
      >
        <el-input v-model="defaultDatailsInfo.lifeMeasurementScore" disabled placeholder="请输入测算分值" />
      </el-form-item>
      <el-form-item
        label="问题"
        prop="lifeQuestion"
        class="two-form-item"
      >
        <el-input v-model="ruleForm.lifeQuestion" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label="尽调反馈"
        prop="lifeFeedback"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.lifeFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="lifeFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="准入结论"
        prop="lifeConclusion"
        style="width: 100%"
      >
        <el-select v-model="ruleForm.lifeConclusion" 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: {
        lifeQuestion: '',
        lifeFeedback: '',
        lifeConclusion: ''
      },
      lifeFile: [],
      rules: {
        lifeConclusion: [
          { required: true, message: '请选择准入结论', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { lifeQuestion, lifeFile, lifeFeedback, lifeConclusion } = newV.value
        this.ruleForm.lifeQuestion = lifeQuestion
        this.ruleForm.lifeFeedback = lifeFeedback
        this.ruleForm.lifeConclusion = lifeConclusion
        this.lifeFile = lifeFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('ageParmas', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          lifeFileIds: 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>
.age-box {

}
</style>
