<template>
  <div class="apply-block">
    <el-form :model="formData" ref="applyForm" label-width="auto" :rules="rules">
      <baseTitle title="项目结项"></baseTitle>
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目结项附件" prop="attachment">
            <file-upload @getFile="getAttachment" :showFileList="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="其他文件">
            <el-card style="width: 100%">
              <file-upload @getFile="getOtherFile" :showFileList="true"/>
              <fvTable style="width: 100%;max-height: 250px" v-if="showTable" :tableConfig="tableConfig"
                       :data="otherFileList" :isSettingCol="false" :pagination="false">
                <template #empty>
                  <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
                </template>
              </fvTable>
            </el-card>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="oper-page-btn">
      <el-button color="#DED0B2" @click="handleSubmit(applyForm)">提交</el-button>
    </div>
  </div>
</template>

<script setup lang="jsx">
import FileUpload from "@/components/FileUpload.vue";
import {downloadFile} from "@/api/project-demand";

const formData = ref({})
const rules = reactive({
  attachment: [{required: true, message: '请上传项目结项附件', trigger: 'blur'}],
})
const fileList = ref(null)
const applyForm = ref()
const showTable = ref(true)
const otherFileList = ref([])
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 (
            <div>
              <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
              <el-button type="primary" size="large" link onClick={() => beforeRemove(row)}>删除</el-button>
            </div>
        )
      }
    }
  ]
})
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 compositeParam = (item) => {
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    processNodeTag: null,
    tag: formData.value.collectType
  }
}
const getAttachment = (val) => {
  console.log('上传文件', val)
  showTable.value = false
  nextTick(() => {
    showTable.value = true
  })
  let fileObj = {}
  let newFileArray = []
  if (route.query.isAdd === undefined) {
    val.forEach(item => {
      fileObj = compositeParam(item)
      newFileArray.push(fileObj)
      formData.value.fileList.push(fileObj)
    })
    fileList.value = formData.value.fileList
  } else {
    val.forEach(item => {
      fileObj = compositeParam(item)
      newFileArray.push(fileObj)
    })
    formData.value.fileList = newFileArray
    fileList.value = newFileArray
  }
}
const getOtherFile = () => {

}
const handleSubmit = (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return

  })
}
</script>

<style scoped>

</style>
