// pages/mine/mine.js
const defaultAvatarUrl = "../../static/images/no-login.png"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showAuthorizedDialog: false,
    hasAuth: false,
    avatarUrl: defaultAvatarUrl,
    nickName: "用户登录",
    phoneNumber: "",
    menuList: [{
      icon: "../../static/mine/smbg.png",
      title: "睡眠报告",
      type: "switchTab",
      url: "/pages/data/data"
    }, {
      icon: "../../static/mine/dcwj.png",
      title: "调查问卷",
      type: "navigate",
      url: "/subpages/testSAS/testSAS"
    }, {
      icon: "../../static/mine/bzzx.png",
      title: "帮助中心",
      type: "navigate",
      url: ""
    }, {
      icon: "../../static/mine/gywm.png",
      title: "关于我们",
      type: "navigate",
      url: ""
    }]

  },
  onMenuClick(e) {
    console.log(e.currentTarget.dataset);
    const { index } = e.currentTarget.dataset;
    var hasAuth = wx.getStorageSync("hasAuth");
    switch (index - 0) {
      case 0:
      case 1:
        // need Login
        if (hasAuth) {
          this.toPage(index);
        } else {
          this.showDialog();
        }
        break;
      default:
        this.toPage(index);
        break;
    }
    
  },
  toPage(index) {
    const type = this.data.menuList[index].type;
    const url = this.data.menuList[index].url;
    if (!url) {
      return
    }
    if (type == 'switchTab') {
      wx.switchTab({
        url: url
      })
    } else if (type == 'navigate') {
      wx.navigateTo({
        url: url
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    app.globalData.selectedTabIndex = 3;
    this.getTabBar().setData({
      selected: 3
    })
  
    this.checkAuth()
  },
  showDialog() {
    this.setData({
      showAuthorizedDialog: true
    })
  },
  checkAuth() {
    var hasAuth = wx.getStorageSync("hasAuth");
    this.setData({
      hasAuth: hasAuth
    })
    if (hasAuth) {
      var userInfo = wx.getStorageSync("userInfo");
      this.setData({
        nickName: userInfo.nickName || "用户登录",
        avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
        phoneNumber: userInfo.phoneNumber || ""
      })
    }
    return hasAuth;
  },
  authorizationSuccessful() {
    console.log("授权成功");
    this.setData({ showAuthorizedDialog: false })
    this.checkAuth();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})