// pages/withdraw-records/withdraw-records.js
const api = require('../../utils/api')
const util = require('../../utils/util')

Page({
  data: {
    // 导航栏高度
    statusBarHeight: 44,
    navBarHeight: 44,
    totalNavHeight: 88,

    // 提现记录列表
    list: [],

    // 分页
    page: 1,
    pageSize: 20,
    total: 0,
    hasMore: true,

    // 筛选状态
    currentStatus: 'all', // all, pending, approved, rejected
    statusList: [
      { value: 'all', label: '全部' },
      { value: 'pending', label: '待审核' },
      { value: 'approved', label: '已通过' },
      { value: 'rejected', label: '已拒绝' }
    ],

    // 状态
    loading: false,
    isEmpty: false
  },

  onLoad(options) {
    // 计算导航栏高度
    const systemInfo = wx.getSystemInfoSync()
    const statusBarHeight = systemInfo.statusBarHeight || 44
    const menuButton = wx.getMenuButtonBoundingClientRect()
    const navBarHeight = menuButton.height + (menuButton.top - statusBarHeight) * 2
    const totalNavHeight = statusBarHeight + navBarHeight

    this.setData({
      statusBarHeight,
      navBarHeight,
      totalNavHeight
    })

    // 加载数据
    this.loadWithdrawRecords()
  },

  /**
   * 加载提现记录列表
   */
  async loadWithdrawRecords(page = 1) {
    if (this.data.loading) return

    this.setData({ loading: true })

    try {
      const params = {
        page,
        pageSize: this.data.pageSize
      }

      // 添加状态筛选
      if (this.data.currentStatus !== 'all') {
        params.status = this.data.currentStatus
      }

      const res = await api.commission.getWithdrawals(params)

      if (res.success && res.data) {
        const dataList = res.data.list || []
        const list = page === 1 ? dataList : [...this.data.list, ...dataList]
        const hasMore = dataList.length === this.data.pageSize
        const isEmpty = list.length === 0

        this.setData({
          list,
          page,
          total: res.data.total || 0,
          hasMore,
          isEmpty,
          loading: false
        })
      } else {
        throw new Error(res.message || '加载失败')
      }
    } catch (error) {
      console.error('加载提现记录失败:', error)
      this.setData({ loading: false })
      wx.showToast({
        title: error.message || '加载失败',
        icon: 'none'
      })
    }
  },

  /**
   * 切换状态筛选
   */
  onStatusChange(e) {
    const status = e.currentTarget.dataset.status
    if (status === this.data.currentStatus) return

    this.setData({
      currentStatus: status,
      page: 1,
      list: [],
      hasMore: true
    })

    this.loadWithdrawRecords(1)
  },

  /**
   * 获取状态文本
   */
  getStatusText(status) {
    const statusMap = {
      'pending': '待审核',
      'approved': '已通过',
      'rejected': '已拒绝'
    }
    return statusMap[status] || status
  },

  /**
   * 获取状态样式类
   */
  getStatusClass(status) {
    const classMap = {
      'pending': 'status-pending',
      'approved': 'status-approved',
      'rejected': 'status-rejected'
    }
    return classMap[status] || ''
  },

  /**
   * 格式化金额
   */
  formatMoney(amount) {
    return util.formatMoney(amount)
  },

  /**
   * 格式化时间
   */
  formatTime(timestamp) {
    return util.formatDate(timestamp)
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.loadWithdrawRecords(1).then(() => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 上拉加载更多
   */
  onReachBottom() {
    if (!this.data.hasMore || this.data.loading) return
    this.loadWithdrawRecords(this.data.page + 1)
  },

  /**
   * 返回上一页
   */
  onBack() {
    wx.navigateBack()
  },

  /**
   * 重新加载
   */
  onRetry() {
    this.loadWithdrawRecords(1)
  }
})
