// pages/user/user.js
const app = getApp()
var host = app.globalData.host;
const $api = require('../../utils/api.js').API;
import md5 from '../../utils/md5.js';//支付页面引入md5.js
var logindata={}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    index:-1,
    type:'',
    phone:'',
    phcode:'',
    signup_name:'',
    text:'获取验证码',
    isLoading:0,
    userinfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    $api.getsignup().then(res=>{
      this.setData({
          info:res.data.data
      })
      if(options.type == 1){
        res.data.data.groups.forEach(item=>{
          if(!item.charge){
            this.setData({
              match_group:item.id
            })
          }
        })
        
      }
    })

    wx.getStorage({
      key: 'user',
      complete: res => {
        this.setData({
          userinfo: res.data,
        })
      }
    })

  },

  bindChange(e) {
    let match=this.data.info.groups;
    this.setData({
      index: e.detail.value,
      match_group: match[e.detail.value].id
    })
  },
  getcode(){
    if(this.data.phone.length>=11){
      this.setData({
        text:"59s重新发送"
      })
      this.countdown()
      $api.sendcode({phone:this.data.phone}).then(res=>{
        
      })
    }else{
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号',
      })
    }
    
  },
  countdown(){
    var count=60,_this=this;
    var timer=setInterval(c,1000)
    function c(){
      if(count>0){
        count--;
        _this.setData({
          text:count+"s重新发送"
        })
      }else{
        _this.setData({
          text:'获取验证码'
        })
        clearInterval(timer)
      }
    }
  },

  inputchange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputchange1(e) {
    this.setData({
      signup_name: e.detail.value
    })
  },
  codechange(e) {
    this.setData({
      phcode: e.detail.value
    })
  },
  post(){
      var data={
          signup_type:this.data.type,
          total_fee:this.data.info.viewer_price,
          phone:this.data.phone,
          phcode:this.data.phcode,
          match_group:this.data.match_group,
          match_id:this.data.info.id,
          match_name:this.data.info.name,
          signup_name:this.data.signup_name
      }
      if(this.data.type == 2){
          data.total_fee=this.data.info.player_price 
      }

      if (data.signup_name.length<=0){
        wx.showToast({
          icon: 'none',
          title: '请输入名字',
        })
        return
      }

      if (data.phone.length<11){
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号',
        })
        return
      }
      if (!data.phcode) {
        wx.showToast({
          icon: 'none',
          title: '请输入验证码',
        })
        return
      }
      if(this.data.isLoading == 1){
        return
      }

      this.setData({
        isLoading:1
      })
      wx.showLoading({
        title: '正在提交订单',
      })
      $api.wxpay(data).then(res=>{
        wx.hideLoading()
        this.setData({
          isLoading:0
        })
        if(res.data.data.free){
          app.globalData.follow = 1
          wx.showToast({
            title: '报名成功',
          })
          wx.switchTab({
            url: '../index/index'
          });
        }
        var timestamp = Math.round(new Date().getTime() / 1000).toString();
        var paySign = md5('appId=' + res.data.data.appid + '&nonceStr=' + res.data.data.nonce_str + '&package=prepay_id=' + res.data.data.prepay_id + '&signType=MD5&timeStamp=' + timestamp + '&key=' + res.data.data.key).toUpperCase();
        console.log(paySign)
          wx.requestPayment({
            timeStamp: timestamp,
            nonceStr: res.data.data.nonce_str,
            package: 'prepay_id=' + res.data.data.prepay_id,
            signType: 'MD5',
            paySign: paySign,
            success(res) { 
              app.globalData.follow = 1
              wx.showToast({
                title: '报名成功',
              })
              wx.switchTab({
                url: '../index/index'
              });
            },
            fail(res) { console.log(res)}
          })
      })
  },



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

  },

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


  },

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

  },

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

  },

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

  },

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

  },

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

  }


})