// pages/policy-detail/policy-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 保单ID
    policyId: '',
    
    // 保单信息
    policyInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('保单详情页加载', options)
    const { id } = options
    if (id) {
      this.setData({ policyId: id })
      this.loadPolicyDetail(id)
    } else {
      wx.showToast({
        title: '保单ID不能为空',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log('保单详情页渲染完成')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('保单详情页显示')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('保单详情页隐藏')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('保单详情页卸载')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉刷新')
    this.loadPolicyDetail(this.data.policyId)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('上拉触底')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '保单详情',
      path: `/pages/policy-detail/policy-detail?id=${this.data.policyId}`,
      imageUrl: '/static/images/share-policy-detail.jpg'
    }
  },

  /**
   * 加载保单详情
   */
  loadPolicyDetail(policyId) {
    console.log('加载保单详情:', policyId)
    
    // 模拟保单详情数据
    const mockPolicyInfo = this.getMockPolicyDetail(policyId)
    this.setData({ policyInfo: mockPolicyInfo })
  },

  /**
   * 获取模拟保单详情数据
   */
  getMockPolicyDetail(policyId) {
    const mockPolicies = {
      '1': {
        id: '1',
        policyNo: 'POL20240115001',
        productName: '综合意外伤害保险',
        status: 'active',
        statusIcon: '✓',
        statusText: '有效',
        statusDesc: '您的保单正在有效期内，享受全面保障',
        coverageAmount: 50,
        premium: 299,
        effectiveDate: '2024-01-15',
        expiryDate: '2025-01-15',
        paymentMethod: '年缴',
        insurancePeriod: '1年',
        insuredName: '张先生',
        idCard: '110101199001011234',
        phone: '138****1234',
        gender: '男',
        birthDate: '1990-01-01',
        beneficiary: {
          name: '张太太',
          relation: '配偶',
          idCard: '110101199201011234'
        },
        coverage: [
          {
            name: '意外身故',
            amount: '50万元',
            description: '因意外事故导致身故，给付保险金额'
          },
          {
            name: '意外伤残',
            amount: '最高50万元',
            description: '因意外事故导致伤残，按伤残等级给付'
          },
          {
            name: '意外医疗',
            amount: '5万元',
            description: '因意外事故产生的医疗费用，实报实销'
          },
          {
            name: '住院津贴',
            amount: '200元/天',
            description: '因意外事故住院，每日给付津贴'
          }
        ],
        paymentRecords: [
          {
            period: '2024年保费',
            amount: 299,
            status: 'paid',
            statusText: '已缴费',
            paymentDate: '2024-01-15'
          }
        ],
        claimsRecords: [
          {
            type: '意外医疗',
            amount: 2500,
            status: 'completed',
            statusText: '已完成',
            date: '2024-01-20'
          }
        ]
      },
      '2': {
        id: '2',
        policyNo: 'POL20240110002',
        productName: '重大疾病保险',
        status: 'active',
        statusIcon: '✓',
        statusText: '有效',
        statusDesc: '您的保单正在有效期内，享受重疾保障',
        coverageAmount: 30,
        premium: 1200,
        effectiveDate: '2024-01-10',
        expiryDate: '2025-01-10',
        paymentMethod: '年缴',
        insurancePeriod: '1年',
        insuredName: '李女士',
        idCard: '110101198501011234',
        phone: '139****5678',
        gender: '女',
        birthDate: '1985-01-01',
        beneficiary: {
          name: '李女士',
          relation: '本人',
          idCard: '110101198501011234'
        },
        coverage: [
          {
            name: '重大疾病',
            amount: '30万元',
            description: '确诊重大疾病，一次性给付保险金额'
          },
          {
            name: '轻症疾病',
            amount: '6万元',
            description: '确诊轻症疾病，给付20%保险金额'
          },
          {
            name: '身故保障',
            amount: '30万元',
            description: '因疾病或意外身故，给付保险金额'
          }
        ],
        paymentRecords: [
          {
            period: '2024年保费',
            amount: 1200,
            status: 'paid',
            statusText: '已缴费',
            paymentDate: '2024-01-10'
          }
        ],
        claimsRecords: []
      }
    }
    
    return mockPolicies[policyId] || mockPolicies['1']
  },

  /**
   * 跳转到续保
   */
  goToRenewal() {
    console.log('跳转到续保')
    wx.navigateTo({
      url: `/pages/renewal/renewal?policyId=${this.data.policyId}`
    })
  },

  /**
   * 跳转到理赔申请
   */
  goToClaim() {
    console.log('跳转到理赔申请')
    wx.navigateTo({
      url: `/pages/claims/claims?policyId=${this.data.policyId}`
    })
  },

  /**
   * 下载保单
   */
  downloadPolicy() {
    console.log('下载保单')
    wx.showModal({
      title: '下载保单',
      content: '是否下载电子保单到本地？',
      confirmText: '下载',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '保单下载中...',
            icon: 'loading'
          })
          
          // 模拟下载
          setTimeout(() => {
            wx.showToast({
              title: '下载成功',
              icon: 'success'
            })
          }, 2000)
        }
      }
    })
  },

  /**
   * 返回上一页
   */
  goBack() {
    console.log('返回上一页')
    wx.navigateBack()
  }
})