<template>
  <div class="change-info">
    <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.basicChangeSize" disabled placeholder="请输入基本变动信息" />
      </el-form-item>
      <el-form-item
        label="标准分值"
      >
        <el-input v-model="defaultDatailsInfo.changeStandardScore" disabled placeholder="请输入标准分值" />
      </el-form-item>
      <el-form-item
        label="测算分值"
      >
        <el-input v-model="defaultDatailsInfo.changeMeasurementScore" disabled placeholder="请输入测算分值" />
      </el-form-item>
      <el-form-item
        label="问题"
        style="width: 100%;"
      >
        <el-input v-model="ruleForm.changeQuestion" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.changeFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="changeFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="准入结论"
        prop="changeConclusion"
        style="width: 100%"
      >
        <el-select v-model="ruleForm.changeConclusion" 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: {
        changeQuestion: '',
        changeFeedback: '',
        changeConclusion: ''
      },
      changeFile: [],
      rules: {
        changeConclusion: [
          { required: true, message: '请选择准入结论', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    defaultDatailsInfo() {
      return { ...this.datailsInfo.value, basicChangeSize: `近一年股东或法人变更${this.datailsInfo.value.basicChangeSize}次` }
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { changeQuestion, changeFeedback, changeConclusion, changeFile } = newV.value
        this.ruleForm = {
          changeQuestion,
          changeFeedback,
          changeConclusion: changeConclusion || ''
        }
        this.changeFile = changeFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('changeInfoParams', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          changeFileIds: 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>
.change-info {

}
</style>
