// pages/userCenter.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    integralNum: 0,
    pageNo: 1,
    pageSize: 30,
    integralList: [],
    noMore: false,
    memberPoint : {
      total: '--',
      expirePoint: 0,
      expireDate: ''
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.refreshPointRequest();
    wx.hideShareMenu();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  //刷新积分请求
  refreshPointRequest(){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    if (!memberId) {
      return;
    }
    wxService.get('/member/memberPoint/refreshExpiredPoint?memberId=' + memberId).then(res => {})
  },
  //查询会员积分信息
  getMemberIntegrateInfo() {
    // wx.showLoading({
    //   title: '数据请求中..',
    // });
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    if (!memberId) {
      return;
    }
    
    wxService.get(`/member/memberPoint/getMemberPointInfo?memberId=${memberId}`).then(res => {
      let data = res.data.data ? res.data.data : {};
      this.data.memberPoint.total = data.point ? data.point : 0;
      this.data.memberPoint.expirePoint = data.pointWillExpired ? data.pointWillExpired : 0;
      let expireDate = data.pointExpiredTime ? data.pointExpiredTime : '2019-12-31 23:59:59';
      if (expireDate){
        let dateStr = expireDate.replace(/-/g, '/');
        let da = new Date(dateStr);
        let year = da.getFullYear();
        let month = (da.getMonth() + 1) < 10 ? '0' + (da.getMonth() + 1) : da.getMonth() + 1;
        let day = da.getDate() < 10 ? '0' + da.getDate() : da.getDate();
        expireDate = `${year}年${month}月${day}日`
      }

      this.data.memberPoint.expireDate = expireDate;
      this.setData({
        memberPoint: this.data.memberPoint
      });
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 积分流水
    this.getIntegralList()
    // 查询会员积分信息
    this.getMemberPointInfo()
    this.getMemberIntegrateInfo();
  },
  // 查询会员积分信息
  getMemberPointInfo(){
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/member/memberPoint/getMyPointInfo`).then(res => {
      if(res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            integralNum: data.point
          })
        }
      }
    })
  },
  getIntegralList() {
    wx.showLoading({
      title: '加载中'
    })
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    if (!memberId) {
      return;
    }
    const params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    }
    wxService.post(`/member/memberPointLog/findPage?pageNo=${this.data.pageNo}&pageSize=${this.data.pageSize}`, params).then(res=> {
      if(res) {
        const {result,data} = res.data
        if(result == 0){
           wx.hideLoading()
           this.setData({
             integralList: data.content.slice(0,30)
           })
        }
      }
    })
  },
  // 点击跳转至会员规则页
  handleGoMemberRulesRules() {
    wxService.router(`/pages/memberRules/memberRules?type=1`)
  },
    // 去积分商城
  handleGoIntegralMall() {
    // wxService.router(`/pages/IntegralMall/IntegralMall`)
    // wxService.router(`/subPackage/page/pages/IntegralMallGoods/IntegralMallGoods`)
    //现在修改为跳转到积分活动分组列表
    wxService.router(`/subPackageA/page/pages/pointActicityGroup/pointActivityGroup`);
  },
  // 消费详情
  handleGoDetail(e) {
    const { memberid, expandinfo, changetype} = e.currentTarget.dataset
    // orderNo 存在 跳转订单详情
    if (changetype == 26){ // 积分换物
      wxService.router(`/subPackage/page/pages/recordList/recordList`)
    }
    else{
      if (expandinfo && expandinfo.orderNo) {
        let orderNo = expandinfo.orderNo
        wxService.router(`/pages/consumptionDetails/consumptionDetails`).search({ orderNo })
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      noMore: true
    })
  },

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

  }
})