// pages/mine/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toolHeight: app.globalData.statusBarHeight,
    loginStatus: wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
    roleCodeList: wx.getStorageSync('roleCodeList') ? wx.getStorageSync('roleCodeList') : [],
    avatar: '',
    name: '',
    phone: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.login({
      success: (res) => {
        console.log(res.code)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const roleCodeList = wx.getStorageSync('roleCodeList')
    let loginStatus = wx.getStorageSync('loginStatus')
    this.setData({
      roleCodeList: roleCodeList ? roleCodeList : [],
      loginStatus: loginStatus ? loginStatus : false,
      avatar: wx.getStorageSync('avatar') ? wx.getStorageSync('avatar') : '/images/headerImg.png',
      name: wx.getStorageSync('name') ? wx.getStorageSync('name') : '未填写',
      phone: wx.getStorageSync('phone') ? wx.getStorageSync('phone') : '手机号',
    })
    // let selected = 2
    // //动态tabbar
    // if (roleCodeList == 'bizAdmin') {
    //   this.getTabBar().setData({
    //     list: app.globalData.allList[0].list2,
    //   })
    //   selected = 4
    // } else if (roleCodeList == 'send') {
    //   this.getTabBar().setData({
    //     list: app.globalData.allList[0].list3,
    //   })
    //   selected = 1
    // } else if (roleCodeList == 'sale') {
    //   this.getTabBar().setData({
    //     list: app.globalData.allList[0].list4,
    //   })
    // } else if (roleCodeList == 'customer') {
    //   this.getTabBar().setData({
    //     list: app.globalData.allList[0].list5,
    //   })
    // } else if (roleCodeList == 'driver' || roleCodeList == '') {
    //   this.getTabBar().setData({
    //     list: app.globalData.allList[0].list1,
    //   })
    // }
    // if (typeof this.getTabBar === 'function' && this.getTabBar()) {
    //   this.getTabBar().setData({
    //     selected: selected
    //   })
    // }
  },

  //获取用户信息
  getInfo: function () {

  },

  //绑定微信
  bindingWX: function () {
    let _this = this;
    if (this.data.loginStatus) {
      wx.showModal({
        title: '提示',
        content: '确定绑定该微信账号？',
        success: function (res) {
          if (res.confirm) {
            wx.login({
              success: (res) => {
                app.request.POST({
                  url: app.API.bindingWX,
                  params: {
                    'id': wx.getStorageSync('id'),
                    'js_code': res.code
                  },
                  page: _this,
                  isLoadingTxt: '绑定中...',
                  isSubmitting: true,
                  successFun: true
                }).then(res => {
                  wx.showToast({
                    title: '绑定成功',
                    icon: 'success',
                    duration: 2000,
                    complete: function () {
                      setTimeout(() => {
                        wx.navigateBack()
                      }, 1500) //延迟时间  
                    }
                  })
                })

              },
            })

          }
        }
      })
    } else {
      wx.showToast({
        title: '您还未登录！',
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }
  },

  toLogin: function () { //跳转至账号登录页面
    wx.navigateTo({
      url: '../login/index?page=mine',
    })
  },

  // 菜单页面跳转
  toLink: function (e) {
    if (this.data.loginStatus) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '登录后可操作',
        duration: 1500
      })
    }
  },

  //退出登录
  logout: function (e) {
    let _this = this
    if (this.data.loginStatus) {
      wx.showModal({
        title: '提示',
        content: '确定退出登录？',
        success: function (res) {
          if (res.confirm) {
            app.request.GET({
              url: app.API.logout,
              page: _this,
              successFun: true
            }).then(res => {
              wx.clearStorageSync()
              _this.setData({
                loginStatus: false
              })
              _this.onShow()
              // _this.getTabBar().setData({
              //   list: app.globalData.allList[0].list1,
              // })
              wx.showToast({
                title: '退出成功',
                icon: 'success',
                duration: 2000,
                complete: function () {
                  setTimeout(function () {
                    // wx.reLaunch({
                    //   url: '/pages/login/index',
                    // })
                  }, 1500) //延迟时间 
                }
              })
            })
          }
        }
      })
    } else {
      app.util.checkForm('登录后可操作')
    }

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})