<template>
  <div v-loading="loading" class="add-block">
    <baseTitle title="专项资金信息录入"></baseTitle>
    <el-form :model="formData" inline class="query-form" ref="fundForm" :rules="rules">
      <el-form-item label="专项资金名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入专项资金名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="fundAmount">
        <el-input v-model="formData.fundAmount" placeholder="请输入金额" clearable></el-input>
      </el-form-item>
    </el-form>
    <baseTitle title="介绍"></baseTitle>
    <Tinymce image-url="/notice/file" file-url="/notice/file" v-if="showTinymce"
             v-model:value="formData.introduce" height="300"/>
    <baseTitle title="申请文件"></baseTitle>
    <file-upload @getFile="getFile"/>
    <fvTable style="width: 100%;max-height: 200px" v-if="showTable"
             :tableConfig="tableConfig" :data="formData.files"
             :isSettingCol="false" :pagination="false">
      <template #empty>
        <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
      </template>
    </fvTable>
    <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" v-if="routerName === 'Fund/add'" @click="handleSubmit(fundForm)">提交</el-button>
      <el-button color="#DED0B2" v-else @click="handleResubmit">重新提交</el-button>
      <el-button @click="handleBack">返回</el-button>
    </div>
  </div>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {addFund, resubmitFund, getFundDetail} from "@/api/special-fund";
import {useRouter} from "vue-router";
import {useTagsView} from '@/stores/tagsview.js'

import {useProcessStore} from '@/stores/processStore.js';

const tagsViewStore = useTagsView()
const router = useRouter()
const route = useRoute()
const processStore = useProcessStore()
const loading = ref(false)
const showTinymce = ref(true)
const showTable = ref(true)
const processInstanceData = ref()
const fundForm = ref()
const processDiagramViewer = ref(false)
const formData = ref({
  name: '',
  fundAmount: '',
  introduce: '',
  files: []
})
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',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        return (
            <div>
              <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
            </div>
        )
      }
    }
  ]
})
const routerName = ref(router.currentRoute.value.name)
const rules = reactive({
  name: [{required: true, message: '请输入专项资金名称', trigger: 'blur'}],
  fundAmount: [{required: true, message: '请输入金额', trigger: 'blur'}],
  introduce: [{required: true, message: '请输入介绍', trigger: 'blur'}],
})

const compositeParam = (item) => {
  let tag = ''
  if (routerName.value === 'Fund/add' || routerName.value === 'Fund/edit') {
    tag = '专项资金'
  }
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    tag: tag
  }
}
const getFile = (val) => {
  console.log('上传文件', val)
  showTable.value = false
  let fileObj = compositeParam(val)
  formData.value.files.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}
const handleBack = () => {
  history.back()
}

const submitParam = (item) => {
  let files = []
  item.files.forEach(item => {
    let obj = {
      fileId: item.fileId,
      tag: item.tag
    }
    files.push(obj)
  })
  return {
    fundAmount: item.fundAmount,
    introduce: item.introduce,
    name: item.name,
    files: files,
    // deploymentId: processInstanceData.value.deploymentId
  }
}
const handleSubmit = async (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) return
    const {msg, code} = await addFund(submitParam(formData.value))
    ElNotification({
      title: '提示',
      message: msg,
      type: code === 1000 ? 'success' : 'error'
    })
    if (code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      await router.push({
        name: 'Fund'
      })
    }
  })
}
const handleResubmit = () => {
  if (!route.query.id) return
  let params = {
    id: route.query.id,
    ...submitParam(formData.value)
  }
  resubmitFund(params).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      router.push({
        name: 'Fund'
      })
    }
  })
}
const init = async () => {
  getWorkflowInfo().then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      let data = res.data
      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
      })
    }
  })
}
const getDetailInfo = async () => {
  getFundDetail(route.query.id).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      formData.value = res.data.formData
      showTinymce.value = false
      showTable.value = false
      nextTick(() => {
        showTinymce.value = true
        showTable.value = true
      })
    }
  })
}
onMounted(async () => {
  loading.value = true
  // await init()
  if (route.query.id) {
    await getDetailInfo()
  }
  loading.value = false
})
</script>

<style scoped>

</style>
