// pages/welfareMall/personal/personal.js
import WelfareMall from '../../../api/welfareMall'
import activity from '../../../api/activity'
import {getMobileCache, getPhoneNumber as getPhoneNumberSync} from '../../../utils/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    mobileTop: 'TONY WU',
    countNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // await this.getUserPhone();
    let  userMobile = await activity.getMobileCache();
    console.log(userMobile)
    if (userMobile.length !== 0) {
      this.setData({
        isLogin: true,
        mobileTop: userMobile,
      })
    }
    // this.setData({
    //   isLogin: getMobileCache() != '',
    //   mobileTop: getMobileCache(),
    // })

    if(this.data.isLogin){
      let nums = await this.getCountNum();
      this.setData({
        countNum: nums,
      })
    }

  },

  /**
   * 获取用户信息
   */
  getUserPhone: async function(){
    await WelfareMall.getUser({
      openid: activity.getOpenId(),
      source:'FREE_MINI_APP',
    });

  },

  getCountNum: async function(){
  /**
   * 福利社
   */
    let resBuy = await WelfareMall.getUserBuyCouponNum({
      mobile: getMobileCache(),
    });
    let resAward = await  WelfareMall.getUserAwardCouponNum({
      mobile: getMobileCache(),
    });
    let resDraw = await  WelfareMall.getUserDrawCouponNum({
      mobile: getMobileCache(),
    });
    let nums = Number(resBuy.data.notUseNum)+Number(resAward.data.notUseNum)+Number(resDraw.data.notUseNum)
    return nums;
  },

  // 授权手机号
  getPhoneNumber(e) {
    getPhoneNumberSync(e, async () => {
        let nums = await this.getCountNum();
        this.setData({
            isLogin: true,
            mobileTop: getMobileCache(),
            countNum: nums,
        })       
    })
  },

  toHistorical(e) {
      const url = "../historical/historical?id=" + e.currentTarget.dataset.id
      wx.navigateTo({
          url
      })
  },

  toFeedback(e) {
    const url = "../feedback/feedback?id=" + e.currentTarget.dataset.id
    wx.navigateTo({
      url: url,
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})