import {
  get,
  post
} from '../../utils/http'
import {
  formatActivity
} from '../../utils/time'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    agree: false,
    visible: false,
    sum: 0,
    activity: {},
    needReqActivity: true,
    inserllAmount: 0,
    inserllAmountLittle: '00',
    CardDiscount: '', //是否购买次卡拥有美妆折扣
    goods_discount: '', //是否存在商品权益，1是，0否
    level: '', //用户折扣等级 (0是未拥有等级)
    levelList: {
      1: '0.98',
      4: '0.95',
      7: '0.90',
    }, //折扣列表
    levelImage: ['https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220920/%E7%BC%96%E7%BB%84%208%E5%A4%87%E4%BB%BD%205%402x.png', 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220920/%E7%BC%96%E7%BB%84%208%E5%A4%87%E4%BB%BD%202%402x.png', 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220920/%E7%BC%96%E7%BB%84%208%E5%A4%87%E4%BB%BD%207%402x.png']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取用户折扣等级
    this.getLevel()
    this.getUser()
    this.getAmount()
    this.getCoin()
    if (wx.getStorageSync('token') && this.data.needReqActivity) {
      this.getUserActivity()
    }
    this.setData({
      agree: wx.getStorageSync('agree') || false
    })
  },

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.timer_) {
      clearInterval(this.timer_)
    }
  },

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 点击头像切换环境
  onAvatar() {
    this.data.sum++
    if (this.data.sum >= 10) {
      this.setData({
        visible: true,
        sum: 0
      })
    }
  },
  // 获取用户折扣等级
  getLevel() {
    get('/v2/api/user/discount_level', {}, (res) => {
        let arr = [1,4,7]
       let index =  arr.findIndex(item=>{
          return item==res.data
        })
        this.setData({
          imageIndex:index
        })
      if (res.code == 200) {
        if (res.data == 0) {
          this.setData({
            level: 0
          })
          return
        }
        let getValue = (item, val) => {
          return item[val]
        }
        this.setData({
          level: Number(getValue(this.data.levelList, res.data))
        })
      }
    })
  },
  /**
   * 获取用户信息
   * api/user
   */
  getUser() {
    get('api/user', {}, (res) => {
      if (res.data) {
        this.setData({
          userInfo: res.data,
          goods_discount: res.data.goods_discount
        }, )
        wx.setStorageSync('userInfo', res.data)
      }
    })
  },

  /**
   * 获取我的储值
   * api/user
   */
  getAmount() {
    get('api/user/amount', {}, (res) => {
      if (res.data) {
        this.setData({
          inserllAmount: res.data.amount.split('.')[0],
          inserllAmountLittle: res.data.amount.split('.')[1],
        })
      }
    })
  },

  /**
   * 获取我的积分
   * api/user
   */
  getCoin() {
    get('api/user/coin', {}, (res) => {
      if (res.data) {
        this.setData({
          coin: res.data.coin_num
        })
      }
    })
  },


  /**
   * 跳转vip页面
   */
  goToVip() {
    console.log(this.data.userInfo)
    let nextDatas = JSON.stringify(this.data.userInfo)
    wx.navigateTo({
      url: `/pages/vip/vip?userInfo=${encodeURIComponent(nextDatas)}`
      // url: `/pages/vip/vip?userInfo=${JSON.stringify(this.data.userInfo)}`
      // url: '/pages/vip/vip?userInfo='+ JSON.stringify(this.data.userInfo),
    })
  },

  /**
   * 获取首次开卡优惠
   * api/user/activity
   */
  getUserActivity() {
    if (this.timer) {
      clearInterval(this.timer_)
    }
    get('api/user/activity', {}, (res) => {
      this.data.needReqActivity = false
      if (res.data) {
        res.data.currentTime = formatActivity(res.data.expire_time_seconds)
        this.setData({
          activity: res.data
        })
        this.timer_ = setInterval(() => {
          if (res.data.expire_time_seconds <= 0) {
            clearInterval(this.timer_)
          }
          res.data.expire_time_seconds--
          res.data.currentTime = formatActivity(res.data.expire_time_seconds)
          this.setData({
            activity: res.data
          })
        }, 1000)
      }
    })
  },
  // 获取是否拥有购买次卡后拥有的美妆折扣
  getCardDiscount() {
    get('', {}, (res) => {
      this.setData({
        CardDiscount: ''
      })
    })
  }
})