const { default: activity } = require("../../api/activity");

import {getMobileCache, getPhoneNumberNew as getPhoneNumberSync} from '../../utils/user'

// pages/activityList/activityList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userMobile:'',
     //火热
     list: [], //卡券列表
     page:1,
     pageNum:10,
     count:0,
   

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let _self = this;
    //获取列表
    _self.getList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

    var totalPage = Math.ceil(this.data.count/this.data.pageNum)
    console.log('到底了')
    var page = this.data.page
    page++;
    this.setData({
      page:page
    })
    if(page<=totalPage){
      console.log(123)
      this.getList();
    }

  },

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

  },
  goToShare(e){
    console.log(e)
    let _self = this;
    wx.navigateTo({
      url: '/pages/activityInfo/activityInfo?id='+e.currentTarget.dataset.id+'&mobile='+_self.data.userMobile,
    })
  },
  //获取手机号
  getPhoneNumber(e) {

    let _self = this;
    getPhoneNumberSync(e, async () => {
      let userMobile = activity.getMobileCache();
      console.log(userMobile)
      if (userMobile.length !== 0) {
        _self.setData({
          userMobile: userMobile,
        })
        //判断该手机号在不在分享名单里
        _self.isShareByMobile();
      }
    })
    // let _self = this;
    // var encryptedData = e.detail.encryptedData;
    // console.log(encryptedData);
    // var iv = e.detail.iv;
    // if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
    //   return;
    // }
    //获取手机号
    // getApp().doDecodePhone(encryptedData, iv, function () {
    //   let userMobile = activity.getMobileCache();
    //   console.log(userMobile)
    //   if (userMobile.length !== 0) {
    //     _self.setData({
    //       userMobile: userMobile,
    //     })
    //     //判断该手机号在不在分享名单里
    //     _self.isShareByMobile();
    //   }
    //
    // });
  },

  //获取活动列表
  getList: async function() {
    let _self = this;
    var result = await activity.getActivityList(this.data.page,this.data.pageNum,2);

    var list = this.handleCouponList(result.list);
    if(this.data.page != 1){
      var nowResult = this.data.list.concat(list)
    }else{
      var nowResult = list
    }
    //判断该电话是否存在 如果不存在 取第一条
    if(_self.data.userMobile=='') {
      nowResult = nowResult.length>0?[nowResult[0]]:nowResult;
    }
   
    this.setData({
      list:nowResult,
      count:result.count,
      page:result.page
    })
  },

  async isShareByMobile(){
    let _self = this;
    var result = await activity.isShareByMobile(_self.data.userMobile);
    if(result.isShare==1){
      _self.setData({
        page:1
      })
      _self.getList();
    }
  },
  handleCouponList(list) {
    if (list.length === 0) {
      return list;
    }

    var objSwitch = getApp().globalData.typeSwitchObj
    for (let i = 0; i < list.length; i++) {

      var couponList = list[i].activityCouponList;

      var couponTypeAndNumObj = {};
      for (let j = 0; j < couponList.length; j++) {
        var couponNum = couponList[j]['num'] ? couponList[j]['num'] : 1;

        var value = couponList[j]['coupon']['type'];
        var showText = objSwitch[couponList[j]['coupon']['type']];
        
        var nowNume = 0;
        if (couponTypeAndNumObj[value]) {
          nowNume = couponTypeAndNumObj[value]['num'];
          console.log(nowNume)
        }
        couponTypeAndNumObj[value] = {
          "typeText": showText,
          "num": couponNum + nowNume
        }
    
      }
      list[i].couponTypeAndNum = couponTypeAndNumObj
    }
    console.log(list);
    return list;
  },
})