var timer; // 计时器
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var code
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clickCode: false,
    phoneNumber: '',
    countdownTime: 59,
    vCode: ''
  },

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

  bindNow: function (event) {
    var that = this
    console.log(event.detail.value)
    let phone = event.detail.value.phoneNumber
    let vcode = event.detail.value.vCode

    if (!phone) {
      that.lackToast('未填手机号')
      return
    }
    if (!vcode) {
      that.lackToast('未填验证码')
      return
    }

    if(vcode != code){
      that.lackToast('验证码不正确，请重新输入')
      return
    }
    var data = { 
      openid: wx.getStorageSync('openid'),
      empPhone:phone
    }
    var url = baseUrl + BASEURL.getUrl.Binding 
    netUtil.requestLoading(url, data, '正在加载数据...', 'POST', function (res) {
      if (res.data.success) {
        wx.showToast({
          title: res.data.data,
          icon: 'none',
          duration: 2000
        })
        // wx.setStorageSync('accountphone', phone)
        wx.setStorageSync('isregistered', true)
        // var pages = getCurrentPages()
        // var prevPage = pages[pages.length - 1]  //当前界面
        // var prevPage = pages[pages.length - 2]  //上一个页面
        // prevPage.setData({

        // })
        setTimeout(function () {
          wx.navigateBack({
          })
        }, 2000)
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })


    return
    wx.login({
      success: res => {
        wx.request({
          url: baseUrl + BASEURL.getUrl.getOpenid,  //获取openid
          method: 'GET',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            code: res.code
          },
          success: function (res) {
            console.log('res2=', res)
            if (res.data) {
              var openId = res.data.data.openid
              if (!openId) return
              //进行绑定
              wx.request({
                url: baseUrl + BASEURL.getUrl.Binding + '?empPhone=' + phone +  '&openid=' + openId,
                method: 'POST',
                data: {
                },
                header: {
                  'content-type': 'application/json;charset=UTF-8',
                },
                success: function (res) {
                  console.log('bindable=', res)
                  if (res.data.success) {
                    wx.showToast({
                      title: res.data.data,
                      icon: 'none',
                      duration: 2000
                    })
                    wx.setStorageSync('accountphone', phone)
                    // var pages = getCurrentPages()
                    // var prevPage = pages[pages.length - 1]  //当前界面
                    // var prevPage = pages[pages.length - 2]  //上一个页面
                    // prevPage.setData({

                    // })
                    setTimeout(function () {
                      wx.navigateBack({
                      })
                    }, 3000)
                  } else {
                    wx.showToast({
                      title: res.data.bdfall2,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  /**
  * 监听新手机号输入值
  */
  getphoneNumber: function (e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  getvCode: function () {
    var that = this
    if (!that.data.phoneNumber) {
      that.lackToast('未填手机号')
      return
    }
    console.log('phoneNumber', that.data.phoneNumber)

    var data = { empPhone: that.data.phoneNumber }
    var url = baseUrl + BASEURL.getUrl.sendCode
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data && res.data.message == 'OK'){
        wx.showToast({
          title: '短信已发送',
          duration: 2000
        })
        code = res.data.code
        that.setData({
          clickCode: true,
          // vCode:code
        })
        that.TimerHandle()
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none',
          duration: 2000
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })

  },

  TimerHandle: function () {
    var that = this
    timer = setTimeout(function () {
      if (parseInt(that.data.countdownTime) == 0) {
        that.setData({
          countdownTime: 59,
          clickCode: false
        })
        clearTimeout(timer);
      } else {
        let time = that.numberAdd2(parseInt(that.data.countdownTime) - 1)
        that.setData({
          countdownTime: time
        })
        that.TimerHandle()
        // console.log('time=' + time)
      }
    }, 1000)
  },
  numberAdd2: function (num) {
    let result
    if (num < 10) {
      result = '0' + num
    } else {
      result = num
    }
    return result
  },

  lackToast: function (title) {
    if (title == '') {
      title = '缺填必选项'
    }
    wx.showToast({
      title: title,
      icon:'none',
      duration: 2000
    });
  },
 
})