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

/**
 * 分页查询工单回复记录表
 */
export async function pageOaTaskRecord(params: OaTaskRecordParam) {
  const res = await request.get<ApiResult<PageResult<OaTaskRecord>>>(
    '/oa/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 listOaTaskRecord(params?: OaTaskRecordParam) {
  const res = await request.get<ApiResult<OaTaskRecord[]>>(
    '/oa/oa-task-record',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加工单回复记录表
 */
export async function addOaTaskRecord(data: OaTaskRecord) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-task-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改工单回复记录表
 */
export async function updateOaTaskRecord(data: OaTaskRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-task-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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