import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Notice, NoticeParam } from './model/index';
import { TabsParam } from '@/api/tabs';

/**
 * 分页查询消息
 */
export async function pageNotice(params: NoticeParam) {
  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 listNotice(params?: NoticeParam) {
  const res = await request.get<ApiResult<Notice[]>>('/oa/notice', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询消息
 */
export async function getNotice(id: number) {
  const res = await request.get<ApiResult<Notice>>('/oa/notice/' + id);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加消息
 */
export async function addNotice(data: Notice) {
  const res = await request.post<ApiResult<unknown>>('/oa/notice', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改消息
 */
export async function updateNotice(data: Notice) {
  const res = await request.put<ApiResult<unknown>>('/oa/notice', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除消息
 */
export async function removeNotice(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/oa/notice/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改消息
 */
export async function updateBatchNotices(data: Notice[]) {
  const res = await request.put<ApiResult<unknown>>('/oa/notice/batch', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除消息
 */
export async function removeBatchNotice(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/oa/notice/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getCount(params?: TabsParam) {
  const res = await request.get<ApiResult<TabsParam>>('/oa/notice/count', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
