Newer
Older
smart-metering-front / src / views / system / tool / certificate / certificate.vue
<!-- 证书报告模板 -->
<script lang="ts" setup>
import { Delete } from '@element-plus/icons-vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
// import tableHeader from '../../notice/tableHeader.vue'
import type { templateType, typeofSign } from '../tool_interface'
import addDDialog from './addDDialog.vue'
import { templateDelete, templateExport, templatePage } from '@/api/system/tool'
import { getDictByCode } from '@/api/system/dict'
import { exportExcel } from '@/utils/exportXlsx'
import { exportFile } from '@/utils/exportUtils'
const searchQuery = reactive({
  templateNo: '', // 编号
  templateName: '', // 名称
  templateCreator: '', // 负责人
  createTime: '', // 创建时间
  limit: 10,
  offset: 1,
  templateType: '',
}) // 查询参数
const loadingTable = ref<boolean>(false) // 表格loading
const total = ref<number>(0) // 数据总条数
const columns = ref([
  { text: '编号', value: 'templateNo', align: 'center', width: '180' },
  { text: '模板名称', value: 'templateName', align: 'center', width: '150' },
  { text: '模板负责人', value: 'templateCreator', align: 'center', width: '100' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '250' },
  { text: '描述', value: 'signDesc', align: 'center' },
]) // 表格
const list = ref([]) // 表格数据
const addRef = ref() // 添加/编辑/详情/组件
// 获取类型
const getType = async () => {
  const res = await getDictByCode('templateType')
  searchQuery.templateType = res.data.filter((item: typeofSign) => item.name === '证书报告模板')[0].value
}
// 获取数据列表
const getList = () => {
  loadingTable.value = true
  templatePage(searchQuery).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.limit = val.size
  }
  if (val && val.page) {
    searchQuery.offset = val.page
  }
  getList()
}
// 编辑
const edit = (row: templateType) => {
  addRef.value.initDialog({
    ...row,
    templateType: searchQuery.templateType,
    title: '更新',
  })
}
// 详情
const detail = (row: templateType) => {
  addRef.value.initDialog({
    ...row,
    templateType: searchQuery.templateType,
    title: '详情',
  })
}
// 删除
const remove = (row: templateType) => {
  ElMessageBox.confirm(
    `确认删除${row.templateName}吗?`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      templateDelete({ id: (row.id as string) }).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getList()
        }
      })
    })
}
// 搜索
const search = () => {
  getList()
}
// 重置
const reset = () => {
  searchQuery.createTime = ''
  searchQuery.templateCreator = ''
  searchQuery.templateName = ''
  searchQuery.templateNo = ''
  getList()
}

// 新增
const add = () => {
  addRef.value.initDialog({
    templateType: searchQuery.templateType,
    title: '新增',
  })
}
// 下载
const exportSele = (row: templateType) => {
  exportExcel({
    json: [row].map((item: templateType, index: number) => ({ index: index + 1, templateNo: item.templateNo, templateName: item.templateName, templateCreator: item.templateCreator, createTime: item.createTime, templateDesc: item.templateDesc })),
    name: '证书报告',
    titleArr: ['序号', '模板编号', '模板名称', '模板负责人', '创建时间', '描述'],
    sheetName: 'sheet1',
  })
}
// 导出
const exportExcelBtn = () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  templateExport(searchQuery).then((res) => {
    exportFile(res.data, '证书报告模板')
    loading.close()
  }).catch((_) => {
    loading.close()
  })
}
// 打印
const printObj = ref({
  id: 'print', // 需要打印元素的id
  popTitle: '证书报告模板', // 打印配置页上方的标题
  extraHead: '', // 最上方的头部文字,附加在head标签上的额外标签,使用逗号分割
  preview: false, // 是否启动预览模式,默认是false
  previewBeforeOpenCallback() { console.log('正在加载预览窗口!') }, // 预览窗口打开之前的callback
  previewOpenCallback() { console.log('已经加载完预览窗口,预览打开了!') }, // 预览窗口打开时的callback
  beforeOpenCallback() { console.log('开始打印之前!') }, // 开始打印之前的callback
  openCallback() { console.log('执行打印了!') }, // 调用打印时的callback
  closeCallback() { console.log('关闭了打印工具!') }, // 关闭打印的callback(无法区分确认or取消)
  clickMounted() { console.log('点击v-print绑定的按钮了!') },
  // url: 'http://localhost:8080/', // 打印指定的URL,确保同源策略相同
  // asyncUrl (reslove) {
  //   setTimeout(() => {
  //     reslove('http://localhost:8080/')
  //   }, 2000)
  // },
  standard: '',
  extarCss: '',
})
onMounted(() => {
  getType().then((_) => {
    getList()
  })
})
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.templateNo" placeholder="编号" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.templateName" placeholder="模板名称" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.templateCreator" placeholder="模板负责人" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="searchQuery.createTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
          placeholder="创建时间"
        />
      </search-item>
      <!-- <template #btns>
        <el-button class="filter-item" type="info" :icon="Delete" :style="{ marginTop: '-10px' }" @click="reset">
          重置
        </el-button>
      </template> -->
    </search-area>
    <table-container>
      <!-- 表头区域 -->
      <template #btns-right>
        <el-button type="primary" @click="add">
          新建
        </el-button>
        <el-button type="primary" @click="exportExcelBtn">
          导出
        </el-button>
        <el-button v-print="printObj" type="primary">
          打印
        </el-button>
      </template>
      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }" :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="#" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="300">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="edit(row)">
                编辑
              </el-button>
              <el-button size="small" type="primary" link @click="detail(row)">
                详情
              </el-button>
              <el-button size="small" type="primary" link @click="exportSele(row)">
                下载
              </el-button>
              <el-button size="small" type="danger" link @click="remove(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <add-d-dialog ref="addRef" @reset-data="getList" />
  </app-container>
</template>

<style lang="scss" scoped>
.normal-input {
  width: 180px !important;
}

:deep(.el-table__header) {
  background-color: #bbb;
}
</style>