index.vue 6.2 KB
<template>
  <div>
    <BasicTable :clickToRowSelect="false" @register="registerTable">
      <template #toolbar>
        <Authority :value="PermissionReportConfigEnum.PERMISSION_POST">
          <a-button
            type="primary"
            @click="handleBussinessDrawer(BusinessReportConfigTextEnum.BUSINESS_ADD_TEXT, null)"
          >
            {{ BusinessReportConfigTextEnum.BUSINESS_ADD_TEXT }}
          </a-button>
        </Authority>
        <Authority :value="PermissionReportConfigEnum.PERMISSION_GET">
          <a-button type="primary" @click="go('/report/export')">
            {{ BusinessReportConfigTextEnum.BUSINESS_EXPORT_TEXT }}
          </a-button>
        </Authority>
        <Authority :value="PermissionReportConfigEnum.PERMISSION_DELETE">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button type="primary" color="error" :disabled="hasBatchDelete">
              {{ BusinessReportConfigTextEnum.BUSINESS_DELETE_TEXT }}
            </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <template #doDeviceSlot="{ record }">
        <a-button type="text" @click="handleDeviceView(record)">
          <span style="color: #377dff">{{
            BusinessReportConfigTextEnum.BUSINESS_VIEW_DEVICE_TEXT
          }}</span>
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: BusinessReportConfigTextEnum.BUSINESS_VIEW_TEXT.slice(0, 2),
              icon: 'ant-design:eye-outlined',
              onClick: handleBussinessDrawer.bind(
                null,
                BusinessReportConfigTextEnum.BUSINESS_VIEW_TEXT,
                record
              ),
              ifShow: record.status === 1,
            },
            {
              label: BusinessReportConfigTextEnum.BUSINESS_UPDATE_TEXT.slice(0, 2),
              icon: 'clarity:note-edit-line',
              auth: PermissionReportConfigEnum.PERMISSION_UPDATE,
              onClick: handleBussinessDrawer.bind(
                null,
                BusinessReportConfigTextEnum.BUSINESS_UPDATE_TEXT,
                record
              ),
              ifShow: record.status === 0,
            },
            {
              label: BusinessReportConfigTextEnum.BUSINESS_DELETE_TEXT.slice(2),
              icon: 'ant-design:delete-outlined',
              auth: PermissionReportConfigEnum.PERMISSION_DELETE,
              color: 'error',
              ifShow: record.status === 0,
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
      <template #configStatus="{ record }">
        <Switch
          :disabled="disabledSwitch"
          :checked="record.status === 1"
          :loading="record.pendingStatus"
          :checkedChildren="BusinessReportConfigTextEnum.BUSINESS_ENABLE_TEXT"
          :unCheckedChildren="BusinessReportConfigTextEnum.BUSINESS_DISABLE_TEXT"
          @change="(checked: boolean) => statusChange(checked, record)"
        />
      </template>
    </BasicTable>
    <ReportConfigDrawer @register="registerDrawer" @success="handleSuccess" />
    <DevicePreviewModal @register="registerModal" />
  </div>
</template>

<script lang="ts" setup>
  import { nextTick, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import {
    reportPage,
    deleteReportManage,
    putReportByidAndStatusManage,
  } from '/@/api/report/reportManager';
  import { defaultTableAttribtes } from './config';
  import { Authority } from '/@/components/Authority';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Popconfirm, Switch } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import { useGo } from '/@/hooks/web/usePage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { ReportConfigDrawer, DevicePreviewModal } from './components';
  import {
    PermissionReportConfigEnum,
    BusinessReportConfigTextEnum,
    BusinessReportConfigStatusEnum,
  } from './enum';

  const disabledSwitch = ref(false);

  const [registerTable, { reload, setProps, setSelectedRowKeys }] = useTable({
    api: reportPage,
    ...defaultTableAttribtes,
  });

  // 业务弹窗
  const [registerDrawer, { openDrawer }] = useDrawer();

  const { createMessage } = useMessage();

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

  const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
    useBatchDelete(deleteReportManage, handleSuccess, setProps) as any;
  selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
    //业务 status为1 选择框则禁用
    if (record.status === BusinessReportConfigStatusEnum.BUSINESS_DISABLE)
      return { disabled: true };
    else return { disabled: false };
  };

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

  // 业务弹窗
  const handleBussinessDrawer = (text, record) => {
    const modalParams = {
      text,
      record,
    };
    openDrawer(true, modalParams);
  };

  //查看设备
  const [registerModal, { openModal }] = useModal();

  const handleDeviceView = (record) => {
    openModal(true, {
      isUpdate: true,
      record,
    });
  };

  const setPropsLoading = (loading) => {
    setProps({
      loading,
    });
    setSelectedRowKeys([]);
    resetSelectedRowKeys();
  };

  const statusChange = async (checked, record) => {
    try {
      setPropsLoading(true);
      disabledSwitch.value = true;
      const newStatus = checked ? 1 : 0;
      const res = await putReportByidAndStatusManage(record.id, newStatus);
      if (res && newStatus)
        createMessage.success(`${BusinessReportConfigTextEnum.BUSINESS_ENABLE_TEXT}成功`);
      else createMessage.success(`${BusinessReportConfigTextEnum.BUSINESS_DISABLE_TEXT}成功`);
    } finally {
      setPropsLoading(false);
      disabledSwitch.value = false;
      reload();
    }
  };
  const go = useGo();
</script>