<template>
  <div class="apply-block">
    <el-form :model="formData" ref="applyForm" label-width="auto" :rules="rules">
      <baseTitle title="项目结项"></baseTitle>
      <AttachmentUpload label="项目结项附件" :showTable="showTable" :otherFileList="otherFileList"
                        @getAttachment="getAttachment"
                        @getOtherFile="getOtherFile" :showFileList="true"/>
      <div class="approval-record">
        <baseTitle title="流程"></baseTitle>
        <process-diagram-viewer mode="view" v-if="processDiagramViewer"/>
      </div>
    </el-form>
    <div class="oper-page-btn">
      <el-button color="#DED0B2" @click="handleSubmit(applyForm)">提交</el-button>
    </div>
  </div>
</template>

<script setup lang="jsx">
import ProcessDiagramViewer from '@/views/workflow/common/ProcessDiagramViewer.vue';
import {downloadFile} from "@/api/project-demand";
import { ElNotification} from "element-plus";
import {useProcessStore} from '@/stores/processStore.js';
import {getProjectConclusionProcess, projectConclusion} from "@/api/project-manage";

const formData = ref({})
const rules = reactive({
  attachment: [{required: true, message: '请上传项目结项附件', trigger: 'blur'}],
})
const fileList = ref([])
const applyForm = ref()
const deploymentId = ref()
const showTable = ref(true)
const otherFileList = ref([])
const processInstanceData = ref()
const processDiagramViewer = ref(true)
const processStore = useProcessStore()
const router = useRouter()
const route = useRoute()
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) => {
  let tag = ''
  if (router.currentRoute.value.name === 'Filing/conclusion') {
    tag = '项目结项'
  }
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    processNodeTag: null,
    tag: tag
  }
}
const getAttachment = (val) => {
  console.log('上传文件getAttachment', val)
  let fileObj = compositeParam(val, 1)
  fileList.value.push(fileObj)
}
const getOtherFile = (val) => {
  console.log('上传文件getOtherFile', val)
  showTable.value = false
  let fileObj = compositeParam(val, 2)
  otherFileList.value.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}

const getFileParam = (item) => {
  return {
    fileId: item.fileId,
    type: item.type
  }
}
const handleSubmit = (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return
    if (fileList.value.length === 0) {
      applyForm.value.validate()
    } else {
      applyForm.value.clearValidate()
    }
    if (!valid) return
    let files = []
    fileList.value.forEach(item => {
      files.push(getFileParam(item))
    })
    otherFileList.value.forEach(item => {
      files.push(getFileParam(item))
    })
    let params = {
      deploymentId: deploymentId.value,
      requirementId: route.query.id,
      files: files,
      projectId:route.query.projectId,
    }
    console.log('params', params)
    let res = await projectConclusion(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'
      })
    }
  })
}
const init =  () => {
  getProjectConclusionProcess().then(res => {
    processDiagramViewer.value = false
    if (res.code === 1000) {
      let data = res.data
      deploymentId.value=data.deploymentId
      processInstanceData.value = data
      processStore.setDesign(data)
      processStore.runningList.value = data.runningList;
      processStore.endList.value = data.endList;
      processStore.noTakeList.value = data.noTakeList;
      processStore.refuseList.value = data.refuseList;
      processStore.passList.value = data.passList;
      nextTick(() => {
        processDiagramViewer.value = true
      })
    }
  })
}
init()
</script>

<style scoped>

</style>
