// pages/profile/profile.js - 我的页面
Page({
  data: {
    userInfo: {
      name: 'AIOTAGRO',
      phone: '15588823774',
      avatar: '/images/logo.png'
    },
    menuList: [
      {
        id: 1,
        title: '养殖系统设置',
        icon: '/images/profile/settings.png',
        url: '/pages/profile/system-settings/system-settings'
      },
      {
        id: 2,
        title: '切换养殖场',
        icon: '/images/profile/switch-farm.png',
        url: '/pages/profile/switch-farm/switch-farm'
      },
      {
        id: 3,
        title: '养殖场识别码',
        icon: '/images/profile/qrcode.png',
        url: '/pages/profile/farm-qrcode/farm-qrcode'
      },
      {
        id: 4,
        title: '关联机构',
        icon: '/images/profile/organization.png',
        url: '/pages/profile/organization/organization'
      },
      {
        id: 5,
        title: '首页自定义',
        icon: '/images/profile/customize.png',
        url: '/pages/profile/customize/customize'
      },
      {
        id: 6,
        title: '养殖场设置',
        icon: '/images/profile/farm-settings.png',
        url: '/pages/profile/farm-settings/farm-settings'
      }
    ]
  },

  onLoad() {
    console.log('我的页面加载')
    this.loadUserInfo()
  },

  onShow() {
    console.log('我的页面显示')
    this.loadUserInfo()
  },

  // 加载用户信息
  loadUserInfo() {
    const app = getApp()
    
    if (!app.globalData.isLogin) {
      // 未登录，跳转到登录页
      wx.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: false,
        success: () => {
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }
      })
      return
    }
    
    // 从全局数据或本地存储获取用户信息
    const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo')
    
    if (userInfo) {
      this.setData({
        userInfo: {
          name: userInfo.name || 'AIOTAGRO',
          phone: userInfo.phone || userInfo.username || '15588823774',
          avatar: userInfo.avatar || '/images/logo.png'
        }
      })
    }
  },

  // 菜单点击
  handleMenuClick(e) {
    const index = e.currentTarget.dataset.index
    const menu = this.data.menuList[index]
    
    if (menu.url) {
      wx.navigateTo({
        url: menu.url,
        fail: () => {
          wx.showToast({
            title: '功能开发中',
            icon: 'none'
          })
        }
      })
    } else {
      wx.showToast({
        title: '功能开发中',
        icon: 'none'
      })
    }
  },

  // 退出登录
  handleLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      confirmText: '退出',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          // 清除登录信息
          wx.clearStorageSync()
          
          const app = getApp()
          app.globalData.isLogin = false
          app.globalData.userInfo = null
          app.globalData.token = ''
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
          
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login'
            })
          }, 1500)
        }
      }
    })
  }
})
