import { mockUserProfile } from '../../utils/mock-data'
import { getMenuRoute, navigateTo } from '../../utils/router'

// TODO: 联调时替换为真实 API 获取用户信息
Page({
  data: {
    userInfo: mockUserProfile,
  },

  onShow() {
    // 同步 custom-tab-bar 选中态
    const tabBar = this.getTabBar?.()
    if (tabBar) tabBar.setData({ active: 'my' })
    // TODO: 联调时在此刷新用户信息
  },

  onMenuTap(e: WechatMiniprogram.TouchEvent) {
    const key = e.currentTarget.dataset.key as string
    const route = getMenuRoute(key)
    if (route) {
      navigateTo(route)
    }
  },

  onLogout() {
    wx.showModal({
      title: '确认退出',
      content: '确认退出当前账号吗？',
      confirmColor: '#e34d59',
      success(res) {
        if (res.confirm) {
          wx.clearStorageSync()
          wx.reLaunch({ url: '/pages/login/login' })
        }
      },
    })
  },
})
