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

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

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

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

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

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

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

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