<template>
  <div class="related-enterprises">
    <el-form
      ref="ruleForm"
      :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="关联企业相关情况"
        prop="resource"
        style="width: 100%"
      >
        <cy-comm-table
          ref="commTable"
          :columns="columns"
          :data="defaultDatailsInfo.enterpriseInfoHistoryList"
        />
      </el-form-item>
      <el-form-item
        label="问题"
        prop="resource"
        style="width: 100%"
      >
        <el-input v-model="ruleForm.associatedEnterpriseQuestion" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>

      <el-form-item
        label=" 尽调反馈"
        prop="resource"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.associatedEnterpriseFeedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="associatedEnterpriseFile"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </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: {
        desc: ''
      },
      columns: [
        {
          label: '序号',
          index: true,
          width: '100'
        },
        {
          label: '关联企业名称',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '状态',
          showTooltip: true,
          prop: 'status'
        },
        {
          label: '法定代表人',
          showTooltip: true,
          prop: 'legalRepresentative'
        },
        {
          label: '持股比例',
          showTooltip: true,
          prop: 'stockPercent',
          render: (h, row) => {
            return <span>{row.stockPercent}%</span>
          }
        },
        {
          label: '主营业务',
          showTooltip: true,
          prop: 'mainBusiness'
        }
      ],
      associatedEnterpriseFile: []
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { associatedEnterpriseQuestion, associatedEnterpriseFeedback, associatedEnterpriseFile } = newV.value
        this.ruleForm = {
          associatedEnterpriseQuestion,
          associatedEnterpriseFeedback
        }
        this.associatedEnterpriseFile = associatedEnterpriseFile || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    bus.on('relatedEnterprisesParams', (cb) => {
      cb(this.getParams())
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          ...this.ruleForm,
          associatedEnterpriseFileIds: 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>
.related-enterprises {

}
</style>
