import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { ClinicReport, ClinicReportParam } from './model';

/**
 * 分页查询报告
 */
export async function pageClinicReport(params: ClinicReportParam) {
  const res = await request.get<ApiResult<PageResult<ClinicReport>>>(
    '/clinic/clinic-report/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询报告列表
 */
export async function listClinicReport(params?: ClinicReportParam) {
  const res = await request.get<ApiResult<ClinicReport[]>>(
    '/clinic/clinic-report',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加报告
 */
export async function addClinicReport(data: ClinicReport) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-report',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改报告
 */
export async function updateClinicReport(data: ClinicReport) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-report',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除报告
 */
export async function removeClinicReport(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-report/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除报告
 */
export async function removeBatchClinicReport(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-report/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询报告
 */
export async function getClinicReport(id: number) {
  const res = await request.get<ApiResult<ClinicReport>>(
    '/clinic/clinic-report/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
