import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OaTask, OaTaskParam } from './model';

/**
 * 分页查询任务记录表
 */
export async function pageOaTask(params: OaTaskParam) {
  const res = await request.get<ApiResult<PageResult<OaTask>>>(
    '/oa/oa-task/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询任务记录表列表
 */
export async function listOaTask(params?: OaTaskParam) {
  const res = await request.get<ApiResult<OaTask[]>>(
    '/oa/oa-task',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加任务记录表
 */
export async function addOaTask(data: OaTask) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-task',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改任务记录表
 */
export async function updateOaTask(data: OaTask) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-task',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除任务记录表
 */
export async function removeOaTask(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-task/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除任务记录表
 */
export async function removeBatchOaTask(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-task/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询任务记录表
 */
export async function getOaTask(id: number) {
  const res = await request.get<ApiResult<OaTask>>(
    '/oa/oa-task/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
