<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btn-event="onBtnClicked" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                // icon: 'ant-design:ellipsis-outlined',
                label: '查看',
                onClick: viewAccount.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <a-modal
      width="100%"
      wrap-class-name="full-modal"
      v-model:open="auditOpen"
      title="详情"
      :destroyOnClose="true"
    >
      <template #footer> </template>
      <Look ref="posRef" :processId="processId" />
    </a-modal>
    <DetailModal @register="registerModal" />
  </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 { Look } from '@/views/demo/workflow/task/process/page';

  import {
    loadSysImMsgList,
    deleteEntitys,
    virtualDeleteEntitys,
    readMsg,
    getInfoByContentId,
  } from '@/api/demo/message';
  import { getDetail } from '@/api/sys/WFSchemeInfo';
  import { flowStore } from '@/store/modules/flow';
  import DetailModal from './DetailModal.vue';

  const flowWfDataStore = flowStore();
  const { createConfirm, createMessage } = useMessage();
  const searchInfo = reactive<Recordable>({});
  const isDelete = ref(0);
  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '消息列表',
    api: loadSysImMsgList,
    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;
    },
    actionColumn: {
      width: 80,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });
  const auditOpen = ref(false);
  const processId = ref('');
  const taskId = ref('');
  const isRead: any = ref(0);
  const type = ref('');
  const [registerModal, { openModal }] = useModal();

  async function viewAccount(record: Recordable) {
    // 任务超期消息
    if (record.contentId) {
      const info = await getInfoByContentId({
        id: record.contentId,
      });
      if (info) {
        let data = await getDetail({ code: info.ProcessCode });
        let scheme = JSON.parse(data.scheme.content);
        let wfData = scheme.wfData;
        flowWfDataStore.setWfDataAll(wfData);
        auditOpen.value = true;
        processId.value = info.ProcessId;
        taskId.value = info.Id;
        type.value = info.Type;
      } else {
        openModal(true, {
          record,
        });
      }
    } else {
      // 已读的消息
      openModal(true, {
        record,
      });
    }
    const data = await readMsg({
      id: record.msgId,
    });
    if (data) {
      record.isRead = 1;
    }
  }

  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;
    }
  }
  function closeMolder() {
    auditOpen.value = false;
  }
</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>
