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

import { useRouter } from 'vue-router'

import { deleteWordClass, getWordClassList, updateWordClass } from '#/api'
import { generateQuickDate } from '#/utils/tool'
import XbTableClass from '#/utils/xbTable'

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

const tableRef = ref()
const router = useRouter()
const xbTable = reactive(
  new XbTableClass(
    {
      column: column({ updateState, handleClick }),
      page: {
        size: 20,
        current: 1,
        total: 0,
      },
      data: [],
    },
    {
      handlePageSize,
      handlePageCurrent,
    },
  ),
)
provide('xbTable', xbTable)
async function init() {
  const res = await getWordClassList({
    ...xbTable.table.page,
    ...filterData.value,
    date: null,
  })
  xbTable.table.page.total = res.totalRows
  xbTable.table.data = res.rows
}
// 过滤
const handleFilterData = (type: ACTION_TYPE) => {
  switch (type.value) {
    case 'reset': {
      xbTable.table.page.current = 1
      init()
      break
    }
    case 'search': {
      init()
      break
    }
  }
}
// 筛选功能
const filterData = ref()
const config = computed(() => {
  return [
    {
      comName: 'el-input',
      label: '词汇名称',
      placeholder: '请输入名称',
      prop: 'name',
      clearable: true,
    },
    {
      comName: 'el-date-picker',
      label: '时间',
      prop: 'date',
      type: 'daterange',
      shortcuts: generateQuickDate(),
      unlinkPanels: true,
      rangeSeparator: '~',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      propFormat: ['startTime', 'endTime'],
    },
  ]
})
async function updateState(row: anyObj) {
  row.loading = true
  try {
    let val = 0
    await updateWordClass({
      id: row.id,
      status: row.status === 0 ? (val = 1) : (val = 0),
    })
    row.status = val
  } catch {
    return false
  } finally {
    row.loading = false
  }
}
// 分页处理
function handlePageSize({ size }: { size: number }) {
  xbTable.table.page.size = size
  init()
}
function handlePageCurrent({ current }: { current: number }) {
  xbTable.table.page.current = current
  init()
}

const showDia = ref(false)
const diaTitle = ref('新增单词')
const diaData = ref<anyObj>({})
function handleClick(val: anyObj, type = 0) {
  switch (type) {
    case 0: {
      router.push(`/word-mgr/word-list/${val.id}`)
      break
    }
    case 1: {
      deleteWordClass(val.id).then(() => {
        xbTable.table.page.current = 1
        init()
      })
      break
    }
  }
}

function handleAdd() {
  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'
})

onMounted(() => {
  init()
})
</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="showDia"
      :title="diaTitle"
      :dia-data="diaData"
      @on-refresh="handlePageCurrent({ current: 1 })"
    />
  </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>
