var app = getApp();
const util = require('../../utils/utils.js');
let timer1 = null;
Page({
  data: {
    smsVal: '', //输入的短信验证码
    phoneVal: '', //输入的手机号码
    code: '获取验证码', //获取短信验证码
    currentTime: 60, //倒计时秒数
    codeDisabled: false, //是否禁用按钮
    timer: null, //定时器
    phone_detail:[],
    title:app.globalData.title
  },
  onLoad: function (options) {
    var that = this;
    var arr = [];
    wx.getSystemInfo({
      success(res) {
        arr = [{
            label: '品牌',
            type: 'brand',
            value: res.brand
          },
          {
            label: '型号',
            type: 'model',
            value: res.model
          },
          {
            label: '系统平台',
            type: 'platform',
            value: res.platform
          },
          {
            label: '系统版本',
            type: 'system',
            value: res.system
          },
          {
            label: '微信版本',
            type: 'version',
            value: res.version
          },
          {
            label: '小程序版本',
            type: 'SDKVersion',
            value: res.SDKVersion
          },
          {
            label: '定位状态',
            type: 'locationEnabled',
            value: res.locationEnabled ? '开' : '关'
          },
          {
            label: '定位授权',
            type: 'locationAuthorized',
            value: res.locationAuthorized ? '已授权' : '未授权'
          }
        ]
        that.setData({phone_detail:arr})
      }
    })
  }, 
  agreement(){
    wx.navigateTo({
      url: '/pages/personal/privacy/privacy',
    })
  },
  //验证手机号
  ckPhone(e) {
    this.setData({
      phoneVal: e.detail.value
    })
  },
  //验证短信验证码
  ckSms(e) {
    this.setData({
      smsVal: e.detail.value
    })
  },
  getCode: util.throttle(function () {
    let self = this
    let currentTime = self.data.currentTime
    let timer = self.data.timer
    if (self.data.phoneVal.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      // 调用短信验证码接口
      let data = {
        mobile: this.data.phoneVal,
        type:1
      }
      app.request('/verification-code', data, 'POST').then(res => {
        if (res.statusCode == 200) {
          if (!timer) {
            timer = setInterval(() => {
              if (currentTime > 0 && currentTime <= 60) {
                currentTime--;
              }
              if (currentTime !== 0) {
                self.setData({
                  code: "重新发送" + "(" + currentTime + ")",
                  codeDisabled: true,
                })
              } else {
                clearInterval(timer);
                self.setData({
                  code: '获取验证码',
                  codeDisabled: false,
                  currentTime: 60,
                  timer: null,
                })
              }
            }, 1000)
          }
        }
      })
      //60秒倒计时
    }
  }, 1000),
  verify: util.throttle(function () {
    let reg = 11 && /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    let reg1 = /^\d{4}$/
    if (!reg1.test(this.data.smsVal)) {
      wx.showToast({
        title: '请输入短信验证码',
        icon: 'none',
        duration: 2000
      })
    }
    if (!reg.test(this.data.phoneVal)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!this.data.smsVal) {
      wx.showToast({
        title: '短信验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else {
      if (app.globalData.req) {
        let data = {
          mobile: this.data.phoneVal,
          code: this.data.smsVal,
          phone_detail: JSON.stringify(this.data.phone_detail),
          type:2
        }
        app.request('/auth/mobileLogin', data, 'POST', app.globalData.req).then(res => {
          if (res.statusCode == 200) {
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('token_time', res.data.exp)
            wx.setStorageSync('userInfo', res.data.user)
            if(app.globalData.compatible){
              wx.reLaunch({
                url: '/pages/compatible/index/index',
              })
            }else{
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '您的操作过于频繁，请稍后再试~',
          icon: 'none'
        })
      }
    }
  }, 1000),
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})