<!-- 计量资料列表 -->
<script name="TechnologyDataList" lang="ts" setup>
import dayjs from 'dayjs'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { IFileInfo, IListQuery } from './data-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { delFileMeter, getFileMeterList } from '@/api/resource/fileTechnology'

const { proxy } = getCurrentInstance() as any
const router = useRouter()

// 查询条件
const searchQuery = ref<IListQuery>({
  fileNo: '', // 文件编号
  fileName: '', // 文件名
  offset: 1,
  limit: 20,
})
const total = ref(0) // 数据条数
const loadingTable = ref(false) // 表格loading

// 表头
const columns = ref<TableColumn[]>([
  { text: '文件编号', value: 'fileNo', align: 'center', width: 300 },
  { text: '文件名称', value: 'fileName', align: 'center' },
  { text: '版本号', value: 'versionNo', align: 'center', width: 200 },
  { text: '创建时间', value: 'createTime', align: 'center', width: 200 },
])
const dataFileList = ref<Array<IFileInfo>>([]) // 表格数据

// 逻辑
// 跳转到新建的页面
const addMeterFile = () => {
  router.push({
    query: {
      type: 'create',
    },
    path: 'data/detail',
  })
}

// 详情
const detail = (row: IFileInfo) => {
  sessionStorage.setItem('fileInfo', JSON.stringify(row))
  router.push({
    query: {
      title: '计量资料',
    },
    path: 'data/stream',
  })
}

// 编辑
const updateById = (row: IFileInfo) => {
  sessionStorage.setItem('meterFileInfo', JSON.stringify(row))
  router.push({
    query: {
      type: 'update',
      id: row.id,
    },
    path: 'data/detail',
  })
}

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

const searchList = () => {
  fetchData(true)
}

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

// 重置
const reset = () => {
  searchQuery.value = {
    fileNo: '', // 文件编号
    fileName: '', // 文件名
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 删除
const deleteById = (row: IFileInfo) => {
  ElMessageBox.confirm(`是否删除文件 ${row.fileName}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delFileMeter({ id: row.id }).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`文件 ${row.fileName} 删除成功`)
        searchList()
      }
      else {
        ElMessage.error(`文件 ${row.fileName} 删除失败: ${res.message}`)
      }
    })
  })
}

onMounted(() => {
  searchList()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="searchList" @clear="reset">
      <search-item>
        <el-input v-model="searchQuery.fileNo" placeholder="文件编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.fileName" placeholder="文件名称" clearable />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container title="计量资料列表">
      <!-- 表头区域 -->
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm(`/resource/technology/data/add`)" icon="icon-add" title="新建" @click="addMeterFile" />
      </template>

      <!-- 表格区域 -->
      <normal-table
        :data="dataFileList" :total="total" :columns="columns"
        :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 fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" link @click="detail(row)">
                查看
              </el-button>
              <el-button v-if="proxy.hasPerm(`/resource/technology/data/edit`)" size="small" type="primary" link @click="updateById(row)">
                编辑
              </el-button>
              <el-button v-if="proxy.hasPerm(`/resource/technology/data/del`)" size="small" type="danger" link @click="deleteById(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

