<template>
  <baseTitle title="基础信息"></baseTitle>
  <fvForm :schema="schema" @getInstance="(e)=>baseForm = e"></fvForm>
  <baseTitle :title="getName()+'-上传附件'"></baseTitle>
  <el-card style="width: 100%;margin: 15px 0">
    <file-upload @getFile="getFile" />
    <fvTable style="width: 100%;max-height: 300px;" v-if="showTable" :tableConfig="tableConfig"
             :data="fileList" :isSettingCol="false" :pagination="false">
      <template #empty>
        <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
      </template>
    </fvTable>
  </el-card>
  <div class="oper-page-btn">
    <el-button color="#DED0B2" @click="handleSubmit">提交</el-button>
  </div>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {useTagsView} from '@/stores/tagsview.js'
import {uploadFileList} from "@/api/project-manage/attachment";
import {computed, ref} from "vue";
import {getBaseInfoApi} from "@/components/steps/api";

const tagsViewStore = useTagsView()
const route = useRoute()
const router = useRouter()
const fileList = ref([])
const formData = ref({
  tagName:''
})
const schema = computed(() => {
  return [
    {
      label: '征集名称',
      prop: 'requirementName',
      colProps: {
        span: 12
      }
    },
    {
      label: '项目名称',
      prop: 'projectName',
      colProps: {
        span: 12
      }
    }
  ]
})
const baseForm = 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>
            </div>
        )
      }
    }
  ]
})
const showTable = ref(true)
const name = ref(router.currentRoute.value.name)
const rules = reactive({
  tagName: [{required: true, message: '请输入标签名称', trigger: 'blur'}],
})
const getBaseInfo = async () => {
  try {
    const {code, data} = await getBaseInfoApi(route.query.id)
    baseForm.value.setValues(data)
  } catch {
  }
}

getBaseInfo()
const getName=()=>{
  switch (route.query.name) {
    case '00':
      return '需求征集'
    case '10':
      return '需求上报'
    case '20':
      return '项目立项'
    case '40':
      return '项目实施'
    case '50':
      return '项目归档'
  }
}
const compositeParam = (item) => {
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    newFile: true,
    tag: getName(),
  }
}
const getFile = (val) => {
  showTable.value = false
  let fileObj = compositeParam(val)
  fileList.value.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}
const getFileParam = (item) => {
  return {
    fileId: item.fileId,
    tag:item.tag
  }
}
const handleSubmit = async () => {
    let files = []
    fileList.value.forEach(item => {
      files.push(getFileParam(item))
    })
    let params = {
      fileList: files,
      targetState:route.query.name,
      projectId: route.query.id,
    }
    let res = await uploadFileList(params)
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      await router.push({
        name: 'Filing/attachment',
        query:{
          id: route.query.id,
          requirementId:route.query.requirementId
        }
      })
    }
}
</script>

<style scoped>
:deep(.el-table--fit ){
  height: 300px!important;
}
</style>
