<template>
  <div class="nature-business">
    <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-input v-model="defaultDatailsInfo.businessScope" disabled placeholder="请输入经营范围" />
      </el-form-item>
      <el-form-item
        label="标准分值"
      >
        <el-input v-model="defaultDatailsInfo.businessScopeStandardScore" disabled placeholder="请输入标准分值" />
      </el-form-item>
      <el-form-item
        label="测算分值"
      >
        <el-input v-model="defaultDatailsInfo.businessScopeMeasurementScore" disabled placeholder="请输入测算分值" />
      </el-form-item>
      <el-form-item
        label="问题"
        style="width: 100%;"
      >
        <el-input v-model="ruleForm.businessScopeQuestion" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.businessScopeFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="businessScopeFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="准入结论"
        prop="businessScopeConclusion"
        style="width: 100%;"
      >
        <el-select v-model="ruleForm.businessScopeConclusion" 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: {
        businessScopeQuestion: '',
        businessScopeFeedback: '',
        businessScopeConclusion: ''
      },
      businessScopeFile: [],
      rules: {
        businessScopeConclusion: [
          { required: true, message: '请选择准入结论', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { businessScopeQuestion, businessScopeFeedback, businessScopeConclusion, businessScopeFile } = newV.value
        this.ruleForm = {
          businessScopeQuestion,
          businessScopeFeedback,
          businessScopeConclusion: businessScopeConclusion || ''
        }
        this.businessScopeFile = businessScopeFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('natureBusinessParams', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          businessScopeFileIds: 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>
.nature-business {

}
</style>
