index.vue 7.21 KB
<script lang="ts" setup>
  import { Button, Tooltip, Card, Popconfirm } from 'ant-design-vue';
  import { AuthIcon, EnumTableCardMode } from '/@/components/Widget';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicCardList, useCardList } from '/@/components/CardList';
  import { ref } from 'vue';
  import { HandleOperationEnum, HandleOperationNameEnum } from '../../config';
  import { deleteApplicationConfig, applicationConfigPage } from '/@/api/application/application';
  import { ApplicationConfigItemType } from '/@/api/application/model/application';
  import { FormDrawer } from '../FormDrawer';
  import { useDrawer } from '/@/components/Drawer';
  import moment from 'moment';
  import { isArray } from '/@/utils/is';
  import AuthDropDown from '/@/components/Widget/AuthDropDown.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { searchFormSchema } from '../FormDrawer/config';

  defineProps<{
    mode: EnumTableCardMode;
  }>();

  defineEmits(['register']);

  enum DropMenuEvent {
    DELETE = 'delete',
  }

  const { t } = useI18n();

  const { createMessage } = useMessage();

  const disabledDeleteFlag = ref(true);

  const [registerCardList, { reload, getSelectedRecords, clearSelectedKeys }] = useCardList({
    api: applicationConfigPage,
    useSearchForm: true,
    gutter: 4,
    rowKey: 'id',
    formConfig: {
      schemas: searchFormSchema,
      labelWidth: 100,
      baseColProps: { span: 8 },
    },
    selections: {
      beforeSelectValidate: () => {
        return true;
      },
      onSelect: (_record, _flag, allSelecteds) => {
        disabledDeleteFlag.value = !allSelecteds.length;
      },
      onSelectAll: () => {
        // 全选事件
        disabledDeleteFlag.value = false;
      },
      onUnSelectAll: () => {
        // 反选事件
        disabledDeleteFlag.value = true;
      },
      onSelectToggle: (status: boolean) => {
        // 全选是false,反选是true
        if (!status) disabledDeleteFlag.value = false;
        else disabledDeleteFlag.value = true;
      },
    },
  });

  const [registerApplicationConfigFormDrawer, { openDrawer: openApplicationConfigFormDrawer }] =
    useDrawer();

  const handleApplicationConfigIsSuccess = () => reload();

  const handleOperationEvent = (
    event: HandleOperationEnum,
    record: ApplicationConfigItemType | null
  ) => {
    const isUpdate = event === HandleOperationEnum.CREATE ? false : true;
    const isUpdateText =
      event === HandleOperationEnum.CREATE
        ? HandleOperationNameEnum.CREATE
        : event === HandleOperationEnum.UPDATE
        ? HandleOperationNameEnum.UPDATE
        : HandleOperationNameEnum.VIEW;
    if (event === HandleOperationEnum.VIEW) {
      openApplicationConfigFormDrawer(true, { isUpdate, record, isUpdateText, event });
    } else {
      openApplicationConfigFormDrawer(true, { isUpdate, record, isUpdateText, event });
    }
  };

  const handleDelete = async (event: HandleOperationEnum, id?: string | null) => {
    try {
      if (event === HandleOperationEnum.BATCH_DELETE) {
        const batchDeleteIds = getSelectedRecords().map(
          (rowRecord: ApplicationConfigItemType) => rowRecord?.id
        ) as string[];
        if (isArray(batchDeleteIds) && batchDeleteIds.length === 0) return;
        await deleteApplicationConfig(batchDeleteIds);
      } else {
        await deleteApplicationConfig([id] as string[]);
      }
      createMessage.success(t('common.deleteSuccessText'));
      clearSelectedKeys();
      disabledDeleteFlag.value = true;
      await reload();
    } catch (error) {
      throw error;
    }
  };
</script>

<template>
  <section>
    <BasicCardList @register="registerCardList">
      <template #toolbar>
        <Button type="primary" @click="handleOperationEvent(HandleOperationEnum.CREATE, null)">
          {{ t('application.config.action.create') }}
        </Button>
        <Popconfirm
          :title="t('common.batchDeleteConfirmText')"
          @confirm="handleDelete(HandleOperationEnum.BATCH_DELETE, null)"
          :disabled="disabledDeleteFlag"
        >
          <Button type="primary" danger :disabled="disabledDeleteFlag">
            {{ t('common.batchDeleteText') }}
          </Button>
        </Popconfirm>
      </template>
      <template #renderItem="{ item }: BasicCardListRenderItem<ApplicationConfigItemType>">
        <Card hoverable>
          <template #cover>
            <div class="w-full h-full !flex flex-col justify-between m-3">
              <div class="!flex justify-between align-center text-center">
                <Tooltip :title="item.name">
                  <span class="truncate font-bold fill-dark-900 text-sm"> {{ item.name }} </span>
                </Tooltip>
              </div>
              <div class="!flex justify-between align-center text-center">
                <span class="truncate text-xs" style="color: #86909c">
                  {{ moment(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}
                </span>
              </div>
            </div>
          </template>
          <template class="ant-card-actions" #actions>
            <Tooltip :title="t('common.detailText')">
              <AuthIcon
                class="!text-lg"
                icon="ant-design:eye-outlined"
                @click.stop="handleOperationEvent(HandleOperationEnum.VIEW, item)"
              />
            </Tooltip>
            <Tooltip :title="t('common.editText')">
              <AuthIcon
                class="!text-lg"
                icon="ant-design:form-outlined"
                @click.stop="handleOperationEvent(HandleOperationEnum.UPDATE, item)"
              />
            </Tooltip>
            <AuthDropDown
              @click.stop
              :trigger="['hover']"
              :drop-menu-list="[
                {
                  text: t('common.delText'),
                  event: DropMenuEvent.DELETE,
                  icon: 'ant-design:delete-outlined',
                  popconfirm: {
                    title: t('common.deleteConfirmText'),
                    onConfirm: handleDelete.bind(null, HandleOperationEnum.DELETE, item?.id),
                  },
                },
              ]"
            />
          </template>
          <Card.Meta>
            <template #description>
              <div class="truncate h-17 !flex justify-between flex-col">
                <div class="truncate !flex">
                  <span class="text-xs" style="color: #86909c">
                    {{ t('application.config.text.organizationName') }}
                  </span>
                  <Tooltip :title="item.organizationName">
                    <span class="truncate ml-7.5 text-xs" style="color: #00b42a">{{
                      item.organizationName
                    }}</span>
                  </Tooltip>
                </div>
              </div>
            </template>
          </Card.Meta>
        </Card>
      </template>
    </BasicCardList>
    <FormDrawer
      @register="registerApplicationConfigFormDrawer"
      @success="handleApplicationConfigIsSuccess"
    />
  </section>
</template>

<style lang="less" scoped>
  .profile-list:deep(.ant-image-img) {
    @apply !w-full !h-full;
  }

  .profile-list:deep(.ant-card-body) {
    @apply !p-4;
  }

  :deep(.ant-card-body) {
    padding: 12px;
  }
</style>