index.vue 3.9 KB
<script lang="ts" setup>
  import { nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { columns, searchFormSchema } from './config';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Popconfirm } from 'ant-design-vue';
  // import { Authority } from '/@/components/Authority';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { FormDrawer } from './components/FormDrawer';
  import { applicationApiPage, deleteApplicationApi } from '/@/api/application/api';
  import { authBtn } from '/@/enums/roleEnum';
  import { getAuthCache } from '/@/utils/auth';
  import { USER_INFO_KEY } from '/@/enums/cacheEnum';

  const { t } = useI18n();

  const userInfo: any = getAuthCache(USER_INFO_KEY);

  const role: string = userInfo.roles[0];

  const [registerTable, { setProps, reload }] = useTable({
    immediate: true,
    api: applicationApiPage,
    columns,
    showIndexColumn: false,
    clickToRowSelect: false,
    formConfig: {
      schemas: searchFormSchema,
      labelWidth: 100,
      baseColProps: { span: 8 },
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    rowKey: 'id',
    actionColumn: {
      width: 200,
      title: t('common.actionText'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  const handleSuccess = () => {
    reload();
    resetSelectedRowKeys();
  };

  const [registerApplicationApiFormDrawer, { openDrawer }] = useDrawer();

  const handleApplicationApiIsSuccess = () => {
    reload();
    resetSelectedRowKeys();
  };

  const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
    useBatchDelete(deleteApplicationApi, handleSuccess, setProps);

  nextTick(() => {
    setProps(selectionOptions);
  });

  const handleCreateOrEdit = (record: Recordable | null) => {
    const drawerData = {
      isUpdate: record ? 1 : 0,
      record,
    };
    openDrawer(true, drawerData);
  };

  const handleViewDetail = (record) => {
    openDrawer(true, {
      isUpdate: 2,
      record,
    });
  };
</script>

<template>
  <div>
    <BasicTable :clickToRowSelect="false" @register="registerTable">
      <template #toolbar>
        <!-- <Authority>
          <a-button type="primary" @click="handleCreateOrEdit(null)">
            {{ t('application.api.action.create') }}
          </a-button>
        </Authority> -->
        <Popconfirm
          :title="t('common.batchDeleteConfirmText')"
          @confirm="handleDeleteOrBatchDelete(null)"
        >
          <a-button color="error" :disabled="hasBatchDelete">
            {{ t('common.batchdelText') }}
          </a-button>
        </Popconfirm>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('common.detailText'),
              icon: 'ant-design:eye-outlined',
              onClick: handleViewDetail.bind(null, record),
            },
            {
              label: t('common.editText'),
              icon: 'clarity:note-edit-line',
              ifShow: authBtn(role) && (getAuthCache(USER_INFO_KEY) as any).tenantId === record.creator,
              onClick: handleCreateOrEdit.bind(null, record),
            },
            {
              label: t('common.delText'),
              icon: 'ant-design:delete-outlined',
              ifShow: authBtn(role) && (getAuthCache(USER_INFO_KEY) as any).tenantId === record.creator,
              color: 'error',
              popConfirm: {
                title: t('common.isDelete'),
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <FormDrawer
      @register="registerApplicationApiFormDrawer"
      @success="handleApplicationApiIsSuccess"
    />
  </div>
</template>