<script setup name="recordListDialg">
import { watch, ref, h } from 'vue'
import { recordList } from '@/http/apis/performanceEvaluation/inquirySheetManage'
import store from '@/store'
import { downFileById } from '@/http/apis/commonApi'
const { projectTypeOptions } = store.dictStore.globalDicts || {}
const props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object
  }),
  emits = defineEmits(['close']),
  column = [
    {
      label: '序号',
      type: 'index',
      width: '80'
    },
    {
      label: '项目名称',
      key: 'projectName',
      prop: 'projectName'
    },
    {
      label: '申报单位',
      key: 'buildOrgName',
      prop: 'buildOrgName'
    },
    {
      label: '项目类型',
      key: 'projectType',
      prop: 'projectType',
      render: row => h('span', projectTypeOptions[row.projectType] || '-')
    },
    {
      label: '预算年度',
      key: 'projectYear',
      prop: 'projectYear'
    },
    {
      label: '咨询单状态',
      key: 'status',
      prop: 'status',
      render: row =>
        h(
          'span',
          {
            class:
                    [1, 2, 3].includes(row.status) // 1.待回复 2.待处理 3.待审核 4.通过 5.驳回
                      ? 'text-warning'
                      : row.status === 4 ? 'text-success' : 'text-danger'
          },
          row.statusName
        )
    },
    {
      label: '审核时间',
      key: 'createOn',
      prop: 'createOn'
    },
    {
      label: '咨询单',
      key: 'consultationForm',
      slot: 'consultationForm'
    },
    {
      label: '回复单',
      key: 'replyForm',
      slot: 'replyForm'
    }
  ],
  tableData = ref(),
  getList = async () => {
    const res = await recordList(props.data.id)
    tableData.value = res.data
  },
  // 下载
  down = async (file) => {
    const fileId = file && JSON.parse(file)?.[0]?.id
    const fileName = file && JSON.parse(file)?.[0]?.originalFileName
    const res = await downFileById({ fileId: fileId })
    const url = URL.createObjectURL(res)
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.download = fileName
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    window.URL.revokeObjectURL(url)
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      getList()
    }
  }
)

</script>
<template>
  <el-dialog
    :model-value="visible"
    :title="`${props.data?.projectName}-咨询单、回复单列表`"
    width="70%"
    destroy-on-close
    @close="emits('close')"
  >
    <table-list
      ref="tableListRef"
      :column="column"
      :data="tableData"
      :pagination="false"
    >
      <template #consultationForm="{ scope }">
        <a @click="down(scope.row.consultationForm)">下载</a>
      </template>
      <template #replyForm="{ scope }">
        <a v-if="scope.row.replyForm" @click="down(scope.row.replyForm)">下载</a>
      </template>
    </table-list>
  </el-dialog>
</template>
