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

/**
 * 分页查询患者
 */
export async function pageClinicPatientUser(params: ClinicPatientUserParam) {
  const res = await request.get<ApiResult<PageResult<ClinicPatientUser>>>(
    '/clinic/clinic-patient-user/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询患者列表
 */
export async function listClinicPatientUser(params?: ClinicPatientUserParam) {
  const res = await request.get<ApiResult<ClinicPatientUser[]>>(
    '/clinic/clinic-patient-user',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加患者
 */
export async function addClinicPatientUser(data: ClinicPatientUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-patient-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改患者
 */
export async function updateClinicPatientUser(data: ClinicPatientUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-patient-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询患者
 */
export async function getClinicPatientUser(id: number) {
  const res = await request.get<ApiResult<ClinicPatientUser>>(
    '/clinic/clinic-patient-user/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
