// API 使用示例
import api, { userService, travelService, homeService, apiUtils } from './index.js'

// 示例1: 用户登录
async function exampleLogin() {
  try {
    const result = await userService.login({
      username: 'user123',
      password: 'password123'
    })
    
    console.log('登录成功:', result)
    // 保存token
    api.setAuthToken(result.token)
    
  } catch (error) {
    console.error('登录失败:', error)
    api.handleError(error)
  }
}

// 示例2: 获取首页数据
async function exampleGetHomeData() {
  try {
    const homeData = await homeService.getHomeData()
    console.log('首页数据:', homeData)
    
    // 或者使用错误处理包装器
    const { success, data, error } = await api.withErrorHandling(
      homeService.getHomeData(),
      '首页数据加载成功'
    )
    
    if (success) {
      // 处理数据
      console.log('处理首页数据:', data)
    }
    
  } catch (error) {
    api.handleError(error, '首页数据加载失败')
  }
}

// 示例3: 分页获取旅行计划
async function exampleGetTravelPlans() {
  try {
    const pagination = apiUtils.generatePagination(1, 10)
    const travels = await travelService.getList({
      ...pagination,
      keyword: '西藏'
    })
    
    console.log('旅行计划列表:', travels)
    
  } catch (error) {
    api.handleError(error)
  }
}

// 示例4: 文件上传
async function exampleUploadAvatar() {
  try {
    // 选择图片
    const res = await uni.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album']
    })
    
    const filePath = res.tempFilePaths[0]
    const uploadResult = await userService.uploadAvatar(filePath)
    
    console.log('头像上传成功:', uploadResult)
    
  } catch (error) {
    api.handleError(error, '头像上传失败')
  }
}

// 示例5: 完整的页面数据加载流程
async function loadPageData() {
  try {
    // 并行加载多个数据
    const [banners, travels, animals, flowers] = await Promise.all([
      homeService.getBanners(),
      homeService.getRecommendedTravels(),
      homeService.getHotAnimals(),
      homeService.getFeaturedFlowers()
    ])
    
    return {
      banners: banners || [],
      travelPlans: travels || [],
      animals: animals || [],
      flowers: flowers || []
    }
    
  } catch (error) {
    api.handleError(error)
    // 返回默认数据或空数据
    return {
      banners: [],
      travelPlans: [],
      animals: [],
      flowers: []
    }
  }
}

// 示例6: 订单支付流程
async function examplePaymentFlow(orderId) {
  try {
    // 1. 创建支付
    const payment = await paymentService.create({
      orderId,
      amount: 100,
      paymentMethod: 'wechat'
    })
    
    // 2. 调用微信支付
    const payResult = await uni.requestPayment({
      timeStamp: payment.timeStamp,
      nonceStr: payment.nonceStr,
      package: payment.package,
      signType: payment.signType,
      paySign: payment.paySign
    })
    
    // 3. 确认支付结果
    if (payResult.errMsg === 'requestPayment:ok') {
      // 支付成功，更新订单状态
      await orderService.pay(orderId)
      console.log('支付成功')
    }
    
  } catch (error) {
    api.handleError(error, '支付失败')
  }
}

// 导出示例函数
export default {
  exampleLogin,
  exampleGetHomeData,
  exampleGetTravelPlans,
  exampleUploadAvatar,
  loadPageData,
  examplePaymentFlow
}

// 在页面中使用示例
/*
// 在Vue组件的methods中
methods: {
  async loadData() {
    const pageData = await loadPageData()
    this.banners = pageData.banners
    this.travelPlans = pageData.travelPlans
    this.animals = pageData.animals
    this.flowers = pageData.flowers
  },
  
  async handleLogin() {
    await exampleLogin()
    // 登录成功后重新加载数据
    await this.loadData()
  }
}
*/