import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
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.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

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