index.vue 4.88 KB
<template>
  <div>
    <PageWrapper dense contentFullHeight contentClass="flex">
      <OrganizationIdTree
        class="w-1/4 xl:w-1/5"
        @select="handleSelect"
        ref="organizationIdTreeRef"
      />
      <BasicTable
        :clickToRowSelect="false"
        @register="registerTable"
        :searchInfo="searchInfo"
        class="w-3/4 xl:w-4/5"
      >
        <template #toolbar>
          <a-button type="primary" @click="handleCreateOrEdit(null)"> 新增告警联系人 </a-button>
          <a-button
            type="primary"
            color="error"
            @click="handleDeleteOrBatchDelete(null)"
            :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" />
  </div>
</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 { useResetOrganizationTree, OrganizationIdTree } from '/@/views/common/organizationIdTree';

  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;
      };
      const searchInfo = reactive<Recordable>({});
      const { organizationIdTreeRef, resetFn } = useResetOrganizationTree(searchInfo);
      // 表格hooks
      const [registerTable, { reload }] = useTable({
        title: '告警联系人列表',
        api: getAlarmContact,
        columns,
        clickToRowSelect: false,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          resetFunc: resetFn,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        rowSelection: {
          onChange: onSelectRowChange,
          type: 'checkbox',
        },
        rowKey: 'id',
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      // 弹框
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { createMessage } = useMessage();

      // 刷新
      const handleSuccess = () => {
        reload();
      };
      // 新增或编辑
      const handleCreateOrEdit = (record: Recordable | null) => {
        if (record) {
          openDrawer(true, {
            isUpdate: true,
            record,
          });
        } else {
          openDrawer(true, {
            isUpdate: false,
          });
        }
      };
      // 删除或批量删除
      const handleDeleteOrBatchDelete = async (record: Recordable | null) => {
        if (record) {
          try {
            await deleteAlarmContact([record.id]);
            createMessage.success('删除联系人成功');
            handleSuccess();
          } catch (e) {}
        } else {
          try {
            await deleteAlarmContact(selectedRowIds.value);
            createMessage.success('批量删除联系人成功');
            selectedRowIds.value = [];
            handleSuccess();
          } catch (e) {}
        }
      };

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