<template>
  <div v-loading="loading">
    <el-form :model="formData" ref="form" label-width="auto">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称">
            <span>{{ formData.requirementName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属公司">
            <span>{{ formData.companyIds }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="征集类型">
            <span>{{ formData.collectType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止时间">
            <span>{{ formData.deadline }}</span>
          </el-form-item>
        </el-col>
        <baseTitle title="征集说明"></baseTitle>
        <el-col :span="24">
          <el-form-item>
            <el-card style="width: 100%">
              <div v-html="formData.collectExplain">
              </div>
            </el-card>
          </el-form-item>
        </el-col>
        <baseTitle title="附件列表"></baseTitle>
        <el-col :span="24">
          <el-form-item>
            <fvTable style="width: 100%;max-height: 200px" v-if="processViewer" :tableConfig="tableConfig"
                     :data="formData.fileList" :isSettingCol="false" :pagination="false">
              <template #empty>
                <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
              </template>
            </fvTable>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div v-if="formData.taskId">
            <baseTitle title="审核意见"></baseTitle>
            <el-form-item prop="auditOpinion">
              <el-input
                  v-model="formData.auditOpinion"
                  :rows="3"
                  type="textarea"
                  placeholder="请输入审核意见"
              />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <div class="approval-record">
        <baseTitle title="审批记录"></baseTitle>
        <div class="process">
          <operation-render v-if="processViewer" :operation-list="data.operationList"
                            :state="data.state"/>
          <process-diagram-viewer v-if="processViewer"/>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="jsx">
import OperationRender from '@/views/workflow/common/OperationRender.vue'
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue'
import {matterTree} from '@/utils/matterTree.js';
import {downloadFile} from "@/api/project-demand";

const emit = defineEmits(['getInfo',"update:formData"])
const form = ref()
const showTable = ref(false)
const companyNameArray = ref([])

const props = defineProps({
  formData: {
    type: Array,
    default: []
  },
  data: {
    type: Array,
    default: []
  },
  processViewer: {
    type: Boolean,
    default: false
  },
  companyOption: {
    type: Array,
    default: []
  }, loading: {
    type: Boolean,
    default: false
  }
})
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
    },
    {
      prop: 'tag',
      label: '标签',
      align: 'center'
    },
    {
      prop: 'size',
      label: '文件大小',
      align: 'center',
      currentRender: ({row, index}) => (parseInt(row.size / 1024) + 'KB')
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      currentRender: ({row, index}) => {
        return (
            <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
        )
      }
    }
  ]
})
const handleDownload = (row) => {
  downloadFile(row.fileId).then(res => {
    const blob = new Blob([res])
    let a = document.createElement('a')
    a.href = URL.createObjectURL(blob)
    a.download = row.originalFileName
    a.click()
  })
}
const getDataSourceOptionItem = (val) => {
  if (val instanceof Array) {
    val.forEach(item => {
      matterTree(companyNameArray.value, props.companyOption, item)
    })
  }
  let uniqueArr = Array.from(new Set(companyNameArray.value));
  return uniqueArr.join('，');
}

watch(() => props.loading, (newVal) => {
  props.loading = newVal
}, {deep: true})

watch(() => props.companyOption, (newVal) => {
  props.companyOption = newVal
}, {deep: true})
watch(() => props.formData.companyIds, (newVal) => {
  props.formData.companyIds = getDataSourceOptionItem(newVal)
}, {deep: true})
watch(() => props.processViewer, (newVal) => {
  props.processViewer = newVal
}, {deep: true})
</script>

<style scoped>

</style>
