import {
  imgServerUrl
} from '../../config/config.js'
import $wuxCountDown from '../../utils/coutdown.js'
import {
  sendPhoneCode
} from '../../services/index.js'
import {
  saveMinaPhone
} from '../../services/wx.js'
import {
  showToast
} from '../../utils/tips.js'
import { testPhone } from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServerUrl: imgServerUrl,
    iphone: '',
    yzm: "",
    disabled: false,
    title: '绑定手机号码'
  },

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

  },
  privacyCancel(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 清除输入
  clear() {
    this.setData({
      iphone: ''
    })
  },
  // 手机号输入
  bindPhoneInput(e) {
    this.setData({
      iphone: e.detail.value,
      disabled: testPhone(e.detail.value)
    })
  },
  // 导航至验证码页面
  toSend(e) {
    // wx.showModal({
    //   title: '提示',
    //   content: '是否立即获取短信验证码？',
    //   success: res => {
    //     if (res.confirm) {
    //       wx.navigateTo({
    //         url: `/pages/bind-phone/bind?phone=${e.detail.phone}`,
    //       })
    //     }
    //   }
    // })
    wx.navigateTo({
      url: `/pages/bind-phone/bind?phone=${e.detail.phone}`,
    })
  },
  // 验证码号输入
  bindYzmInput(e) {
    this.setData({
      yzm: e.detail.value
    })
  },

  // 验证码发送
  sendCode(e) {
    if (this.data.iphone.length != 11) {
      showToast('请输入11位手机号码！')
      return
    }
    if (this.data.djs && this.data.djs != '获取验证码') {
      return false;
    }
    sendPhoneCode({
      phone: this.data.iphone
    }).then(data => {

    })
    this.vcode()
  },
  //倒计时
  vcode() {
    if (this.djs && this.djs.interval) return !1
    this.djs = new $wuxCountDown({
      date: +(new Date) + 60000,
      onEnd() {
        this.setData({
          djs: '获取验证码',
        })
      },
      render(date) {
        const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
        date.sec !== 0 && this.setData({
          djs: '剩余' + sec + '',
        })
      },
    })
  },
  //绑定手机号
  phone_login() {
    var self = this;
    if (self.data.iphone.length != 11) {
      return false;
    }
    if (self.data.yzm.length != 6) {
      return false;
    }
    if (!self.data.disabled) {
      self.setData({
        disabled: true
      })
      var openId = wx.getStorageSync('openId');
      var sessionKey = wx.getStorageSync('sessionKey');
      var unionId = wx.getStorageSync('unionId');
      let paramsObj = {
        phone: self.data.iphone,
        code: self.data.yzm,
        openId: openId,
        unionId: unionId,
        sessionKey: sessionKey
      }
      Object.assign(paramsObj);
      saveMinaPhone(paramsObj).then(data => {
        wx.setStorageSync("hpuser", data.data.hpUser);
        wx.setStorageSync("hpMember", data.data.hpMember);
        wx.setStorageSync("user_id", data.data.hpUser.id);
        wx.setStorageSync("member_id", data.data.hpMember.id);
        wx.setStorageSync("user_token", data.data.hpUser.userToken);
        wx.setStorageSync("phone", data.data.hpUser.phone);
        showToast("登录成功");
        setTimeout(function () {
          wx.navigateBack();
        }, 500)
      })
    }
    setTimeout(function () {
      self.setData({
        disabled: false
      })
    }, 1000)
  },

})