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

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

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

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

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

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

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

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