<script lang="ts" setup>
import { Space } from 'ant-design-vue'
import { PlusOutlined } from '@ant-design/icons-vue'
import DedailModal from './DedailModal.vue'
import TenantModal from './TenantFormModal.vue'
import AuthSettingModal from './AuthSettingModal.vue'
import { columns, searchFormSchema } from './data'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { deleteTenant, getTenantList } from '@/api/system/tenant'
import type { Tenant } from '@/api/system/tenant/types'
import { useModal } from '@/components/Modal'
import { useMessage } from '@/hooks/web/useMessage'

// import { usePermission } from '@/hooks/web/usePermission'

defineOptions({ name: 'SystemTenant' })

const [registerModal, { openModal }] = useModal<Tenant>()
const [registerDetailModal, { openModal: openDetailModal }] = useModal<Tenant>()
const [registerAuthSettingModal, { openModal: openAuthSettingModal }] = useModal<Tenant>()

// const { hasPermission } = usePermission()

const [registerTable, { reload }] = useTable({
  api: getTenantList,
  columns,
  formConfig: {
    labelWidth: 100,
    schemas: searchFormSchema,
    actionColOptions: { span: 4 },
  },
  bordered: true,
  canResize: false,
  useSearchForm: true,
  actionColumn: {
    width: 300,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    // auth: ['tenant_delete', 'tenant_edit'],
  },
})

async function handleDelete(id: string) {
  try {
    await deleteTenant(id)
    useMessage().createMessage.success('删除成功！')
    reload()
  }
  catch {}
}
</script>

<template>
  <div>
    <BasicTable :api="async () => ([] as Tenant[])" @register="registerTable">
      <template #tableTitle>
        <Space>
          <a-button type="primary" @click="openModal">
            <PlusOutlined />
            新建
          </a-button>
        </Space>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'i-ant-design:eye-outlined',
                label: '查看',
                // auth: 'tenant_edit',
                onClick: () => openDetailModal(true, record),
              },
              {
                icon: 'i-ant-design:edit-outlined',
                label: '编辑',
                // auth: 'tenant_edit',
                onClick: () => openModal(true, record),
              },
              {
                icon: 'i-ant-design:setting-outlined',
                label: '授权配置',
                // auth: 'tenant_edit',
                onClick: () => openAuthSettingModal(true, record),
              },
              {
                icon: 'i-ant-design:delete-outlined',
                label: '删除',
                danger: true,
                // auth: 'tenant_delete',
                popConfirm: {
                  title: '确定要删除数据吗？',
                  placement: 'left',
                  confirm: () => handleDelete(record.id),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>

    <TenantModal @register="registerModal" @success="reload()" />
    <DedailModal @register="registerDetailModal" @success="reload()" />
    <AuthSettingModal @register="registerAuthSettingModal" @success="reload()" />
  </div>
</template>
