<template>
  <PageWrapper>
    <a-card class="proCard tabsCard">
      <a-tabs v-model:activeKey="activeName" @change="handleClick">
        <a-tab-pane
          :key="item.value"
          :tab="item.configName"
          v-for="(item, index) in messageTypeList"
        >
          <template #tab>
            <a-badge :count="item.number ? item.number : ''" class="item">
              <span class="t1">{{ item.name }}</span>
            </a-badge>
          </template>
        </a-tab-pane>
      </a-tabs>
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :row-selection="{ onChange: onSelectionChange }"
      >
        <template #tableTitle>
          <a-space>
            <a-button type="primary" @click="handleSetRead()"> 批量确认 </a-button>
            <a-button type="danger" @click="handleDelete()" :disabled="!selectionData.length">
              <template #icon>
                <DeleteOutlined />
              </template>
              删除
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button type="primary" @click="handleSetRead(record.id)">
                <template #icon><CheckOutlined /></template>
                确认
              </a-button>
              <a-button type="primary" @click="handleDetail(record.id)">
                <template #icon><EyeOutlined /></template>
                详情
              </a-button>
              <a-button type="primary" danger @click="handleDelete(record.id)">
                <template #icon><DeleteOutlined /></template>
                删除
              </a-button>
            </a-space>
          </template>
        </template>
      </BasicTable>
    </a-card>
    <editDialog
      v-if="editVisible"
      :messageId="messageId"
      v-model:visible="editVisible"
      @success="reloadTable('noRefresh')"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h, nextTick, defineAsyncComponent, onMounted } from 'vue';
  import { EyeOutlined, DeleteOutlined, CheckOutlined } from '@ant-design/icons-vue';
  import {
    getMessageProfile,
    messageDelete,
    messageBatchDelete,
    setRead,
  } from '@/api/dashboard/message';
  import { columns } from './columns';
  import { Modal, message } from 'ant-design-vue';

  /**
   * 定义参数
   */
  const editDialog = defineAsyncComponent(() => import('./edit.vue'));
  const messageId = ref(0);
  const docHeight = ref(500);
  const activeName = ref(1);
  const editVisible = ref(false);
  const selectionData = ref([]);
  const tableRef = ref();

  /**
   * 定义查询参数
   */
  const formParams = reactive({
    type: 1,
  });

  /**
   * 定义数据源
   */
  const messageTypeList = ref([
    {
      name: '系统通知',
      value: 1,
    },
    {
      name: '用户私信',
      value: 2,
    },
    {
      name: '代办事项',
      value: 3,
    },
  ]);

  /**
   * 加载数据列表
   * @param res 参数
   */
  const loadDataTable = async (res: any) => {
    const result = await getMessageProfile({ ...formParams, ...res });
    messageTypeList.value[0].number = result.systemNum;
    messageTypeList.value[1].number = result.profileNum;
    messageTypeList.value[2].number = result.projectNum;
    return result;
  };

  /**
   * 刷新数据列表
   * @param noRefresh 参数
   */
  function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }

  /**
   * 执行点击事件
   * @param e 参数
   */
  const handleClick = (e) => {
    activeName.value = e;
    formParams.type = e;
    reloadTable();
  };

  /**
   * 执行查看详情
   * @param id 参数
   */
  const handleDetail = async (id) => {
    messageId.value = id;
    await nextTick();
    editVisible.value = true;
  };

  /**
   * 执行删除
   * @param id 参数
   */
  async function handleDelete(id) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除？',
      onOk: async () => {
        id ? await messageDelete(id) : await messageBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }

  /**
   * 执行已读
   * @param id 参数
   */
  async function handleSetRead(id) {
    Modal.confirm({
      title: '提示',
      content: '确定标记为已读？',
      onOk: async () => {
        id ? await setRead({ idList: [id] }) : await setRead({idList:selectionData.value});
        message.success('标记成功');
        reloadTable();
      },
    });
  }

  /**
   * 数据行选中事件
   * @param value 参数
   */
  function onSelectionChange(value) {
    selectionData.value = value;
  }

  /**
   * 钩子函数
   */
  onMounted(() => {
    docHeight.value = document.body.clientHeight - 160;
  });
</script>

<style lang="less" scoped>
  :deep(.ant-badge-count) {
    right: -13px;
  }
</style>
