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

/**
 * 分页查询药品库存
 */
export async function pageClinicMedicineStock(params: ClinicMedicineStockParam) {
  const res = await request.get<ApiResult<PageResult<ClinicMedicineStock>>>(
    '/clinic/clinic-medicine-stock/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询药品库存列表
 */
export async function listClinicMedicineStock(params?: ClinicMedicineStockParam) {
  const res = await request.get<ApiResult<ClinicMedicineStock[]>>(
    '/clinic/clinic-medicine-stock',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加药品库存
 */
export async function addClinicMedicineStock(data: ClinicMedicineStock) {
  const res = await request.post<ApiResult<unknown>>(
    '/clinic/clinic-medicine-stock',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改药品库存
 */
export async function updateClinicMedicineStock(data: ClinicMedicineStock) {
  const res = await request.put<ApiResult<unknown>>(
    '/clinic/clinic-medicine-stock',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除药品库存
 */
export async function removeClinicMedicineStock(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-medicine-stock/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除药品库存
 */
export async function removeBatchClinicMedicineStock(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/clinic/clinic-medicine-stock/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询药品库存
 */
export async function getClinicMedicineStock(id: number) {
  const res = await request.get<ApiResult<ClinicMedicineStock>>(
    '/clinic/clinic-medicine-stock/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
