<script setup lang="ts" name="setting_updateManagement">
import { delVersion, getVersionList } from '#/api'
import Table from '#/components/Table/index.vue'
import XbTableClass from '#/utils/xbTable'

import column from './column.jsx'
import Dia from './dia.vue'

const tableRef = ref()
const showDia = ref(false)
const diaTitle = ref('')
const diaData = ref({})

const xbTable = reactive(
  new XbTableClass(
    {
      column: column({
        handleClick,
      }),
      page: {
        size: 20,
        current: 1,
        total: 0,
      },
      data: [],
    },
    {
      handlePageSize,
      handlePageCurrent,
    },
  ),
)

// 筛选功能
const filterData = ref()
// const config = computed(() => {
//   return []
// })
// const handleFilterData = (type: ACTION_TYPE) => {
//   switch (type.value) {
//     case 'reset': {
//       xbTable.table.page.current = 1
//       getTableData()
//       break
//     }
//     case 'search': {
//       getTableData()
//       break
//     }
//   }
// }

provide('xbTable', xbTable)

async function getTableData() {
  const res = await getVersionList({
    ...xbTable.table.page,
    ...filterData.value,
  })
  xbTable.table.data = res.rows
  xbTable.table.page.total = Number(res.totalRows)
}

// 分页处理
function handlePageSize({ size }: { size: number }) {
  xbTable.table.page.size = size
  getTableData()
}
function handlePageCurrent({ current }: { current: number }) {
  xbTable.table.page.current = current
  getTableData()
}

// async function updateState(row: anyObj) {
//     row.loading = true;
//     try {
//         let { data: res } = await changeDictTypeStatus({
//             id: row.id,
//             status: row.status === 0 ? 1 : 0,
//         });
//         return true;
//     } catch (err) {
//         return false;
//     } finally {
//         row.loading = false;
//     }
// }

function addDictType() {
  showDia.value = true
  diaData.value = {}
  diaTitle.value = '新增'
}

const tableHeight = computed(() => {
  if (tableRef.value && tableRef.value.getBox) {
    const { top, pageHeight } = tableRef.value!.getBox()
    return `${window.innerHeight - top - pageHeight}px`
  }
  return '600px'
})

async function handleClick(data: anyObj) {
  await delVersion(data.id)
  getTableData()
}

onMounted(() => {
  getTableData()
})
</script>
<template>
  <section class="page g-container">
    <!-- <Filter v-model="filterData" :config="config" @handleAction="handleFilterData" /> -->
    <Table ref="tableRef" class="page-table" :height="tableHeight">
      <template #neck>
        <ElButton type="primary" class="add-btn" @click="addDictType">
          新增版本
        </ElButton>
      </template>
    </Table>
    <Dia
      v-model:show-dia="showDia"
      :title="diaTitle"
      :dia-data="diaData"
      @on-refresh="getTableData"
    />
  </section>
</template>
<style lang="scss" scoped>
@include xbj_table;

.add-btn {
  width: 94px;
  padding: 0;
  margin-bottom: 16px;
}
</style>
