<template>
  <div class="business-address">
    <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="经营地址"
        style="width: 100%"
      >
        <el-input v-model="defaultDatailsInfo.businessAddress" disabled placeholder="请输入经营地址" />
      </el-form-item>
      <el-form-item
        label="租房合同"
      >
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="defaultDatailsInfo.rentalFiles"
          disabled
          :upload-from-orange-status="true"
          placeholder="请上租房合同"
        />
      </el-form-item>
      <el-form-item
        label="水电费缴纳流水"
      >
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="defaultDatailsInfo.waterFiles"
          disabled
          :upload-from-orange-status="true"
          placeholder="请上水电费缴纳流水"
        />
      </el-form-item>
      <el-form-item label="办公环境图">
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="defaultDatailsInfo.officeFiles"
          disabled
          :upload-from-orange-status="true"
          placeholder="请上传办公环境图"
        />
      </el-form-item>
      <el-form-item label="标准分值">
        <el-input v-model="defaultDatailsInfo.businessAddressStandardScore" disabled placeholder="请输入 标准分值" />
      </el-form-item>
      <el-form-item label="测算分值">
        <el-input v-model="defaultDatailsInfo.businessAddressMeasurementScore" disabled placeholder="请输入测算分值" />
      </el-form-item>
      <el-form-item label="问题">
        <el-input v-model="ruleForm.businessAddressQuestion" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.businessAddressFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="businessAddressFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="准入结论"
        prop="businessAddressConclusion"
        style="width: 100%;"
      >
        <el-select v-model="ruleForm.businessAddressConclusion" 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: {
        businessAddressQuestion: '',
        businessAddressFeedback: '',
        businessAddressConclusion: ''
      },
      businessAddressFile: [],
      rules: {
        businessAddressConclusion: [
          { required: true, message: '请选择准入结论', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { businessAddressQuestion, businessAddressFeedback, businessAddressConclusion, businessAddressFile } = newV.value
        this.ruleForm = {
          businessAddressQuestion,
          businessAddressFeedback,
          businessAddressConclusion: businessAddressConclusion || ''
        }
        this.businessAddressFile = businessAddressFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('businessAddressParams', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          businessAddressFileIds: 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>
.business-address {

}
</style>
