import { requestClient } from '#/api/request';

const prefix = 'warehouse-drug-management/';

/**
 * 分页查询用户列表
 * @param data
 */
export async function getWarehouseDrugManagementList(data: any) {
  return requestClient.get<any>(`${prefix}list`, { params: data });
}

/**
 * 分页查询用户列表
 * @param data
 */
export async function getWarehouseDrugManagementOption(data: any) {
  return requestClient.get<any>(`${prefix}option`, { params: data });
}

/** 搜索可加入总仓的药品（yii_drugstore_drug 中尚不存在） */
export async function searchAvailableWarehouseDrugs(params: {
  name: string;
  type: number;
}) {
  return requestClient.get<any[]>(`${prefix}search-available-drugs`, {
    params,
  });
}

/** 仓库筛选：商品类型（与后端 ProductTypeEnum 一致） */
export async function getWarehouseProductTypeOptions() {
  return requestClient.get<{ label: string; value: number }[]>(
    `${prefix}product-type-options`,
  );
}

/**
 * 获取西（中成）药详情
 * @param id
 */
export async function getWarehouseDrugManagementInfo(id: number) {
  return requestClient.get<any>(`${prefix}detail`, { params: { id } });
}

/**
 * 导出仓库药品药品
 */
export async function exportWarehouseDrugManagementApi(params?: { type?: number }) {
  return requestClient.download(`${prefix}export`, { params });
}
/**
 * 导出仓库药品药品 - 模板
 */
export async function exportWarehouseDrugManagementTemplateApi() {
  return requestClient.download(`${prefix}export-template`);
}

/**
 * 上架/下架
 * @param id
 */
export async function updateWarehouseDrugManagementStatusApi(id: number) {
  return requestClient.get<any>(`${prefix}update-status`, { params: { id } });
}

/**
 * 新增西（中成）药
 * @param data
 */
export async function createWarehouseDrugManagement(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}create`, data);
}

/**
 * 编辑西（中成）药
 * @param data
 */
export async function updateWarehouseDrugManagement(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}update`, data);
}

/**
 * 删除西（中成）药
 * @param data
 */
export async function deleteWarehouseDrugManagement(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}delete`, data);
}

/**
 * 导入仓库药品
 * @param data
 */
export async function importWarehouseDrugManagementApi(data: Record<string, any>) {
  return requestClient.upload(`${prefix}import`, data);
}

/**
 * 导入仓库药品修改价格
 * @param data
 */
export async function importUpdatePriceApi(data: Record<string, any>) {
  return requestClient.upload(`${prefix}import-update-price`, data);
}

export interface WarehouseBatchPriceItem {
  id: number;
  drug_id: number;
  drug_name: string;
  drug_number: string;
  pinyin_simple: string;
  market_price: number | string;
  price: number | string;
}

/** 批量改价预览：全量仓库药品 */
export async function getAllForBatchPriceApi(params: { type: number }) {
  return requestClient.get<{ items: WarehouseBatchPriceItem[] }>(
    `${prefix}list-all-for-batch-price`,
    { params },
  );
}

export interface BatchPriceUpdateItem {
  id: number;
  market_price: number;
  price: number;
}

export interface BatchPriceCreateItem {
  drug_name: string;
  drug_number: string;
  market_price: number;
  price: number;
}

/** 批量改价预览：应用更新与新增 */
export async function applyBatchPriceApi(data: {
  type: number;
  updates?: BatchPriceUpdateItem[];
  creates?: BatchPriceCreateItem[];
  notify?: boolean;
}) {
  return requestClient.post<{
    update_count: number;
    new_count: number;
  }>(`${prefix}apply-batch-price`, data);
}
