<script lang="ts" setup name="checkpointManage_levelEditing">
import type { ACTION_TYPE } from '#/components/Filter'

import { deleteWords, getStageList, getSynonymWordList } from '#/api'
import XbTableClass from '#/utils/xbTable'

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

const handleClick = (val: anyObj, type = 0) => {
  switch (type) {
    case 0: {
      showWordDia.value = true
      diaData.value = val
      break
    }
    case 1: {
      deleteWords(val.id).then(() => {
        xbTable.table.page.current = 1
        init()
      })
      break
    }
  }
}

// 分页处理
const handlePageSize = ({ size }: { size: number }) => {
  xbTable.table.page.size = size
  init()
}
const handlePageCurrent = ({ current }: { current: number }) => {
  xbTable.table.page.current = current
  init()
}
const init = async () => {
  const res = await getSynonymWordList({
    ...xbTable.table.page,
    ...filterData.value,
    type: 3,
  })
  xbTable.table.page.total = res.totalRows
  xbTable.table.data = res.rows
}

const xbTable = reactive(
  new XbTableClass(
    {
      column: column({ handleClick }),
      page: {
        size: 20,
        current: 1,
        total: 0,
      },
      data: [],
    },
    {
      handlePageSize,
      handlePageCurrent,
    },
  ),
)
provide('xbTable', xbTable)
// async function updateState(row: anyObj) {
//     row.loading = true;
//     try {
//         let val = 0;
//         let { data: res } = await updateLevel({
//             id: row.id,
//             status: row.status === 0 ? (val = 1) : (val = 0),
//         });
//         row.status = val;
//     } catch (err) {
//         return false;
//     } finally {
//         row.loading = false;
//     }
// }
const showWordDia = ref(false)
const diaData = ref<anyObj>({})
const handleAdd = () => {
  diaData.value = {}
  showWordDia.value = true
}

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

// 筛选功能
const filterData = ref({
  queryWordContent: '',
})
const config = computed(() => {
  return [
    {
      comName: 'el-input',
      label: '',
      prop: 'queryWordContent',
      clearable: true,
    },
  ]
})
// 过滤
const handleFilterData = (type: ACTION_TYPE) => {
  switch (type.value) {
    case 'reset': {
      xbTable.table.page.current = 1
      init()
      break
    }
    case 'search': {
      init()
      break
    }
  }
}
const stageListOptions = ref([])

onMounted(() => {
  init()
  getStageList({ current: 1, size: 1000, status: 0 }).then((res) => {
    stageListOptions.value = res.rows
  })
  document.addEventListener('visibilitychange', () => {
    if (document.visibilityState === 'visible') {
      handlePageCurrent({ current: Number(xbTable.table.page.current) })
    }
  })
})
onActivated(() => {
  handlePageCurrent({ current: Number(xbTable.table.page.current) })
})
</script>

<template>
  <div class="page g-container">
    <Filter
      v-model="filterData"
      :config="config"
      @handle-action="handleFilterData"
    />
    <Table ref="tableRef" class="page-table" :height="tableHeight">
      <template #neck>
        <div class="btn">
          <el-button type="primary" @click="handleAdd"> 新增 </el-button>
        </div>
      </template>
    </Table>

    <Dia
      v-model="showWordDia"
      :dia-data="diaData"
      @on-refresh="
        handlePageCurrent({ current: Number(xbTable.table.page.current) })
      "
    />
  </div>
</template>

<style lang="scss" scoped>
@include xbj_table;
@include cell_user;

.page {
  .btn {
    margin-bottom: $box-mt;

    .el-button {
      padding: 6px 8px;
    }

    .icon-add {
      margin-right: 6px;
    }
  }
}
</style>
<style lang="scss">
.texts {
  color: red;
  text-decoration: none;
}

.wordImgPopover {
  position: fixed !important;
  inset: 0 50% 0 0 !important;

  .image-grid {
    display: grid;
    grid-template-rows: repeat(2, 1fr);
    grid-template-columns: repeat(2, 1fr);
    gap: 4px;
    width: 100%;
    height: 100%;

    .box-card {
      box-sizing: border-box;
      width: 100%;
      height: 100%;
    }

    .el-card__body {
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
    }

    .img {
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: center;
      max-height: 317px;
      overflow: hidden;

      .el-image {
        width: 100%;
        height: 100%;
      }

      .image-slot {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;
        font-size: 30px;
        color: var(--el-text-color-secondary);
        background: var(--el-fill-color-light);
      }
    }

    .footer {
      box-sizing: border-box;
      flex-shrink: 0;
      margin-top: 20px;
      font-size: 24px;
      text-align: center;
    }
  }
}
</style>
