index.vue 5.69 KB
<template>
  <div>
    <BasicTable
      @selection-change="useSelectionChange"
      @register="registerTable"
      :rowSelection="{ type: 'checkbox' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增设备配置 </a-button>
        <ImpExcel @success="loadDataSuccess" dateFormat="YYYY-MM-DD">
          <a-button type="info" @click="handleImport"> 导入设备配置 </a-button>
        </ImpExcel>
        <a-button type="danger" @click="handleTableDel"> 删除 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '详情',
              icon: 'clarity:note-edit-line',
              onClick: handleDetailView.bind(null, record),
            },
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '导出',
              icon: 'clarity:note-edit-line',
              onClick: handleExport.bind(null, record),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DeviceProfileModal v-if="isJudgeStatus" @register="registerModal" @success="handleSuccess" />
    <DeviceConfigDetail v-if="!isJudgeStatus" @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './device.profile.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { deviceConfigGetQuery, deviceConfigDelete } from '/@/api/device/deviceConfigApi';
  import { useModal } from '/@/components/Modal';
  import DeviceProfileModal from '/@/views/device/profile/DeviceProfileModal.vue';
  import DeviceConfigDetail from '/@/views/device/profile/deviceConfigDetail.vue';
  // import { aoaToSheetXlsx } from '/@/components/Excel';
  import { ImpExcel, ExcelData } from '/@/components/Excel';

  export default defineComponent({
    name: 'DeviceProfileManagement',
    components: { BasicTable, DeviceProfileModal, TableAction, ImpExcel, DeviceConfigDetail },
    setup() {
      let selectedRowKeys: string[] = reactive([]);
      let isJudgeStatus = ref(true);
      const { createMessage } = useMessage();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '设备配置列表',
        clickToRowSelect: false,
        api: deviceConfigGetQuery,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      const tableListRef = ref<
        {
          title: string;
          columns?: any[];
          dataSource?: any[];
        }[]
      >([]);

      function loadDataSuccess(excelDataList: ExcelData[]) {
        tableListRef.value = [];
        console.log(excelDataList);
        for (const excelData of excelDataList) {
          const {
            header,
            results,
            meta: { sheetName },
          } = excelData;
          const columns: BasicColumn[] = [];
          for (const title of header) {
            columns.push({ title, dataIndex: title });
          }
          tableListRef.value.push({ title: sheetName, dataSource: results, columns });
        }
      }

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
        isJudgeStatus.value = true;
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
        isJudgeStatus.value = true;
      }
      const useSelectionChange = () => {
        selectedRowKeys = getSelectRowKeys();
      };
      async function handleTableDel() {
        await deviceConfigDelete(selectedRowKeys);
        createMessage.success('删除成功');
        // reload();
        handleSuccess();
      }

      function handleDelete(record: Recordable) {
        let ids = [record.id];
        deviceConfigDelete(ids).then(() => {
          createMessage.success('删除设备配置成功');
          handleSuccess();
        });
      }

      async function handleDetailView(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: false,
        });
        isJudgeStatus.value = false;
      }
      function handleExport(record: Recordable) {
        console.log(record);
        // aoaToSheetXlsx({
        //   // data: record,
        //   // header: arrHeader,
        //   filename: '二维数组方式导出excel.xlsx',
        // });
      }
      function handleImport() {
        console.log('record');
      }
      function handleSuccess() {
        reload();
      }
      return {
        useSelectionChange,
        handleTableDel,
        isJudgeStatus,
        tableListRef,
        loadDataSuccess,
        handleImport,
        handleExport,
        handleDetailView,
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        registerModal,
      };
    },
  });
</script>