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

/**
 * 分页查询医疗记录
 */
export async function pageClinicDoctorMedicalRecord(params: ClinicDoctorMedicalRecordParam) {
  const res = await request.get<ApiResult<PageResult<ClinicDoctorMedicalRecord>>>(
    '/clinic/clinic-doctor-medical-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询医疗记录列表
 */
export async function listClinicDoctorMedicalRecord(params?: ClinicDoctorMedicalRecordParam) {
  const res = await request.get<ApiResult<ClinicDoctorMedicalRecord[]>>(
    '/clinic/clinic-doctor-medical-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 addClinicDoctorMedicalRecord(data: ClinicDoctorMedicalRecord) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-doctor-medical-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改医疗记录
 */
export async function updateClinicDoctorMedicalRecord(data: ClinicDoctorMedicalRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-doctor-medical-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除医疗记录
 */
export async function removeClinicDoctorMedicalRecord(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/clinic/clinic-doctor-medical-record/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询医疗记录
 */
export async function getClinicDoctorMedicalRecord(id: number) {
  const res = await request.get<ApiResult<ClinicDoctorMedicalRecord>>(
    '/clinic/clinic-doctor-medical-record/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
