<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import { getSysOrganListApi, delSysOrganListApi, getSysOrgType } from '@/api/param/SysOrgan'
import { TableData, ListItem } from '@/api/param/SysOrgan/types'
import { useTable } from '@/hooks/web/useTable'
import { Table, TableColumn } from '@/components/Table'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { ContentWrap } from '@/components/ContentWrap'
import { transfDictList, findListLabel } from '@/utils'

const ids = ref<string[]>([])

/** 机构类型 */
const ORGAN_TYPE_LIST = ref<ListItem[]>([])

const { tableRegister, tableState, tableMethods } = useTable({
  fetchInitParamsApi: async () => {
    const res = await getSysOrgType()
    if (res) {
      ORGAN_TYPE_LIST.value = transfDictList(res.body.result)
    }
  },
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getSysOrganListApi({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.body.list,
      total: res.body.total
    }
  },
  fetchDelApi: async () => {
    const res = await delSysOrganListApi(unref(ids))
    return !!res
  }
})

const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, refresh } = tableMethods

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'selection',
    type: 'selection'
  },
  {
    field: 'code',
    label: '机构号',
    width: 120
  },
  {
    field: 'orgname',
    label: '机构名称'
  },
  {
    field: 'orgLeveType',
    label: '类型',
    width: 120,
    formatter: (row: Recordable, __: TableColumn) => {
      const label = findListLabel(unref(ORGAN_TYPE_LIST), row.orgLeveType)
      if (label) {
        return label
      }
    }
  },
  {
    field: 'startdate',
    label: '开始日期',
    width: 200
  },
  {
    field: 'enddate',
    label: '结束日期',
    width: 200
  }
])

const searchSchema = reactive<FormSchema[]>([
  {
    field: 'orgname',
    label: '机构名称',
    component: 'Input'
  },
  {
    field: 'code',
    label: '机构号',
    component: 'Input'
  }
])

const searchParams = ref({})
const setSearchParams = (data: any) => {
  searchParams.value = data
  getList()
}

const disabled = ref(true)

const onSelectionChange = (selection: TableData[]) => {
  disabled.value = selection.length === 0
}

// const getSelections = async () => {
//   const elTableRef = await getElTableExpose()
//   const selections = elTableRef?.getSelectionRows()
//   return selections
// }

const submit = async () => {
  const elTableRef = await getElTableExpose()
  const selections = elTableRef?.getSelectionRows()
  return selections
}

defineExpose({
  submit
})
</script>

<template>
  <ContentWrap>
    <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />

    <Table
      :columns="tableColumns"
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total
      }"
      @selection-change="onSelectionChange"
      @register="tableRegister"
      @refresh="refresh"
      :show-action="false"
    />
  </ContentWrap>
</template>
