<template>
  <div class="apply-block">
    <AttachmentUpload ref="attachment" label="阶段变更附件" :showTable="showTable" :otherFileList="otherFileList"
                      @getAttachment="getAttachment"
                      @getOtherFile="getOtherFile" :showFileList="true" :formData="formData"
                      :preview="name === 'Implementation/edit'"/>
    <div class="approval-record">
      <baseTitle title="流程"></baseTitle>
      <process-diagram-viewer mode="view" v-if="processDiagramViewer"/>
    </div>
    <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 {getProjectCheckProcess, projectCheck, getCheckDetail, resubmitCheck} from "@/api/project-manage";
import {ElNotification} from "element-plus";
import {useProcessStore} from '@/stores/processStore.js';
import {useTagsView} from '@/stores/tagsview.js'

const tagsViewStore = useTagsView()
const router = useRouter()
const route = useRoute()
const attachment = ref()
const name = ref(router.currentRoute.value.name)
const loading = ref(false)
const formData = ref({})
const file = 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 compositeParam = (item) => {
  let tag = ''
  if (name.value === 'Phase/change') {
    tag = '阶段变更'
  }
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    tag: tag
  }
}
const getAttachment = (val) => {
  console.log('上传文件getAttachment', val)
  file.value = compositeParam(val)
}
const getOtherFile = (val) => {
  console.log('上传文件getOtherFile', val)
  showTable.value = false
  let fileObj = compositeParam(val)
  otherFileList.value.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}

const getFileParam = (item) => {
  return {
    fileId: item.fileId,
    tag: item.tag
  }
}
const handleSubmit = (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return
    if (JSON.stringify(file.value) === "{}") {
      attachment.value.validate()
    } else {
      attachment.value.clearValidate()
    }
    if (!valid) return
    let files = []
    let singleFile = {}
    let fileArray

    if (file.value.fileId !== undefined) {
      singleFile = {
        fileId: file.value.fileId
      }
    }
    fileArray = otherFileList.value

    fileArray.forEach(item => {
      files.push(getFileParam(item))
    })
    let params = {
      deploymentId: deploymentId.value,
      requirementId: route.query.id,
      fileList: files,
      singleFile: singleFile,
      projectId: route.query.projectId,
    }
    console.log('params-提交', params)
    // let res = await projectCheck(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: 'Implementation'
    //   })
    // }
  })
}

const init = async () => {
  const {msg, code,data} = await getProjectCheckProcess(route.query.projectId)
  ElNotification({
    title: '提示',
    message: msg,
    type: code === 1000 ? 'success' : 'error'
  })
  processDiagramViewer.value = false
  if (code === 1000) {
    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
    })
  }
}
onMounted(async () => {
  // await init()
})
</script>

<style scoped>
.apply-block{
  margin: 15px 0;
}
</style>
