/**
 * 门店管理相关API
 */

import { USE_MOCK } from '@/utils/config';
import { mockShopList, mockShopDetail } from '@/mock';

export interface ShopQueryParams {
  page?: number;
  pageSize?: number;
  name?: string;
  status?: number;
}

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

/**
 * 获取门店列表
 */
export async function getShopList(params: ShopQueryParams = {}): Promise<ShopListResponse> {
  if (USE_MOCK) {
    const { page = 1, pageSize = 10, status, name } = params;
    let list = [...mockShopList];
    
    // 状态筛选
    if (status !== undefined && status !== -1) {
      list = list.filter(item => item.status === status);
    }
    
    // 名称搜索
    if (name) {
      list = list.filter(item => item.name.includes(name));
    }
    
    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('/shops', params);
}

/**
 * 获取门店详情
 */
export async function getShopDetail(shopId: number): Promise<any> {
  if (USE_MOCK) {
    return Promise.resolve(mockShopDetail);
  }
  
  const { get } = await import('@/utils/request');
  return get(`/shops/${shopId}`);
}

/**
 * 获取门店下拉列表（用于筛选）
 */
export async function getShopOptions(): Promise<any[]> {
  if (USE_MOCK) {
    return Promise.resolve(mockShopList.map(item => ({
      id: item.id,
      name: item.name
    })));
  }
  
  const { get } = await import('@/utils/request');
  return get('/shops/options');
}
