// pages/point/point.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var pageSize = 1
var isregistered 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab切换  
    currentTab: 0,
    clientHeight: '',
    idAddPoint:'idAddPoint',
    idMinusPoint:'idMinusPoint',

    /** 
      * 页面配置 
      */
    winWidth: 0,
    winHeight: 0,

    addList:[],
    minusList:[],
    points_rank:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          clientHeight: res.windowHeight
        });
      }
    })
    
  },
 
 gotoWxlogin:function(){
    wx.showModal({
      title: '提示',
      content: '您当前尚未进行微信授权登录，是否立即前往?',
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '../mine/mine',
          })
        } else if (res.cancel) {
          return
        }
      }
    })
  
 },
  getHisPointData: function () {
    var that = this
    var data = { 
      openid: wx.getStorageSync('openid'),
      pageCount:20,
      pageSize: pageSize
      }
    var url = baseUrl + BASEURL.getUrl.getAllRank
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if(res.data.success && res.data.data){
       if(pageSize == 1){
         that.setData({
           addList: res.data.data.addList,
           minusList: res.data.data.minusList,
           points_rank: res.data.data.points_rank
         })
       }else{
         that.setData({
           addList: [...that.data.addList, ...res.data.data.addList],
           minusList: [...that.data.minusList, ...res.data.data.minusList],
           points_rank: [...that.data.points_rank, ...res.data.data.points_rank],
         })

       }
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })


  },

  btnClick: function (event) {
    var that = this
    var id = event.currentTarget.id
    var wxnickName = wx.getStorageSync('wxnickName')
    console.log('wxnickName', wxnickName)
    if (!wxnickName) {
      wx.showModal({
        title: '提示',
        content: '您当前尚未进行微信授权登录，是否立即前往?',
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../mine/mine',
            })
          } else if (res.cancel) {
            return
          }
        }
      })
      return
    } else 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.idAddPoint){
      wx.navigateTo({
        url: '../pointapply/pointapply',
      })
    } else if (id == that.data.idMinusPoint){
      wx.navigateTo({
        url: '../minusapply/minusapply',
      })
    }

  },
  addListClick:function(event){
    // console.log("点击了", event.currentTarget.dataset.item)
    var itemdata = event.currentTarget.dataset.item
    wx.navigateTo({
      url: '../pointapply/pointapply?getisDetails=' + true + '&getItemdata=' + JSON.stringify(itemdata),
    })
  },
  minusListClick: function (event){
    var itemdata = event.currentTarget.dataset.item
    wx.navigateTo({
      url: '../minusapply/minusapply?getisDetails=' + true + '&getItemdata=' + JSON.stringify(itemdata),
    })
  },
  /** 
    * 点击tab切换 
    */
  swichNav: function (e) {
    var that = this;
    // console.log('swichNav=', e)
    if (this.data.currentTab == e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  /** 
* 滑动切换tab 
*/
  bindChange: function (e) {
    var that = this;
    if (e.detail.source == 'touch') {
      that.setData({ currentTab: e.detail.current });
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    isregistered = wx.getStorageSync('isregistered')
    if (isregistered) {
      this.getHisPointData()
    } else {
      // that.gotoWxlogin()
    }
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载
    pageSize = 1
    that.getHisPointData()
    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    pageSize++;
    this.getHisPointData()
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function () { wx.hideNavigationBarLoading() }, 2000);
  },

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

  }
})