// pages/person/person.js

const {
  user
} = require("../../utils/const")

const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    nickName: "请登录",
    displayUserId: "",
    menuList: [
      {
        id: "myOrder",
        title: "我的订单",
      },
      {
        id: "logout",
        color: 'red',
        title: "退出登录",
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userInfo = wx.getStorageSync("userInfo")
    if (!userInfo) {
      console.log("用户未登录")
      wx.redirectTo({
        url: '/pages/index/login'
      })
      return
    } else {
      console.log("userInfo", userInfo)
      this.setData({
        nickName: userInfo.realname,
        displayUserId: "用户名: " + userInfo.username,
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  onChooseAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl,
    })
  },

  onGetUserInfo(e) {
    let userInfo = JSON.parse(e.detail.rawData)
    console.log("e", userInfo)
    this.setData({
      avatarUrl: userInfo.avatarUrl,

    })
  },

  menuTap: function (event) {
    console.log("event.target", event.target)
    let menuId = event.target.id
    switch (menuId) {
      // 我的订单
      case "myOrder":
        wx.navigateTo({
          url: '/pages/shop/myOrder'
        })
        break

      // 退出登录
      case "logout":
        wx.clearStorageSync()
        app.globalData.userInfo = null
        console.log("userInfo", wx.getStorageSync("userInfo"))
        wx.redirectTo({
          url: '/pages/index/login'
        })
        break
      default:
        break
    }
  }
})
