<template>
  <div class="operate-situation">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item
        label="指标名称"
        prop="resource"
        style="width: 100%"
      >
        <el-input v-model="title" disabled placeholder="请输入指标名称" />
      </el-form-item>
      <el-form-item
        label="上游采购单据(请提供2家合作企业)"
        prop="resource"
        style="width: 100%"
      >
        <cy-comm-table
          ref="commTable"
          :columns="upstreamColumns"
          :data="upstreamTableData"
        />
      </el-form-item>
      <el-form-item
        label="下游销售单据"
        prop="resource"
        style="width: 100%"
      >
        <cy-comm-table
          ref="commTable"
          :columns="downstreamColumns"
          :data="downstreamTableData"
        />
      </el-form-item>
      <el-form-item
        label="问题"
        prop="resource"
        style="width: 100%;"
      >
        <el-input v-model="ruleForm.question" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        prop="resource"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.feedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="fileStorageList"
          :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: {
        question: '',
        feedback: ''
      },
      fileStorageList: [],
      upstreamTableData: [],
      upstreamColumns: [
        {
          label: '合作企业名称',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '上游采购单据（请上传验收单、结算单或对账单、发票、近期回款凭证）',
          showTooltip: true,
          prop: 'userNumber',
          render: (h, row) => {
            return <cy-upload
              uploadParams={{}}
              fileData={row.fileStorageDOS}
              disabled
              defaultDesc='请上传附件'
            />
          }
        },
        {
          label: '标准分值',
          showTooltip: true,
          prop: 'standardScore'
        },
        {
          label: '测算分值',
          showTooltip: true,
          prop: 'score'
        },
        {
          label: '准入结论',
          showTooltip: true,
          ruleRequired: true,
          render: (h, row) => {
            return <el-select
              v-model={row.result}
              disabled={this.disabled}
              placeholder='请选择准入结论'>
              <el-option label='是' value={1}></el-option>
              <el-option label='否' value={0}></el-option>
            </el-select>
          }
        }
      ],
      downstreamTableData: [],
      downstreamColumns: [
        {
          label: '国央企名称',
          prop: 'name',
          showTooltip: true
        },
        {
          label: '下游销售单据（请上传验收单、结算单或对账单、发票、近期回款凭证',
          showTooltip: true,
          prop: 'userNumber',
          render: (h, row) => {
            return <cy-upload
              uploadParams={{}}
              fileData={row.fileStorageDOS}
              disabled
              defaultDesc='请上传附件'
            />
          }
        },
        {
          label: '标准分值',
          showTooltip: true,
          prop: 'standardScore'
        },
        {
          label: '测算分值',
          showTooltip: true,
          prop: 'score'
        },
        {
          label: '准入结论',
          showTooltip: true,
          ruleRequired: true,
          render: (h, row) => {
            return <el-select
              v-model={row.result}
              disabled={this.disabled}
              placeholder='请选择准入结论'>
              <el-option label='是' value={1}></el-option>
              <el-option label='否' value={0}></el-option>
            </el-select>
          }
        }
      ]
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        if (newV.value === undefined) return
        const { upstreamProcurementDocumentsList, salesDocumentsInfoList, videoDueDiligenceOperations } = JSON.parse(JSON.stringify(newV.value))
        const upstreamTableData = upstreamProcurementDocumentsList.map(item => {
          item.result = item.result === null ? '' : item.result
          return item
        })
        this.$set(this, 'upstreamTableData', upstreamTableData)
        const downstreamTableData = salesDocumentsInfoList.map(item => {
          item.result = item.result === null ? '' : item.result
          return item
        })
        this.$set(this, 'downstreamTableData', downstreamTableData)
        this.ruleForm = {
          question: videoDueDiligenceOperations.question,
          feedback: videoDueDiligenceOperations?.feedback || ''
        }
        this.fileStorageList = videoDueDiligenceOperations?.files || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      bus.on('operateSituationParams', (cb) => {
        cb(this.getParams())
      })
    })
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        const fileList = this.$refs.fileStorage.getFileLists
        const params = {
          upstreamProcurementDocumentsList: this.upstreamTableData,
          salesDocumentsInfoList: this.downstreamTableData,
          videoDueDiligenceOperations: {
            ...this.ruleForm,
            fileIds: fileList.map(item => item.id)
          }
        }
        if (this.submitType() === 'temp') {
          resolve(params)
        } else {
          for (let i = 0; i < this.upstreamTableData.length; i++) {
            const { result } = this.upstreamTableData[i]
            if (result === '') {
              this.$message.warning(`第${i + 1}条上游采购单据请选择准入结论`)
              reject()
              return
            }
          }
          for (let i = 0; i < this.downstreamTableData.length; i++) {
            const { result } = this.downstreamTableData[i]
            if (result === '') {
              this.$message.warning(`第${i + 1}条下游销售单据请选择准入结论`)
              reject()
              return
            }
          }
          resolve(params)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.operate-situation {

}
</style>
