// pages/usercenter/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: 'https://img.yzcdn.cn/vant/cat.jpeg',
      nickName: '美丽用户',
      phone: '138****8888',
      level: '黄金会员',
      points: 520
    },
    
    // 我的预约
    appointmentList: [],
    
    // 功能列表
    functionList: [
      { id: 1, name: '我的预约', icon: 'calendar', url: '/pages/appointment/index' },
      { id: 2, name: '会员卡', icon: 'card', url: '/pages/member/index' },
      { id: 3, name: '优惠券', icon: 'discount', url: '/pages/coupon/index' },
      { id: 4, name: '积分商城', icon: 'shop', url: '/pages/points/index' }
    ],
    
    // 服务列表
    serviceList: [
      { id: 1, name: '联系客服', icon: 'service', type: 'contact' },
      { id: 2, name: '意见反馈', icon: 'chat', url: '/pages/feedback/index' },
      { id: 3, name: '关于我们', icon: 'info-circle', url: '/pages/about/index' },
      { id: 4, name: '设置', icon: 'setting', url: '/pages/settings/index' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取用户信息
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        'userInfo.nickName': userInfo.nickName,
        'userInfo.avatarUrl': userInfo.avatarUrl
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().init();
    }
    
    // 获取预约列表
    const appointmentList = wx.getStorageSync('appointmentList') || [];
    this.setData({ appointmentList });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  /**
   * 跳转到功能页面
   */
  goToFunction(e) {
    const url = e.currentTarget.dataset.url;
    const type = e.currentTarget.dataset.type;
    
    if (type === 'contact') {
      this.contactCustomerService();
      return;
    }
    
    if (url) {
      wx.navigateTo({
        url: url
      });
    }
  },
  
  /**
   * 联系客服
   */
  contactCustomerService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567'
    });
  },
  
  /**
   * 查看预约详情
   */
  viewAppointmentDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/appointment/index?appointmentId=${id}`
    });
  },
  
  /**
   * 取消预约
   */
  cancelAppointment(e) {
    const id = e.currentTarget.dataset.id;
    const appointmentList = this.data.appointmentList.filter(item => item.id !== id);
    
    // 更新本地存储
    wx.setStorageSync('appointmentList', appointmentList);
    
    this.setData({ appointmentList });
    
    wx.showToast({
      title: '已取消预约',
      icon: 'success'
    });
  }
})