// pages/register/register.js

const app = getApp()
let time;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yesSend:false,//是否发送验证码
    minter: 60,//发送验证码60秒后才可再次发送
    num:60,
    phone: "",//手机号
    phoneCode: "",//验证码
    yesRegister:false,//防止重复点击注册
    userInfo:[],//用户信息
  },

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

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.canIUse('hideHomeButton')) {
      wx.hideHomeButton()
    }
  },

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

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "老门框爆肚会员",
      success: res => {
        console.log(res, "转发成功")
      },
    }
  },

  yesCodeFn:function(){//验证码接口
    app.wxRequest(app.globalData.httpUrl + 'smscode', { user_phone: this.data.phone }, e => {
      console.log(e)
      if (e.code == 200) {
        wx.showToast({
          title: e.message,
          icon: 'success',
          duration: 500
        })
      }else{
        wx.showToast({
          title: e.message,
          icon: 'none', 
          duration: 500
        })
      }
    }, this)
  },
  sendTouch:function(){//发送验证码
    if (this.data.phone){
      if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
        wx.showToast({
          title: '手机号错误',
          icon: 'none',
          duration: 500
        })
        return;
      } 
    }else{
      wx.showToast({
        title: '请先输入手机号',
        icon: 'none',
        duration: 500
      })
      return;
    }
    
    this.setData({
      yesSend: true,
    })
    if (this.data.yesSend) {
      time = setInterval(this.setIntervalFn,1000)
    }

    if (app.globalData.openId) {
      this.yesCodeFn();
    } else {
      app.globalData.openidSuccessFuc = this.yesCodeFn;
    }
    
  },
  setIntervalFn:function(){//定时器
    if (this.data.num > 0) {
      this.data.num--;
      if (this.data.num == 0) {
        this.setData({
          yesSend: false,
        })
        if (!this.data.yesSend){
          this.setData({
            num: 60,
            minter: 60,
          })
        }
        clearInterval(time);
      }
    }
    this.setData({
      minter: this.data.num
    })
    console.log(this.data.minter)
  },
  submitUser: function () {//提交用户信息
    if(this.data.userInfo){
      var params = {
        "province": this.data.userInfo.province,
        "language": this.data.userInfo.language,
        "city": this.data.userInfo.city,
        "gender": this.data.userInfo.gender,
        "nickname": this.data.userInfo.nickName,
        "headimgurl": this.data.userInfo.avatarUrl,
      }
      app.wxRequest(app.globalData.httpUrl + 'submit', params, e => {
        console.log(e)
        if (e.code == 200) {

        }
      }, this, "POST")
    }
   
  },
  getUserFn:function(e){//点击按钮注册
    console.log(e)
    if (e.detail.userInfo) {
      this.data.userInfo = e.detail.userInfo;
      wx.setStorageSync('noUser', 1);
      app.globalData.userInfo  = e.detail.userInfo
    } else {
      wx.setStorageSync('noUser', 0);
    }
    this.registerFn();
    // if (app.globalData.openId) {
      
    // } else {
    //   app.globalData.openidSuccessFuc = this.registerFn;
    // }
  },
  registerFn:function(){//注册接口
    
    if(this.data.phone){
      if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
        wx.showToast({
          title: '手机号错误',
          icon: 'none',
          duration: 500
        })
        return;
      } 
    }else{
      wx.showToast({
        title: '请先输入手机号',
        icon: 'none',
        duration: 500
      })
      return;
    }
    if (!this.data.phoneCode){
      wx.showToast({
        title: '请先输入验证码',
        icon: 'none',
        duration: 500
      })
      return;
    }
    if (this.data.yesRegister) {
      return;
    }
    this.data.yesRegister = true;
    app.wxRequest(app.globalData.httpUrl + 'register', { user_phone: this.data.phone, code_num: this.data.phoneCode }, e => {
      console.log(e)
      if (e.code == 200) {
        clearInterval(time);
        wx.setStorageSync('user_phone', e.data.user_phone);
        wx.setStorageSync("userInfo", this.data.userInfo)
        wx.setStorageSync('state', 1);
        // app.globalData.userInfo = e.data;
        this.submitUser();
        this.setData({
          minter: 60,
          num: 60
        })
        if (app.globalData.present_id){
          wx.reLaunch({
            url: '/pages/receiveTicket/receiveTicket?shareId=' + app.globalData.present_id + "&number=" + app.globalData.number
          })
        } else {
          console.log(5555)
          wx.navigateBack();
        }
      } else {
        this.data.yesRegister = false;
        wx.showToast({
          title: e.message,
          icon:"none",
          duration:500
        })
      }
    }, this,"POST")
  },
  getPhone: function (e) {//获取手机号
    console.log(e.detail.value);
    this.setData({
      phone: e.detail.value
    })
  },
  getCode: function (e) {//获取验证码
    console.log(e.detail.value);
    this.setData({
      phoneCode: e.detail.value
    })
  },
})