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

/**
 * 分页查询处方明细表

 */
export async function pageClinicPrescriptionItem(params: ClinicPrescriptionItemParam) {
  const res = await request.get<ApiResult<PageResult<ClinicPrescriptionItem>>>(
    '/clinic/clinic-prescription-item/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询处方明细表
列表
 */
export async function listClinicPrescriptionItem(params?: ClinicPrescriptionItemParam) {
  const res = await request.get<ApiResult<ClinicPrescriptionItem[]>>(
    '/clinic/clinic-prescription-item',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加处方明细表

 */
export async function addClinicPrescriptionItem(data: ClinicPrescriptionItem) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-prescription-item',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改处方明细表

 */
export async function updateClinicPrescriptionItem(data: ClinicPrescriptionItem) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-prescription-item',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除处方明细表

 */
export async function removeClinicPrescriptionItem(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-prescription-item/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除处方明细表

 */
export async function removeBatchClinicPrescriptionItem(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-prescription-item/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询处方明细表

 */
export async function getClinicPrescriptionItem(id: number) {
  const res = await request.get<ApiResult<ClinicPrescriptionItem>>(
    '/clinic/clinic-prescription-item/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
