// pages/memberIndex/memberIndex.js
var http = require("../../../utils/http.js");
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuthInfo: true,
    loginResult: {},
    userCenterInfo: {},
    current: 1,
    size: 10,
    scoreProdList: [],  //积分商品列表
    userInfo: {}, //个人信息
    userMemberInfo: {}, // 用户等级信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取会员中心信息
    this.getMemberCenterInfo()
    //请求积分商品列表
    this.getScoreProdList()
    //获取用户信息
    this.getUserInfo()
    // 获取用户会员信息
    this.getUserMemberInfo()
  },

  /**
   * 获取用户当前会员信息
   */
  getUserMemberInfo(){
    var param = {
      url: '/p/score/scoreLevel/page',
      method: 'GET',
      data: {
        levelType: 0,
      },
      callBack: (res) => {
        this.setData({
          userMemberInfo: res
        })
      }
    }
    http.request(param)
  },


  /**
   * 获取用户信息
   */
  getUserInfo: function() {
    var param = {
      url: '/p/user/userInfo',
      method: 'GET',
      data: {},
      callBack: (res) => {
        this.setData({
          userInfo: res
        })
      }
    }
    http.request(param)
  },


  /**
   * 获取会员中心信息
   */
  getMemberCenterInfo : function() {
    var params = {
      url: "/p/score/scoreInfo",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          userCenterInfo: res
        });
      }
    };
    http.request(params);
  },

  /**
   * 获取积分的方法（签到）
   */
  getPoints(e) {
    var index = e.currentTarget.dataset.index
    var params = {
      url: '/p/score/updateUserScore',
      method: 'GET',
      data: {},
      callBack: (res) => {
        console.log(res)
        wx.showToast({
          title: res,
          icon: 'none',
        })
        this.getMemberCenterInfo()
      }
    }
    if (this.data.userCenterInfo.isSignIn == 1 && this.data.userCenterInfo.signInCount == index + 1) {
      wx.showToast({
        title: '今天已经签到过啦~',
        icon: 'none'
      })
    } else if (this.data.userCenterInfo.isSignIn == 0 && this.data.userCenterInfo.signInCount == index + 1) {
      http.request(params)
    } else if (this.data.userCenterInfo.signInCount > index + 1) {
      wx.showToast({
        title: '这天已经签到过啦~',
        icon: 'none'
      })
    } else {

    }
  },

  /**
   * 跳转到积分明细(当前积分)
   */
  toIntegralDetail: function() {
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/integralDetail/integralDetail',
    })
  },

  /**
   * 跳转到会员中心(当前成长值)
   */
  toMemberCenter: function() {
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/memberCenter/memberCenter',
    })
  },

  /**
   * 购买会员跳转页面
   */
  buyMember: function() {
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/buyVip/buyVip'
    })
  },

  /**
   * 去购物
   */
  toIndexPage: function() {
    console.log(1111)
    wx.switchTab({
      url: '../../../pages/index/index',
    })
  },

  /**
   * 积分兑换 查看更多
   */
  goodsListViewMore: function() {
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/integralIndex/integralIndex'
    })
  },

  /**
   * 获取积分商品列表
   */
  getScoreProdList() {
    var param = {
      url: "/p/score/prodScorePage",
      method: "GET",
      data: {
        current: 1,
        size: 10,
      },
      callBack: (res) => {
        this.setData({
          scoreProdList: res.current==1? res.records : this.data.scoreProdList.concat(res.records),
          current: res.current,
          pages: res.pages
        })
        wx.hideLoading()
      }
    };
    http.request(param);
  },

  /**
   * 跳转积分商品详情
   */
  toScoreProdDet(e) {
    const prodId = e.currentTarget.dataset.scoreprodid
    wx.navigateTo({
      url: '/packageMemberIntegral/pages/convertProdDet/convertProdDet?prodId=' + prodId,
    })
  },

  /**
   * 跳转大转盘抽奖
   */
  toLuckyDraw(){
    wx.showToast({
      title: '即将上线，敬请期待~',
      icon:'none'
    })
  },


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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isAuthInfo: Boolean(wx.getStorageSync('loginResult').nickName),
      loginResult: wx.getStorageSync('loginResult')
    })
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current < this.data.pages) {
      this.setData({
        current: this.data.current + 1
      })
      this.getScoreProdList()
    }
  },

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

  }
})