const config = require('../../config/config.js')
const Common = require('../../common/common.js')
const app = getApp()
var interval = null //倒计时函数
Page({
  data: {
    phone: '',
    yzm: '',
    flag: true,
    retry: false,
    time: 60,
    phones: "",
    date: '请选择日期',
    fun_id: 2,
    time: '获取验证码', //倒计时 
    currentTime: 61,
    disabled:false,
    setInter:''
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    that.data.setInter = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(that.data.setInter)
        that.setData({
          time: '获取验证码',
          currentTime: 61,
          disabled: false
        })
      }
      console.log(that.data.time)
    }, 1000)
  },
  onHide:function(){
    let that = this;
    clearInterval(that.data.setInter);
    that.setData({
      time: '获取验证码',
      currentTime: 61,
      disabled: false
    })
  },
  onUnload:function(){
    let that = this;
    clearInterval(that.data.setInter)
    that.setData({
      time: '获取验证码',
      currentTime: 61,
      disabled: false
    })
  },
  onLoad:function(options){
    console.log(options);
    if(tt.getStorageSync("phone")){
      this.setData({
        phone: tt.getStorageSync("phone"),
      })
    }
    if(options.flags=='reset'){
      tt.setNavigationBarTitle({
        title: '修改密码'
      })
      this.setData({
        flags: options.flags
      })
    }else{
      tt.setNavigationBarTitle({
        title: '忘记密码'
      })
    }
   
  },
  onShow: function () {
    let that = this;
    that.setData({
      phones: tt.getStorageSync("phone"),
      disabled:false
    });
    console.log(that.data.phones)
  },
  getYZM(e) {
    console.log(e);
    if (e.detail.value.phone) {
      this.setData({
        phone: e.detail.value.phone,
        yzm: e.detail.value.yzm
      })
    }else{
      this.setData({
        phone: tt.getStorageSync("phone"),
        yzm: e.detail.value.yzm
      })
    }
  },
  next() {
    setTimeout(() => {
      console.log(this.data)
      Common.hasValidationCode(this.data.phone, this.data.yzm)
        .then(res => {
          console.log(res)
          setTimeout(() => {
            console.log(res)
            if (res.code == 200) {
              tt.navigateTo({
                url: `/pages/setpwd/setpwd?phone=${this.data.phone}&code=${this.data.yzm}`
              })
            } else {
              tt.showToast({
                title: "验证码错误",
                icon: "none",
                duration: 2000
              })
            }
          }, 500)
        })
    }, 1000)
  },
  send(e) {
    console.log(this.data.phone);
    var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
    let that = this;
    // setTimeout(() => {
      let tel = that.data.phone;
      console.log(tel)
      if (phoneReg.test(tel)) {
        Common.sendValidationCode(tel)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              that.getCode();
              that.setData({
                disabled: true
              })
              tt.showToast({
                title: res.message,
                image: '../../static/images/success.png'
              })
            } else if (res.code == 12061){
              tt.showToast({
                title: "短息已发送,稍后重试",
                icon: "none",
              })
            } else {
              tt.showToast({
                title: res.message,
                icon: "none",
              })
            }
          })
      } else {
        tt.showToast({
          title: '手机号有误',
          icon: "none",
        })
      }
    // }, 500)
  }
})