/**
 * 库存管理相关API
 */

import { USE_MOCK } from '@/utils/config';
import { mockInventoryList, mockInventoryLogList, mockInventoryStats } from '@/mock';

export interface InventoryQueryParams {
  page?: number;
  pageSize?: number;
  shopId?: number;
  deviceId?: number;
  productName?: string;
  status?: string; // normal, low, out
}

export interface InventoryListResponse {
  list: any[];
  total: number;
  page: number;
  pageSize: number;
}

/**
 * 获取库存列表
 */
export async function getInventoryList(params: InventoryQueryParams = {}): Promise<InventoryListResponse> {
  if (USE_MOCK) {
    const { page = 1, pageSize = 10, status, productName } = params;
    let list = [...mockInventoryList];
    
    // 状态筛选
    if (status && status !== 'all') {
      list = list.filter(item => item.status === status);
    }
    
    // 商品名称搜索
    if (productName) {
      list = list.filter(item => item.productName.includes(productName));
    }
    
    const total = list.length;
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    
    return Promise.resolve({
      list: list.slice(startIndex, endIndex),
      total,
      page,
      pageSize
    });
  }
  
  const { get } = await import('@/utils/request');
  return get('/inventory', params);
}

/**
 * 获取库存日志
 */
export async function getInventoryLogs(params: any = {}): Promise<any> {
  if (USE_MOCK) {
    return Promise.resolve({
      list: mockInventoryLogList,
      total: mockInventoryLogList.length
    });
  }
  
  const { get } = await import('@/utils/request');
  return get('/inventory/logs', params);
}

/**
 * 获取库存统计
 */
export async function getInventoryStats(): Promise<any> {
  if (USE_MOCK) {
    return Promise.resolve(mockInventoryStats);
  }
  
  const { get } = await import('@/utils/request');
  return get('/inventory/stats');
}
