<script lang="ts" setup>
import { Space } from 'ant-design-vue'
import { DeleteOutlined, PlusOutlined } from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import { columns, searchFormSchema } from './data'
import DictFormModal from './DictFormModal.vue'
import { useMessage } from '@/hooks/web/useMessage'
import { useModal } from '@/components/Modal'
import { BasicTable, TableAction, useTable } from '@/components/Table'
import { deleteDict, getDictList } from '@/api/system/dict'
import type { SystemDict } from '@/api/system/dict/types'

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

defineOptions({ name: 'SystemDict' })

const router = useRouter()
const { createMessage, createConfirm } = useMessage()
const [registerModal, { openModal }] = useModal<SystemDict>()

// const { hasPermission } = usePermission()

const [registerTable, { reload, getSelectRowKeys, clearSelectedRowKeys }] = useTable({
  api(params) {
    return getDictList(params)
  },
  columns,
  formConfig: {
    labelWidth: 80,
    schemas: searchFormSchema,
  },
  rowKey: 'id',
  rowSelection: {},
  useSearchForm: true,
  bordered: true,
  canResize: false,
  actionColumn: {
    width: 160,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    // auth: ['user_edit', 'user_delete'],
  },
})

async function handleDelete(id?: string) {
  const ids = id ? [id] : (getSelectRowKeys() as string[])
  if (!ids.length)
    return createMessage.warning('请选择要删除的数据')

  async function execute() {
    try {
      await deleteDict(ids)
      createMessage.success('删除成功')
      reload()
      clearSelectedRowKeys()
    }
    catch {}
  }

  if (id)
    return execute()

  createConfirm({
    iconType: 'warning',
    title: '是否要删除选择的字典？',
    onOk: execute,
  })
}

function toPath(data: SystemDict) {
  router.push({
    path: `/system/dict/setting/${data.id}`,
  })
}
</script>

<template>
  <div>
    <BasicTable :api="async () => ([] as SystemDict[])" @register="registerTable">
      <template #tableTitle>
        <Space>
          <a-button type="primary" @click="openModal">
            <PlusOutlined />
            新增
          </a-button>
          <a-button danger @click="handleDelete()">
            <DeleteOutlined />
            批量删除
          </a-button>
        </Space>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'i-ant-design:edit-outlined',
                label: '修改',
                onClick: () => openModal(true, record),
                // auth: 'user_edit',
              },
              {
                icon: 'i-ant-design:setting-outlined',
                label: '字典配置',
                // auth: 'user_delete',
                onClick: () => toPath(record),
              },
              {
                icon: 'i-ant-design:delete-outlined',
                danger: true,
                label: '删除',
                // auth: 'user_delete',
                popConfirm: {
                  title: '确定要删除数据吗？',
                  placement: 'left',
                  confirm: () => handleDelete(record.id),
                },
              },

            ]"
          />
        </template>
      </template>
    </BasicTable>

    <DictFormModal @register="registerModal" @success="reload()" />
  </div>
</template>
