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

/**
 * 分页查询工单成员
 */
export async function pageTaskUser(params: TaskUserParam) {
  const res = await request.get<ApiResult<PageResult<TaskUser>>>(
    MODULES_API_URL + '/oa/task-user/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询工单成员列表
 */
export async function listTaskUser(params?: TaskUserParam) {
  const res = await request.get<ApiResult<TaskUser[]>>(
    MODULES_API_URL + '/oa/task-user',
    {
      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 getTaskUser(id: number) {
  const res = await request.get<ApiResult<TaskUser>>(
    MODULES_API_URL + '/oa/task-user/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加工单成员
 */
export async function addTaskUser(data: TaskUser) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改工单成员
 */
export async function updateTaskUser(data: TaskUser) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/task-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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