<!-- 文档管理列表 -->
<script lang="ts" setup name="DocumentList">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { delDocument, getListPage } from '@/api/eqpt/document/index'
import { urlToBlob } from '@/utils/download'
import { getPhotoUrl } from '@/api/system/tool'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  fileNo: '',
  fileName: '',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '文件号',
    value: 'fileNo',
    align: 'center',
  },
  {
    text: '文件名称',
    value: 'fileName',
    align: 'center',
  },
  {
    text: '类别',
    value: 'fileTypeName',
    align: 'center',
  },
  {
    text: '发布人',
    value: 'createUserName',
    align: 'center',
  },
  {
    text: '发布时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '实施状态',
    value: 'implementationStatusName',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  // getListPage(listQuery).then((response) => {
  //   list.value = response.data.rows
  //   total.value = parseInt(response.data.total)
  listLoading.value = false
  // })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  listQuery.fileName = ''
  listQuery.fileNo = ''
  listQuery.offset = 1
  listQuery.limit = 20
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
const $router = useRouter()
// 新建编辑操作
const handler = (row: any, type: string) => {
  $router.push({
    path: `/documentlist/${type}`,
    query: {
      row: JSON.stringify(row),
      id: row.id,
    },
  })
}
// 删除
const delHandler = (row: any) => {
  ElMessageBox.confirm(
    '确认删除此文档信息吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delDocument(row.id).then((res) => {
      ElMessage.success('操作成功')
      // close()
      search()
    })
  })
}
// 下载
const download = (row: any) => {
  // 获取文件地址
  // downloadFile
  getPhotoUrl(row.attachment).then((res) => {
    urlToBlob(res.data, row.fileName)
  })
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.fileNo" placeholder="文件号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.fileName" placeholder="文件名称" clearable />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="handler({}, 'create')" />
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" @change="changePage"
      >
        <template #columns>
          <el-table-column label="操作" width="160" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handler(scope.row, 'detail')">
                查看
              </el-button>
              <el-button link type="primary" size="small" @click="handler(scope.row, 'update')">
                编辑
              </el-button>
              <el-button link type="danger" size="small" @click="delHandler(scope.row)">
                删除
              </el-button>
              <el-button link type="primary" size="small" @click="download(scope.row)">
                下载
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

