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

/**
 * 分页查询处方订单
 */
export async function pageClinicOrder(params: ClinicOrderParam) {
  const res = await request.get<ApiResult<PageResult<ClinicOrder>>>(
    '/clinic/clinic-order/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询处方订单列表
 */
export async function listClinicOrder(params?: ClinicOrderParam) {
  const res = await request.get<ApiResult<ClinicOrder[]>>(
    '/clinic/clinic-order',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加处方订单
 */
export async function addClinicOrder(data: ClinicOrder) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-order',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改处方订单
 */
export async function updateClinicOrder(data: ClinicOrder) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-order',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除处方订单
 */
export async function removeClinicOrder(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-order/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询处方订单
 */
export async function getClinicOrder(id: number) {
  const res = await request.get<ApiResult<ClinicOrder>>(
    '/clinic/clinic-order/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
