<script lang="ts" setup>
import CompanyModal from './CompanyModal.vue'
import { columns } from './Company'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useModal } from '@/components/Modal'
import { IconEnum } from '@/enums/appEnum'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { deleteCompany, getCompanyPage } from '@/api/system/config/Company'

defineOptions({ name: 'UnitCompany' })

const { t } = useI18n()
const { createMessage } = useMessage()
const [registerModal, { openModal }] = useModal()

// 移除了 expandAll, collapseAll 相关的功能
const [register, { getForm, reload }] = useTable({
  title: '集团列表',
  api: getList,
  columns,
  rowKey: 'id',
  formConfig: { labelWidth: 120 },
  pagination: true,
  useSearchForm: false,
  showTableSetting: true,
  showIndexColumn: false,
  actionColumn: {
    width: 140,
    title: t('common.action'),
    dataIndex: 'action',
    fixed: 'right',
  },
})

async function getList() {
  const res = await getCompanyPage(getForm().getFieldsValue() as any)
  return res
}

function handleCreate() {
  openModal(true, { isUpdate: false })
}

function handleEdit(record: Recordable) {
  openModal(true, { record, isUpdate: true })
}

async function handleDelete(record: Recordable) {
  await deleteCompany(record.id)
  createMessage.success(t('common.delSuccessText'))
  reload()
}
</script>

<template>
  <div style="height: calc(100vh - 200px); overflow: auto;">
    <BasicTable @register="register">
      <template #toolbar>
        <a-button v-auth="['system:company:create']" type="primary" :pre-icon="IconEnum.ADD" @click="handleCreate">
          {{ t('action.create') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              { icon: IconEnum.EDIT, label: t('action.edit'), auth: 'system:company:update', onClick: handleEdit.bind(null, record) },
              {
                icon: IconEnum.DELETE,
                danger: true,
                label: t('action.delete'),
                auth: 'system:company:delete',
                popConfirm: {
                  title: t('common.delMessage'),
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <CompanyModal @register="registerModal" @success="reload()" />
  </div>
</template>

<style scoped>
/* 深度选择器覆盖 Ant Design 内部样式 */
:deep(.ant-table-body) {
  height: auto !important;
  min-height: 200px;
  max-height: 300 !important;
}
</style>
