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

/**
 * 分页查询任务
 */
export async function pageTaskRecord(params: TaskRecordParam) {
  const res = await request.get<ApiResult<PageResult<TaskRecord>>>(
    MODULES_API_URL + '/oa/task-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询任务列表
 */
export async function listTaskRecord(params?: TaskRecordParam) {
  const res = await request.get<ApiResult<TaskRecord[]>>(
    MODULES_API_URL + '/oa/task-record',
    {
      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 getTaskRecord(id: number) {
  const res = await request.get<ApiResult<TaskRecord>>(
    MODULES_API_URL + '/oa/task-record/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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