<template>
  <div :class="prefixCls">
    <Popover title="" trigger="click" :overlayClassName="`${prefixCls}__overlay`">
      <Badge size="small" dot :count="count" :numberStyle="numberStyle" @click="onNoticeClick">
        <BellOutlined />
      </Badge>
      <!-- <template #content>
        <Tabs>
          <template v-for="item in listData" :key="item.key">
            <TabPane>
              <template #tab>
                {{ item.name }}
                <span v-if="item.list.length !== 0">({{ item.list.length }})</span>
              </template>
              <NoticeList :list="item.list" v-if="item.key === '1'" @title-click="onNoticeClick" />
              <NoticeList :list="item.list" v-else />
            </TabPane>
          </template>
        </Tabs>
      </template> -->
    </Popover>
  </div>
</template>
<script lang="ts">
  import { onMounted, defineComponent, ref, h } from 'vue';
  // Tabs, Badge
  import { Popover, Badge } from 'ant-design-vue';
  import { BellOutlined } from '@ant-design/icons-vue';
  import { tabListData } from './data';
  // import NoticeList from './NoticeList.vue';
  import { useDesign } from '/@/hooks/web/useDesign';
  // import { useMessage } from '/@/hooks/web/useMessage';
  import { unreadNoticeApi, readAllApi } from '/@/api/notification/list';
  import { useGo } from '/@/hooks/web/usePage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { TableImg } from '/@/components/Table';
  // Tabs, TabPane: Tabs.TabPane, Badge, NoticeList
  export default defineComponent({
    components: { Popover, BellOutlined, Badge },
    setup() {
      const { prefixCls } = useDesign('header-notify');
      // const { createMessage } = useMessage();

      const listData = ref(tabListData);
      const go = useGo();
      const { createWarningModal } = useMessage();

      const count = ref(0);
      async function fetchNotice() {
        const unread = await unreadNoticeApi();
        console.log('unread', unread.totalCount);
        count.value = unread.totalCount;
        interface Unread {
          title: string;
          type: number;
          content: string;
        }
        if (unread.list?.length) {
          const list = unread.list[0] as unknown as Unread;
          if (list.type === 0) {
            createWarningModal({
              title: list.title,
              content: list.content,
              onOk: async () => {
                await readAllApi();
                await fetchNotice();
              },
            });
          }
          if (list.type === 1) {
            createWarningModal({
              title: list.title,
              content: h(TableImg, {
                size: 330,
                imgList: [list.content],
              }),
              onOk: async () => {
                await readAllApi();
                await fetchNotice();
              },
            });
          }
        }
      }
      onMounted(async () => {
        fetchNotice();
      });

      function onNoticeClick() {
        go('/notification');
        count.value = 0;
      }

      return {
        prefixCls,
        listData,
        count,
        onNoticeClick,
        numberStyle: {},
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-header-notify';

  .@{prefix-cls} {
    padding-top: 2px;

    &__overlay {
      max-width: 360px;
    }

    .ant-tabs-content {
      width: 300px;
    }

    .ant-badge {
      font-size: 18px;

      .ant-badge-multiple-words {
        padding: 0 4px;
        transform: translate(50%, 50%);
      }

      svg {
        width: 0.9em;
      }
    }
  }
</style>
