<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btn-event="onBtnClicked" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { columns, searchFormSchema } from './data';
  import { getGroupOrUserTaskAndTuBan } from '@/api/demo/statistics';

  const { createConfirm, createMessage } = useMessage();
  const searchInfo = reactive<Recordable>({});
  // 2-小组，1-人员
  searchInfo.type = 2;
  const isDelete = ref(0);
  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '消息列表',
    api: getGroupOrUserTaskAndTuBan,
    columns,
    rowKey: 'msgId',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    // 勾选列
    rowSelection: {
      //多选框
      type: 'checkbox',
    },
    // 搜索
    handleSearchInfoFn(info) {
      isDelete.value = info.isDelete;
      return info;
    },
  });
  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选至少一条消息进行删除');
    }
    const ids = rows.map((t) => t.msgId);
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除选中消息吗',
      onOk: async () => {
        const data = await virtualDeleteEntitys(ids);
        if (data) {
          handleSuccess();
          createMessage.success('删除成功');
        } else {
          createMessage.error('删除失败');
        }
      },
    });
  }
  // 彻底删除
  async function completelyDel() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请勾选至少一条消息进行彻底删除');
    }
    const ids = rows.map((t) => t.msgId);
    createConfirm({
      iconType: 'info',
      title: '彻底删除',
      content: '确定要彻底删除选中消息吗',
      onOk: async () => {
        const data = await deleteEntitys(ids);
        if (data) {
          handleSuccess();
          createMessage.success('彻底删除成功');
        } else {
          createMessage.error('彻底删除失败');
        }
      },
    });
  }

  function handleSuccess() {
    clearSelectedRowKeys();
    reload();
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnDelete':
        if (isDelete.value == 1) {
          completelyDel();
        } else {
          handleDelete();
        }
        break;
      default:
        break;
    }
  }
</script>
<style lang="less">
  .full-modal {
    .ant-modal {
      max-width: 100%;
      top: 0;
    }

    .ant-modal-content {
      height: calc(100vh);
    }

    .ant-modal-body {
      height: 85%;
    }
  }
</style>
