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

/**
 * 分页查询工单成员
 */
export async function pageOaTaskUser(params: OaTaskUserParam) {
  const res = await request.get<ApiResult<PageResult<OaTaskUser>>>(
    '/oa/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 listOaTaskUser(params?: OaTaskUserParam) {
  const res = await request.get<ApiResult<OaTaskUser[]>>(
    '/oa/oa-task-user',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

/**
 * 根据id查询工单成员
 */
export async function getOaTaskUser(id: number) {
  const res = await request.get<ApiResult<OaTaskUser>>(
    '/oa/oa-task-user/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
