// pages/mine/mine.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var dateutil = require('../../utils/DateTime.js')
var isregistered 

Page({

  /**
   * 页面的初始数据
   */
  data: {
   //id
    idTypeRank:'idTypeRank',
    isregistered:false,
    idMyInfo:'idMyInfo',
    idCompanyInfo: 'idCompanyInfo',
    idAbout: 'idAbout',
    idgoBind:'idgoBind',
    idWxlogin:'idWxlogin',
    pointdata:{},//本月、上月积分及排名

    "curmonth_points": 0, //本月当前积分
    "curmonth_rank": '-',//本月当前排名
    "lastmonth_points": 0,//上月积分
    "lastmonth_rank": '-' //上月排名
  },

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

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  btnClick: function (event) {
    var that = this
    var id = event.currentTarget.id
    if (id == that.data.idAbout) {
      wx.navigateTo({
        url: '../about/about',
      })
      return
    } 
    isregistered = wx.getStorageSync('isregistered')
    console.log('isregistered', isregistered)
    if (id == that.data.idWxlogin){
      return
    }
    if (!wx.getStorageSync('wxnickName')) {
      that.comToast('您当前尚未进行微信授权登录')
      return
    }
    if (id == that.data.idgoBind) {
      wx.navigateTo({
        url: '../bindphone/bindphone',
      })
      return
    }
    if (id == that.data.idTypeRank){
      wx.navigateTo({
        url: '../pointRank/pointRank',
      })
    }
    if (!isregistered) {
      wx.showModal({
        title: '提示',
        content: '您当前尚未与企业关联绑定，是否立即去绑定?',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../bindphone/bindphone',
            })
          } else if (res.cancel) {
            return
          }
        }
      })
      return
    }

    if (id == that.data.idMyInfo){
        wx.navigateTo({
          url: '../perinfo/perinfo',
        })
    } else if (id == that.data.idCompanyInfo){
      wx.navigateTo({
        url: '../corporateInfo/corporateInfo',
      })
    } 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    this.setData({
      isregistered: wx.getStorageSync('isregistered'),
      isLogin: wx.getStorageSync('wxnickName') ? true : false,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      headerImg: wx.getStorageSync('wxavatarUrl'),
      wxNickname: wx.getStorageSync('wxnickName'),
    })
    isregistered = wx.getStorageSync('isregistered')
    if (!isregistered){
      return
    }
    that.getMyRank()
  },

  getMyRank:function(){
  var that = this
  var data = { 
    openid: wx.getStorageSync('openid'),
   }
    var url = baseUrl + BASEURL.getUrl.getRank
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data.success){
        wx.setStorageSync('isregistered', res.data.data.isregistered)
        if (res.data.data.pointdata){
          var pointdata = res.data.data.pointdata
          that.setData({
            isregistered: res.data.data.isregistered,
            pointdata: pointdata,
            "curmonth_points": pointdata.curmonth_points == null ? '0' : pointdata.curmonth_points, //本月当前积分
            "curmonth_rank": pointdata.curmonth_rank == null ? '0' : pointdata.curmonth_rank,//本月当前排名
            "lastmonth_points": pointdata.lastmonth_points == null ? '0' : pointdata.lastmonth_points,//上月积分
            "lastmonth_rank": pointdata.lastmonth_rank == null ? '-' : pointdata.lastmonth_rank //上月排名
          })

        }
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  bindGetUserInfo: function (e) {
    var that = this
    var userInfo = e.detail.userInfo
    if (!userInfo) return
    console.log('bindGetUserInfo', userInfo)
    var nickName = userInfo.nickName
    var avatarUrl = userInfo.avatarUrl
    var gender = userInfo.gender //性别 0：未知、1：男、2：女
    var province = userInfo.province
    var city = userInfo.city
    var country = userInfo.country
    if (nickName && avatarUrl) {
      baseutils.getPersonlEmp()
      baseutils.judgeisReg()
      that.getMyRank()
      that.setData({
        wxNickname: nickName,
        wxHeader: avatarUrl,
        isLogin: true
      })
      try {
        wx.setStorageSync('wxavatarUrl', userInfo.avatarUrl)
        wx.setStorageSync('wxnickName', userInfo.nickName)
      } catch (e) {
      }
    }

  },

  comToast: function (title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },








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

  },

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

  },

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

  },

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

  },

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

  }
})