<template>
  <PageWrapper>
    <n-card>
      <n-tabs v-model="activeName" @update:value="handleClick">
        <n-tab-pane
          :name="item.value"
          :tab="item.configName"
          v-for="(item, index) in messageTypeList"
        >
          <template #tab>
            <n-badge :value="item.number ? item.number : ''" class="item" :offset="[10, 2]">
              <span class="t1">{{ item.name }}</span>
            </n-badge>
          </template>
        </n-tab-pane>
      </n-tabs>
      <BasicTable
        :columns="columns"
        :actionColumn="actionColumn"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        @update:checked-row-keys="onSelectionChange"
      >
        <template #tableTitle>
          <n-space>
            <n-button type="primary" @click="handleSetRead()"> 批量确认 </n-button>
            <n-button type="error" @click="handleDelete()" :disabled="!selectionData.length">
              <template #icon>
                <n-icon>
                  <DeleteOutlined />
                </n-icon>
              </template>
              删除
            </n-button>
          </n-space>
        </template>
      </BasicTable>
    </n-card>
    <editDialog
      v-if="editVisible"
      :messageId="messageId"
      v-model:visible="editVisible"
      ref="createModalRef"
      @success="reloadTable('noRefresh')"
    >
    </editDialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h, nextTick, defineAsyncComponent, onMounted } from 'vue';
  import { EyeOutlined, DeleteOutlined, CheckOutlined } from '@vicons/antd';
  import { TableAction } from '@/components/Table';
  import {
    getMessageProfile,
    messageDelete,
    messageBatchDelete,
    setRead,
  } from '@/api/dashboard/message';
  import { columns } from './columns';
  import editDialog from './edit.vue';
  import { useMessage, useDialog } from 'naive-ui';
  import { renderIcon } from '@/utils';
  const messageId = ref(0);
  const message = useMessage();
  const dialog = useDialog();
  const activeName = ref(1);
  const messageTypeList = ref([
    {
      name: '系统通知',
      value: 1,
    },
    {
      name: '用户私信',
      value: 2,
    },
    {
      name: '代办事项',
      value: 3,
    },
  ]);
  const editVisible = ref(false);
  const createModalRef = ref();
  const selectionData = ref([]);
  const tableRef = ref();
  const formParams = reactive({
    type: 1,
  });

  const loadDataTable = async (res: any) => {
    selectionData.value = [];
    const result = await getMessageProfile({ ...formParams, ...res });
    let item = messageTypeList.value.find((item, index) => item.value == activeName.value);
    item.number = result.number;
    return result;
  };
  const actionColumn = reactive({
    width: 300,
    title: '操作',
    align: 'center',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '确认',
            icon: renderIcon(CheckOutlined),
            type: 'info',
            onClick: handleSetRead.bind(null, record),
          },
          {
            label: '详情',
            icon: renderIcon(EyeOutlined),
            type: 'info',
            onClick: handleInfo.bind(null, record),
          },
          {
            label: '删除',
            icon: renderIcon(DeleteOutlined),
            type: 'error',
            onClick: handleDelete.bind(null, record),
          },
        ],
      });
    },
  });
  function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }
  const handleClick = (e) => {
    activeName.value = e;
    formParams.type = e;
    reloadTable();
  };

  const handleInfo = async (record) => {
    messageId.value = record.id;
    editVisible.value = true;
    await nextTick();
    createModalRef.value.openModal();
  };

  async function handleDelete(record) {
    dialog.warning({
      title: '提示',
      content: '确定要删除？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        record.id ? await messageDelete(record.id) : await messageBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }
  async function handleSetRead(id) {
    dialog.warning({
      title: '提示',
      content: '确定标记为已读？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        record.id ? await setRead(record.id) : await setRead(rowKeys.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }
  function onSelectionChange(value) {
    selectionData.value = value;
  }
</script>
