import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { TaskCount, TaskCountParam } from './model/index';
import { TabsParam } from '@/api/tabs';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询统计
 */
export async function pageTaskCount(params: TaskCountParam) {
  const res = await request.get<ApiResult<PageResult<TaskCount>>>(
    MODULES_API_URL + '/oa/task-count/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询统计列表
 */
export async function listTaskCount(params?: TaskCountParam) {
  const res = await request.get<ApiResult<TaskCount[]>>(
    MODULES_API_URL + '/oa/task-count',
    {
      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 getTaskCount(id: number) {
  const res = await request.get<ApiResult<TaskCount>>(
    MODULES_API_URL + '/oa/task-count/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加统计
 */
export async function addTaskCount(data: TaskCount) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-count',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改统计
 */
export async function updateTaskCount(data: TaskCount) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-count',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除统计
 */
export async function removeTaskCount(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-count/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除统计
 */
export async function removeBatchTaskCount(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-count/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>>(
    MODULES_API_URL + '/oa/task-count/count',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
