// pages/claims/claims.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前激活的标签
    activeTab: 'all',
    
    // 理赔列表
    claimsList: [],
    
    // 过滤后的理赔列表
    filteredClaims: [],
    
    // 加载状态
    loading: false,
    
    // 是否有更多数据
    hasMore: true,
    
    // 当前页码
    currentPage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('理赔申请页加载', options)
    this.loadClaimsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log('理赔申请页渲染完成')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('理赔申请页显示')
    this.loadClaimsList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('理赔申请页隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('理赔申请页卸载')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉刷新')
    this.setData({
      currentPage: 1,
      hasMore: true
    })
    this.loadClaimsList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('上拉触底')
    if (this.data.hasMore && !this.data.loading) {
      this.loadMore()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '保险理赔申请',
      path: '/pages/claims/claims',
      imageUrl: '/static/images/share-claims.jpg'
    }
  },

  /**
   * 加载理赔列表
   */
  loadClaimsList() {
    this.setData({ loading: true })
    
    console.log('加载理赔列表')
    
    // 模拟API调用
    setTimeout(() => {
      const mockClaims = this.getMockClaims()
      this.setData({
        claimsList: mockClaims,
        filteredClaims: this.filterClaims(mockClaims, this.data.activeTab),
        loading: false
      })
    }, 1000)
  },

  /**
   * 获取模拟理赔数据
   */
  getMockClaims() {
    return [
      {
        id: '1',
        title: '意外伤害理赔',
        description: '交通事故导致的人身伤害',
        status: 'pending',
        statusText: '待处理',
        applicationTime: '2024-01-15 14:30',
        claimAmount: 5000,
        policyNo: 'POL20240115001'
      },
      {
        id: '2',
        title: '医疗费用理赔',
        description: '住院医疗费用报销',
        status: 'processing',
        statusText: '处理中',
        applicationTime: '2024-01-10 09:15',
        claimAmount: 12000,
        policyNo: 'POL20240110002'
      },
      {
        id: '3',
        title: '重疾理赔',
        description: '重大疾病保险金申请',
        status: 'completed',
        statusText: '已完成',
        applicationTime: '2024-01-05 16:45',
        claimAmount: 300000,
        policyNo: 'POL20240105003'
      },
      {
        id: '4',
        title: '意外身故理赔',
        description: '意外身故保险金申请',
        status: 'rejected',
        statusText: '已拒绝',
        applicationTime: '2024-01-01 11:20',
        claimAmount: 500000,
        policyNo: 'POL20240101004'
      },
      {
        id: '5',
        title: '住院津贴理赔',
        description: '住院期间津贴申请',
        status: 'pending',
        statusText: '待处理',
        applicationTime: '2024-01-20 08:30',
        claimAmount: 2000,
        policyNo: 'POL20240120005'
      }
    ]
  },

  /**
   * 过滤理赔列表
   */
  filterClaims(claims, status) {
    if (status === 'all') {
      return claims
    }
    return claims.filter(claim => claim.status === status)
  },

  /**
   * 切换标签
   */
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    console.log('切换标签:', tab)
    
    this.setData({
      activeTab: tab,
      filteredClaims: this.filterClaims(this.data.claimsList, tab)
    })
  },

  /**
   * 跳转到理赔详情
   */
  goToClaimDetail(e) {
    const claimId = e.currentTarget.dataset.id
    console.log('跳转到理赔详情:', claimId)
    wx.navigateTo({
      url: `/pages/claim-detail/claim-detail?id=${claimId}`
    })
  },

  /**
   * 跳转到新理赔申请
   */
  goToNewClaim() {
    console.log('跳转到新理赔申请')
    wx.navigateTo({
      url: '/pages/new-claim/new-claim'
    })
  },

  /**
   * 加载更多
   */
  loadMore() {
    if (this.data.loading || !this.data.hasMore) return
    
    this.setData({ loading: true })
    
    console.log('加载更多理赔数据')
    
    // 模拟加载更多数据
    setTimeout(() => {
      const newClaims = this.getMockClaims().map((claim, index) => ({
        ...claim,
        id: (this.data.claimsList.length + index + 1).toString(),
        applicationTime: this.formatDate(new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000))
      }))
      
      const updatedClaims = [...this.data.claimsList, ...newClaims]
      
      this.setData({
        claimsList: updatedClaims,
        filteredClaims: this.filterClaims(updatedClaims, this.data.activeTab),
        loading: false,
        hasMore: updatedClaims.length < 20 // 模拟最多20条数据
      })
    }, 1000)
  },

  /**
   * 格式化日期
   */
  formatDate(date) {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    
    return `${year}-${month}-${day} ${hours}:${minutes}`
  }
})