var app = getApp();
Page({
  data: {
    text: '获取验证码',
    disabled: false,
    phone: '',
    code: '',
    flag: true,// 弹窗是否显示
  },
  onLoad: function () {
    // console.log(app.globalData.apiUrl)
  },
  phoneChange: function (e){
    this.setData({
      phone: e.detail.value
    })
  },
  getCode: function (e){
    let me = this,
        phone = me.data.phone,
        warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空
    me.setData({
      disabled: true
    })
    if(phone == ''){
      warn = '请输入手机号码！';
    } else if(phone.trim().length != 11 || ! /^1[0-9]{10}$/.test(phone)){
      warn = '手机号码格式不正确！';
    } else{
      //当手机号正确的时候提示用户短信验证码已经发送
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'users/send',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          phone: phone
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            wx.showToast({
              title: '短信验证码已发送',
              mask: true,
              // icon: 'none',
              duration: 2000
            });
            // 设置60s的倒计时
            let num = 60;
            let timer = setInterval(function () {
              num--;
              if (num <= 0) {
                clearInterval(timer);
                me.setData({
                  text: '重新发送',
                  disabled: false
                })
              } else {
                me.setData({
                  text: num+'秒',
                  disabled: true
                })
              }
            }, 1000)
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
            me.setData({
              disabled: false
            })
          }
        }
      })
    }

    //判断 当错误信息不为空时弹窗,并将按钮恢复可用
    if (warn != null) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
    
      me.setData({
        disabled: false
      })
      return;
    }
  },
  codeChange: function (e){
    this.setData({
      code: e.detail.value
    })
  },
  toLogin: function (e){
    // 授权
    if(e.detail.errMsg!='getUserInfo:ok'){
      //拒绝授权
      console.log(e.detail.errMsg)
      return
    }
    //授权成功后
    console.log(e.detail.userInfo)

    wx.request({
      url: app.globalData.apiUrl + '/users/addSmallProgramUser',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        avatarUrl: e.detail.userInfo.avatarUrl,
        city: e.detail.userInfo.city,
        country: e.detail.userInfo.country,
        gender: e.detail.userInfo.gender,
        language: e.detail.userInfo.language,
        nickName: e.detail.userInfo.nickName,
        province: e.detail.userInfo.province
      },
      success: function (res) {
        if(res.data.errno==200){
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
    let me = this,
        phone = me.data.phone,
        code = me.data.code,
        warn = null;// 手机号和验证码校验错误信息
    if(phone.trim().length != 11 || ! /^1[0-9]{10}$/.test(phone)){
      warn = '手机号码格式不正确！';
    } else if(code.length<4){
      warn = '短信验证码错误！'
    } else{
      // 手机号和验证码格式正确
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'users/login',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          phone: phone,
          verification: code
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            wx.showToast({
              title: '登录成功',
              mask: true,
              // icon: 'none',
              duration: 2000
            });
            // 关闭所有页面,打开首页
            wx.reLaunch({
              // url: '/pages/index/index'
              url: '/pages/getCodeIndex/getCodeIndex'
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
            me.setData({
              disabled: false
            })
          }
        }
      })
    }
    //判断 当错误信息不为空时弹窗
    if (warn != null) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
      return;
    }
  },
  // 显示验证码弹窗
  showCodePopup: function (e) {
    this.setData({
      flag: false
    })
  },
  // 验证码弹窗知道
  codePopupKnow: function (e) {
    this.setData({
      flag: true
    })
  }
})
