import type { PageResult } from '@/api';
import request from '@/utils/request';
import { ApiResult } from '@/api';
import { NnReadNum, Notice, NoticeParam } from '@/api/oa/notice/model';

/**
 * 分页查询通知
 */
export async function pageNotices(params: NoticeParam) {
  params.type = 'notice';
  const res = await request.get<ApiResult<PageResult<Notice>>>(
    '/oa/notice/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询私信
 */
export async function pageLetters(params: NoticeParam) {
  params.type = 'letter';
  const res = await request.get<ApiResult<PageResult<Notice>>>(
    '/oa/notice/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询代办
 */
export async function pageTodos(params: NoticeParam) {
  params.type = 'todo';
  const res = await request.get<ApiResult<PageResult<Notice>>>(
    '/oa/notice/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询未读数量
 */
export async function getUnReadNum() {
  const res = await request.get<ApiResult<NnReadNum>>(
    '/oa/notice/getUnReadNum'
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
