import { defineStore } from 'pinia';
import { getMessageType } from '@/api/public'

interface NoticeItem {
  title?: string;
  read: boolean;
  message: any;
  time: string;
}

export const useNoticeStore = defineStore('notice', () => {
  const state = reactive({
    notices: [] as NoticeItem[],
    emailNoReadObj: 0 as any,
    messageNoReadCount: 0 as number,
    messageData: [] as any,
    bulletinNoReadCount: 0 as number
  });

  const addNotice = (notice: NoticeItem) => {
    state.notices.push(notice);
  };

  const removeNotice = (notice: NoticeItem) => {
    state.notices.splice(state.notices.indexOf(notice), 1);
  };

  //实现全部已读
  const readAll = () => {
    state.notices.forEach((item) => {
      item.read = true;
    });
  };

  const clearNotice = () => {
    state.notices = [];
  };

  // 获取邮箱未读数
  const getCount = async() => {
    // await getNewMailCount().then(res => {
    //   state.emailNoReadObj = res.result
    // })
  }

  // 获取消息未读数
  const getMessageCount = async() => {
    // await getMessageType().then(res => {
    //   let countList = (res.result || []).map(item => item.stateNumber)
    //   state.messageNoReadCount = eval(countList.join("+"))
    //   state.messageData = res.result || []
    // })
    // await getBulletinUnreadCount().then(res => {
    //   state.bulletinNoReadCount = res.result.count
    // })
  }

  const clearBulletinCount = () => {
    state.bulletinNoReadCount = 0
  }

  return {
    state,
    addNotice,
    removeNotice,
    readAll,
    clearNotice,
    getCount,
    getMessageCount,
    clearBulletinCount
  };
});

export default useNoticeStore;
