index.vue 4.66 KB
<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <OrganizationIdTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable @register="registerTable" :searchInfo="searchInfo" class="w-3/4 xl:w-4/5">
      <template #toolbar>
        <a-button type="primary" @click="handleCreateOrEdit"> 新增联系人 </a-button>
        <a-button
          type="primary"
          color="error"
          @click="handleDeleteOrBatchDelete"
          :disabled="hasBatchDelete"
        >
          批量删除
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleCreateOrEdit.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </PageWrapper>
  <ContactDrawer @register="registerDrawer" @success="handleSuccess" />
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, computed } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useDrawer } from '/@/components/Drawer';
  import ContactDrawer from './ContactDrawer.vue';
  import OrganizationIdTree from '../../common/OrganizationIdTree.vue';
  import { Modal } from 'ant-design-vue';
  import { getAlarmContact, deleteAlarmContact } from '/@/api/alarm/contact/alarmContact';
  import { searchFormSchema, columns } from './config.data';
  export default defineComponent({
    components: {
      PageWrapper,
      OrganizationIdTree,
      BasicTable,
      TableAction,
      ContactDrawer,
    },
    setup() {
      let selectedRowIds = ref<string[]>([]);
      const hasBatchDelete = computed(() => selectedRowIds.value.length <= 0);
      // 复选框事件
      const onSelectRowChange = (selectedRowKeys: string[]) => {
        selectedRowIds.value = selectedRowKeys;
      };
      // 表格hooks
      const [registerTable, { reload }] = useTable({
        api: getAlarmContact,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        rowSelection: {
          onChange: onSelectRowChange,
          type: 'checkbox',
        },
        rowKey: 'id',
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      // 弹框
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { createMessage } = useMessage();
      const searchInfo = reactive<Recordable>({});
      // 刷新
      const handleSuccess = () => {
        reload();
      };
      // 新增或编辑
      const handleCreateOrEdit = (record: Recordable) => {
        if (record) {
          openDrawer(true, {
            isUpdate: true,
            record,
          });
        } else {
          openDrawer(true, {
            isUpdate: false,
          });
        }
      };
      // 删除或批量删除
      const handleDeleteOrBatchDelete = async (record?: Recordable) => {
        if (record) {
          try {
            await deleteAlarmContact([record.id]);
            createMessage.success('删除联系人成功');
            handleSuccess();
          } catch (e) {
            createMessage.error('删除失败');
          }
        } else {
          try {
            await Modal.confirm({
              title: '警告',
              content: '是否删除勾选的信息?',
            });
            await deleteAlarmContact(selectedRowIds.value);
            createMessage.success('批量删除联系人成功');
            handleSuccess();
          } catch (e) {
            createMessage.info('取消删除');
          }
        }
      };

      // 树形选择器
      const handleSelect = (organizationId: string) => {
        searchInfo.organizationId = organizationId;
        handleSuccess();
      };
      return {
        searchInfo,
        hasBatchDelete,
        handleCreateOrEdit,
        handleDeleteOrBatchDelete,
        handleSelect,
        handleSuccess,
        registerTable,
        registerDrawer,
      };
    },
  });
</script>