<!-- 文件配置管理 -->
<script lang="ts" setup>
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import addDDialog from './addDDialog.vue'
import { printJSON } from '@/utils/printUtils'
import { deleteFileListPage, exportFileListPage, getFileListPage } from '@/api/system/tool'
import { exportFile } from '@/utils/exportUtils'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const { proxy } = getCurrentInstance() as any
const searchQuery = reactive<any>({
  fileName: '', // 文件名称
  moduleName: '', // 模块名
  downloadNo: '',
  limit: 20,
  offset: 1,
  ids: [],
}) // 查询参数
const loadingTable = ref<boolean>(false) // 表格loading
const total = ref<number>(0) // 数据总条数
const columns = ref<TableColumn[]>([
  { text: '文件编号', value: 'downloadNo', align: 'center', width: '160' },
  { text: '模块名称', value: 'moduleName', align: 'center' },
  { text: '业务类型', value: 'businessDownloadTypeName', align: 'center' },
  { text: '文件名称', value: 'fileName', align: 'center' },
  { text: '文件类型', value: 'fileTypeName', align: 'center' },
  // { text: '创建人', value: 'updateUser', align: 'center' },
  { text: '创建时间', value: 'updateTime', align: 'center', width: '180' },
])

// 表格
const list = ref([]) // 表格数据
const addRef = ref() // 添加/编辑/详情/组件
// 获取数据列表
const getList = () => {
  loadingTable.value = true
  getFileListPage(searchQuery).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
getList()
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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: any) => {
  addRef.value.initDialog({
    ...row,
    title: '编辑',
  })
}

// 详情
const detail = (row: any) => {
  addRef.value.initDialog({
    ...row,
    title: '详情',
  })
}
// 删除
const remove = (row: any) => {
  ElMessageBox.confirm(
    `确认删除${row.fileName}吗？`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      deleteFileListPage({ id: row.id as string }).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getList()
        }
      })
    })
}
// 搜索
const search = () => {
  getList()
}
// 重置
const reset = () => {
  searchQuery.fileName = ''
  searchQuery.moduleName = ''
  searchQuery.downloadNo = ''
  getList()
}

// 新增
const add = () => {
  addRef.value.initDialog({
    signType: searchQuery.signType,
    title: '新增',
  })
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (e: any) => {
  selectList.value = e.map((item: { id: string }) => item.id)
}
// 导出
const exportExcelBtn = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (list.value.length > 0) {
    const param = {
      fileName: searchQuery.fileName, // 文件名称
      moduleName: searchQuery.moduleName, // 模块名
      downloadNo: searchQuery.downloadNo,
      limit: 20,
      offset: 1,
      ids: selectList.value,
    }
    exportFileListPage(param).then((res) => {
      exportFile(res.data, '文件配置管理')
      loading.close()
      searchQuery.ids = []
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无数据可导出数据')
  }
  loading.close()
}
// 打印
function printList() {
  const selectIds = selectList.value.map((item: any) => item.id)
  // 打印列
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (selectIds.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '文件配置管理')
  }
  else if (selectIds.length > 0) {
    const printList = list.value.filter((item: any) => selectIds.includes(item.id))
    printJSON(printList, properties, '文件配置管理')
  }
  else {
    ElMessage('无可打印内容')
  }
}
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.downloadNo" placeholder="文件编号" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.moduleName" placeholder="模块名称" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.fileName" placeholder="文件名称" clearable class="w-50 m-2 normal-input" />
      </search-item>
    </search-area>
    <table-container>
      <!-- 表头区域 -->
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/sys/tool/autograph/add')" icon="icon-add" title="新建" @click="add" />
        <icon-button v-if="proxy.hasPerm('/sys/tool/autograph/export')" icon="icon-export" title="导出" @click="exportExcelBtn" />
        <icon-button v-if="proxy.hasPerm('/sys/tool/autograph/print')" icon="icon-print" title="打印" @click="printList" />
      </template>
      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }" :list-loading="loadingTable"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <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="130">
            <template #default="{ row }">
              <el-button v-if="proxy.hasPerm('/sys/tool/autograph/update')" size="small" type="primary" link @click="edit(row)">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm('/sys/tool/autograph/detail')" size="small" type="primary" link @click="detail(row)">
                详情
              </el-button>
              <el-button v-if="proxy.hasPerm('/sys/tool/autograph/delete')" 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>
