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

/**
 * 分页查询病例
 */
export async function pageClinicMedicalHistory(params: ClinicMedicalHistoryParam) {
  const res = await request.get<ApiResult<PageResult<ClinicMedicalHistory>>>(
    '/clinic/clinic-medical-history/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询病例列表
 */
export async function listClinicMedicalHistory(params?: ClinicMedicalHistoryParam) {
  const res = await request.get<ApiResult<ClinicMedicalHistory[]>>(
    '/clinic/clinic-medical-history',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加病例
 */
export async function addClinicMedicalHistory(data: ClinicMedicalHistory) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-medical-history',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改病例
 */
export async function updateClinicMedicalHistory(data: ClinicMedicalHistory) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-medical-history',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询病例
 */
export async function getClinicMedicalHistory(id: number) {
  const res = await request.get<ApiResult<ClinicMedicalHistory>>(
    '/clinic/clinic-medical-history/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
