<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <DeptTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :searchInfo="searchInfo">
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleCreate">新增账号</a-button> -->
        <PermissionBtn @btnEvent="onBtnClicked" />
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
    <AssignRoleModal @register="registerRoleModal" @success="handleSuccess" />
    <OrgPositonModal @register="registerOrgPositonModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';

  import { BasicTable, useTable } from '@/components/Table';
  import { getAccountList, deleteAccount } from '@/api/demo/system';
  import { PageWrapper } from '@/components/Page';
  import { useModal } from '@/components/Modal';
  import { DeptTree, AccountModal, AssignRoleModal, OrgPositonModal } from './page';
  // import { AccountModal } from './page';
  // import { AssignRoleModal } from './page';
  // import { OrgPositonModal } from './page';

  import { columns, searchFormSchema } from './account.data';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage, createConfirm } = useMessage();
  defineOptions({ name: 'AccountManagement' });

  const [registerModal, { openModal: openAccountModal }] = useModal();
  const [registerRoleModal, { openModal: openRoleModal }] = useModal();
  const [registerOrgPositonModal, { openModal: openOrgPositonModal }] = useModal();
  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '账号列表',
    api: getAccountList,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    rowSelection: {
      //多选框
      // type: 'checkbox',
      type: 'radio',
    },
    useSearchForm: true,
    showIndexColumn: false,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
  });

  function handleCreate() {
    openAccountModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个账号进行编辑');
    }
    const record = rows[0];
    openAccountModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选一个账号进行删除');
    }
    const query = [rows[0].id];
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前账号吗',
      onOk: async () => {
        const data = await deleteAccount(query);
        if (data) {
          handleSuccess();
          return createMessage.success('删除成功');
        } else {
          return createMessage.error('删除失败');
        }
      },
    });
  }

  function handleSuccess() {
    clearSelectedRowKeys();
    reload();
  }

  function handleSelect(orgId = '') {
    searchInfo.orgId = orgId;
    reload();
  }

  function onBtnClicked(domId) {
    let rows = getSelectRows();
    const record = rows[0];
    switch (domId) {
      case 'btnAdd':
        handleCreate();
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      case 'btnAssignRoles':
        if (rows.length == 0) {
          return createMessage.warn('请勾选一个账号分配角色');
        }
        openRoleModal(true, {
          record,
          isUpdate: true,
        });
        break;
      case 'btnOrgPositon':
        if (rows.length == 0) {
          return createMessage.warn('请勾选一个账号分配部门和职级');
        }
        openOrgPositonModal(true, {
          record,
          isUpdate: true,
        });
        break;
      default:
        break;
    }
  }
</script>
./page.ts
