index.vue 11.3 KB
<template>
  <div>
    <BasicTable
      @selection-change="useSelectionChange"
      @register="registerTable"
      :loading="loading"
      :rowSelection="{ type: 'checkbox' }"
      :clickToRowSelect="false"
    >
      <template #toolbar>
        <Authority value="api:yt:convert:config:post">
          <a-button type="primary" @click="handleAdd"> 添加流转 </a-button>
        </Authority>
        <Authority value="api:yt:convert:config:delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleConfirm"
          >
            <a-button
              type="primary"
              color="error"
              :disabled="singleStopDeleteStatus || hasBatchDelete"
            >
              <span :style="{ color: singleStopDeleteStatus || hasBatchDelete ? 'grey' : 'white' }"
                >批量删除</span
              >
            </a-button>
          </Popconfirm>
        </Authority>
        <!-- <a-button
          :disabled="disabledStatus2"
          @click="handleMutiuteDisable"
          :type="disabledStatus2 ? 'default' : 'primary'"
        >
          <span :style="{ color: disabledStatus2 ? 'grey' : 'white' }">批量禁用</span>
        </a-button> -->
        <a-button
          :disabled="hasBatchDelete || disabledStatus3"
          @click="handleMutiuteEnable"
          :type="hasBatchDelete || disabledStatus3 ? 'default' : 'primary'"
        >
          <span :style="{ color: hasBatchDelete || disabledStatus3 ? 'grey' : 'white' }"
            >批量启用</span
          >
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '查看',
              auth: 'api:yt:convert:config:get',
              icon: 'ant-design:eye-outlined',
              onClick: handleView.bind(null, record),
              ifShow: record.status == 1,
            },
            {
              label: '编辑',
              auth: 'api:yt:convert:config:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
              ifShow: (_action) => {
                return record.status == 0;
              },
            },

            {
              label: '删除',
              auth: 'api:yt:convert:config:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDeleteOrBatchDelete.bind(null, record),
              },
              ifShow: (_action) => {
                return record.status == 0;
              },
            },
          ]"
        />
      </template>
      <template #status="{ record }">
        <Switch
          :disabled="disabledSwitch"
          :checked="record.status === 1"
          :loading="record.pendingStatus"
          checkedChildren="启用"
          unCheckedChildren="禁用"
          @change="(checked:boolean)=>statusChange(checked,record)"
        />
      </template>
    </BasicTable>
    <div>
      <DataTransferDrawer @register="registerModal" @success="handleSuccess" />
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './config';
  import { useModal } from '/@/components/Modal';
  import DataTransferDrawer from './addDataTransferDrawer.vue';
  import {
    getConvertApi,
    isEnableOrDisableApi,
    deleteConvertApi,
  } from '/@/api/datamanager/dataManagerApi';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Authority } from '/@/components/Authority';
  import { useBatchDelete } from '/@/hooks/web/useBatchDelete';
  import { Switch, Popconfirm } from 'ant-design-vue';

  export default defineComponent({
    name: 'Index',
    components: { BasicTable, TableAction, DataTransferDrawer, Authority, Switch, Popconfirm },
    setup() {
      const disabledSwitch = ref(false);
      const enableObj = reactive({
        convertIds: [],
        status: 0,
      });
      const singleStopDeleteStatus = ref(false);
      const disabledStatus1 = ref(true);
      const disabledStatus2 = ref(true);
      const disabledStatus3 = ref(true);
      const loading = ref(true);
      const { createMessage } = useMessage();
      let getSelectRowsArr: any = ref([]);
      let isJudgeSelectRowsArr: any = ref([]);
      let hasDisableStatus = [];

      const handleSuccess = () => {
        reload();
        resetSelectedRowKeys();
      };
      const [registerModal, { openModal }] = useModal();
      const [
        registerTable,
        { setProps, reload, getSelectRowKeys, getSelectRows, setLoading, clearSelectedRowKeys },
      ] = useTable({
        title: '数据流转列表',
        clickToRowSelect: false,
        columns,
        api: getConvertApi,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        rowKey: 'id',
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 180,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const { hasBatchDelete, handleDeleteOrBatchDelete, selectionOptions, resetSelectedRowKeys } =
        useBatchDelete(deleteConvertApi, handleSuccess, setProps);
      selectionOptions.rowSelection.getCheckboxProps = (record: Recordable) => {
        if (record.length == 0) {
          disabledStatus1.value = true;
          disabledStatus2.value = true;
          disabledStatus3.value = true;
        }
        //status为1的选择框禁用
        if (record.status === 1) {
          return { disabled: true };
        } else {
          disabledStatus2.value = true;
          singleStopDeleteStatus.value = false;
          return { disabled: false };
        }
      };
      nextTick(() => {
        setProps(selectionOptions);
      });

      //新增
      const handleAdd = () => {
        setTimeout(() => {
          openModal(true, {
            isUpdate: false,
          });
        }, 10);
      };

      const handleEdit = (record: Recordable) => {
        setTimeout(() => {
          openModal(true, {
            record,
            isUpdate: true,
          });
        }, 10);
      };
      const useSelectionChange = () => {
        isJudgeSelectRowsArr.value = getSelectRows();
        hasDisableStatus = isJudgeSelectRowsArr.value.map((m) => {
          return m.status;
        });
        console.log(hasDisableStatus);
        if (hasDisableStatus.length == 0) {
          disabledStatus1.value = true;
          disabledStatus2.value = true;
          disabledStatus3.value = true;
        } else {
          hasDisableStatus.every((e) => {
            if (e == 1) {
              disabledStatus3.value = true;
              disabledStatus2.value = false;
            } else if (e == 0) {
              disabledStatus2.value = true;
              disabledStatus3.value = false;
            }
          });
        }
        // let i1 = hasDisableStatus.indexOf(0);
        // let i2 = hasDisableStatus.indexOf(1);
        // if (i1 !== -1 && i2 !== -1) {
        //   disabledStatus2.value = true;
        //   disabledStatus3.value = true;
        // }
        // if (isJudgeSelectRowsArr.value.length == 0) {
        //   disabledStatus1.value = true;
        // } else {
        //   disabledStatus1.value = false;
        // }
        // if (hasDisableStatus.includes(1)) {
        //   disabledStatus1.value = true;
        // }
      };
      const handleMutiuteDisable = async () => {
        enableObj.convertIds.length = 0;
        try {
          setLoading(true);
          getSelectRowsArr.value = getSelectRows();
          getSelectRowsArr.value.forEach((f) => {
            if (f.id) {
              enableObj.status = 0;
              enableObj.convertIds.push(f.id as never);
            }
          });
          const res = await isEnableOrDisableApi(enableObj as never);
          if (res !== '') {
            createMessage.success('流转配置多项禁用成功');
            setLoading(false);
            handleSuccess();
          } else {
            createMessage.error('流转配置多项禁用失败');
          }
        } catch (e) {
          return e;
        } finally {
          setLoading(false);
          clearSelectedRowKeys();
        }
      };
      const handleMutiuteEnable = async () => {
        enableObj.convertIds.length = 0;
        try {
          setLoading(true);
          getSelectRowsArr.value = getSelectRows();
          getSelectRowsArr.value.forEach((f) => {
            if (f.id) {
              enableObj.status = 1;
              enableObj.convertIds.push(f.id as never);
            }
          });
          const res = await isEnableOrDisableApi(enableObj as never);
          if (res !== '') {
            createMessage.success('流转配置多项启用成功');
            setLoading(false);
            handleSuccess();
            disabledStatus1.value = true;
            singleStopDeleteStatus.value = true;
          } else {
            createMessage.error('流转配置多项启用失败');
          }
        } catch (e) {
          return e;
        } finally {
          setLoading(false);
          clearSelectedRowKeys();
        }
      };
      const statusChange = async (checked, record) => {
        try {
          setProps({
            loading: true,
          });
          disabledSwitch.value = true;
          enableObj.convertIds.length = 0;
          resetSelectedRowKeys();
          clearSelectedRowKeys();
          const newStatus = checked ? 1 : 0;
          enableObj.convertIds.push(record.id as never);
          enableObj.status = newStatus;
          const res = await isEnableOrDisableApi(enableObj as never);
          if (res && newStatus) {
            createMessage.success(`流转配置启用成功`);
          } else {
            createMessage.success('流转配置禁用成功');
          }
          singleStopDeleteStatus.value = true;
          disabledStatus1.value = true;
          disabledStatus2.value = true;
          disabledStatus3.value = true;
        } finally {
          setTimeout(() => {
            setProps({
              loading: false,
            });
            disabledSwitch.value = false;
          }, 500);
          reload();
        }
      };
      function handleView(record: Recordable) {
        nextTick(() => {
          openModal(true, {
            record,
            isUpdate: 'view',
          });
        });
      }
      const handleConfirm = () => {
        handleDeleteOrBatchDelete(null);
        disabledStatus3.value = true;
      };

      return {
        disabledStatus1,
        disabledStatus2,
        disabledStatus3,
        handleMutiuteEnable,
        loading,
        registerTable,
        handleAdd,
        registerModal,
        handleSuccess,
        handleEdit,
        useSelectionChange,
        handleMutiuteDisable,
        hasBatchDelete,
        handleDeleteOrBatchDelete,
        singleStopDeleteStatus,
        statusChange,
        disabledSwitch,
        handleView,
        handleConfirm,
      };
    },
  });
</script>