const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px';
// pages/forgetpwd/forgetpwd.js
let config = require('../../config/config.js')
let Http = require('../../utils/HttpBasics')
const app = getApp()

Page({
  data: {
    navigationBarHeight,
    phone: '',
    yzm: '',
    retry: false,
    time: 60,
    couponChannelId: null,
    couponId:null,
    path:null,
    paramData: null
  },
  onLoad: function (options) {
    if(options.path=='index'){
      this.path = options.path
    }
    this.setData({
      paramData: options
    })
    var that = this;
    that.setData({
      couponChannelId: options.couponChannelId,
      couponId: options.couponId,
    })
  },
  getYZM(e) {
    this.setData({
      phone: e.detail.value.phone,
      yzm: e.detail.value.yzm
    })
  },
  next() {
    var that = this
    Http.get({
        url: config.api.validationCode,
        data: {
          phone: that.data.phone,
          code: that.data.yzm,
          type: 1,
          appid: app.globalData.appId
        }
      })
      .then(res => {
        /**
         * 来自积分兑换
         */
        if (that.data.path == 'jifen') {
          wx.redirectTo({
            url: `/pages/integralmall/payIntegcoupondetail/index?couponChannelId=${that.data.paramData.couponChannelId}&couponId=${that.data.paramData.couponId}`
          })
          return;
        }
        /**
         * 来自拼团详情
         */
        if (that.data.path == 'spell') {
          if (that.data.paramData.avatarUrl) {
            wx.redirectTo({
              url: `/pages/joinFrDpell/index?couponId=${that.data.paramData.couponId}&orderGroupId=${that.data.paramData.orderGroupId}&couponChannelId=${that.data.paramData.couponChannelId}&orderId=${that.data.paramData.orderId}&avatarUrl=${that.data.paramData.avatarUrl}&nickName=${that.data.paramData.nickName}`
            })
          } else {
            wx.redirectTo({
              url: `pages/spellGroup/mySpellGroup/index?couponId=${that.data.paramData.couponId}&couponChannelId=${that.data.paramData.couponChannelId}`
            })
          }
        }
        if (that.path != 'index'){
          wx.redirectTo({
            url: "/pages/game/index?url=" + that.data.paramData.url + "&id=" + that.data.paramData.id + "&gameId=" + that.data.paramData.gameId,
          })
        }else{
          /**
           * cuserId若存在来自卡转赠
           */
          if (this.data.paramData.cuserId){
            wx.reLaunch({
              url: "/pages/coupon/detail/index?cuserId=" + that.data.paramData.cuserId + '&couponChannelId=' + that.data.paramData.couponChannelId + '&couponId=' + that.data.paramData.couponId + '&coverImg=' + that.data.paramData.coverImg + '&avatarUrl=' + that.data.paramData.avatarUrl + '&userName=' + that.data.paramData.userName + '&couponOrderId=' + that.data.paramData.couponOrderId + "&updateDate=" + that.data.paramData.updateDate
            })
          }else{
            wx.redirectTo({
              url: '/pages/coupon/detail/index?couponChannelId=' + that.data.couponChannelId + '&couponId=' + that.data.couponId + "&flag=pay",
            })
          }
        }
      })
      .catch(err => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  //计时器
  timerInfo() {
    var timer = setInterval(() => {
      this.data.time--
        this.setData({
          time: this.data.time
        })
      if (this.data.time == 0) {
        clearInterval(timer)
        this.setData({
          flag: true,
          retry: false,
          time: 60
        })
      }
    }, 1000)
  },
  send() {
    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    if (phoneReg.test(this.data.phone)) {
      this.setData({
        retry: true
      })
      Http.get({
          url: config.api.sendValidationCode,
          data: {
            phone: this.data.phone,
            code: this.data.yzm,
            type: 1,
            appid: app.globalData.appId
          }
        })
        .then(res => {
          console.log(res)
          that.timerInfo()
          if (res.code == 200) {
          } else {
            wx.showToast({
              title: res.message
            })
          }
        })
        .catch(err => {
          wx.showToast({
            title: err.errMsg,
            icon: 'none',
            duration: 2000,
            mask: false
          });
        })
    }
  }
})