<!-- 实时工作统计 -->
<script lang="ts" setup name="JobAccountList">
import type { Ref } from 'vue'
import { getCurrentInstance, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'
import type { IList, IListQuery } from './jobAccount-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { printJSON } from '@/utils/printUtils'
import { exportFile } from '@/utils/exportUtils'
import { exportJobAccountList, getJobAccountList } from '@/api/business/board/jobAccount'
const { proxy } = getCurrentInstance() as any
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  offset: 1,
  limit: 20,
})
// 表头
const columns = ref<TableColumn[]>([
  { text: '检测实验室', value: 'organizeName', width: '160', align: 'center' },
  { text: '实验室代码', value: 'organizeNo', width: '160', align: 'center' },
  { text: '当日检测完样品', value: 'samplesCompletedToday', align: 'center' },
  { text: '当日检定员出具证书数', value: 'certificatesToday', align: 'center' },
  { text: '当日到期样品数', value: 'expireSamplesToday', align: 'center' },
  { text: '今年样品数', value: 'samplesYear', align: 'center' },
  { text: '今年超期样品总数', value: 'expireSamplesYear', align: 'center' },
  { text: '今年超期样品总数-检测中', value: 'inMeasureExpireSamplesYear', align: 'center' },
  { text: '超期率', value: 'expireRate', align: 'center' },
])

// 表格数据
const list = ref<IList[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<string[]>([])
// 表格标题时间
const titleTime = ref('')

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getJobAccountList().then((response) => {
    list.value = response.data
    // total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData(true)
}

// 导出
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (list.value.length > 0) {
    exportJobAccountList().then((res) => {
      const blob = new Blob([res.data])
      exportFile(blob, '实时工作统计列表.xlsx')
    })
  }
  else {
    ElMessage.warning('无数据可导出数据')
  }
  loading.close()
}

// 打印列表
function printList() {
  // 打印列
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (checkoutList.value.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '实时工作统计列表')
  }
  else if (checkoutList.value.length > 0) {
    const printList = list.value.filter((item: IList) => checkoutList.value.includes(item.id))
    printJSON(printList, properties, '实时工作统计列表')
  }
  else {
    ElMessage.warning('无可打印内容')
  }
}

onMounted(() => {
  fetchData(true)
  titleTime.value = `计量服务实时工作统计 (${dayjs().format('YYYY-MM-DD HH:mm')})`
})
</script>

<template>
  <app-container>
    <table-container :title="titleTime" :is-center="true">
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/business/board/jobAccount/export')" icon="icon-export" title="导出" type="primary" @click="exportAll" />
        <icon-button v-if="proxy.hasPerm('/business/board/jobAccount/print')" icon="icon-print" title="打印" type="primary" @click="printList" />
      </template>
      <normal-table
        :data="list" :total="total" :pagination="false" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" :is-showmulti-select="false" @change="changePage" @multi-select="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>
