var a = getApp(),
  o = a.globalData,
  r = a.globalData.setting,
  t = a.request,
  s = require("../../../../utils/common.js"),
  util = require("../../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iurl: r.imghost,
    inputxphone: 1,
    mobile: null, //旧手机号码
    currentTime: "", //验证码发送倒计时
    getvcode: "获取验证码", //显示文字
    time_len: 0, //重新获取时长
    wxopenid: null,
    isver: 0, //是否有发送验证码
    code: "", //验证码
    change_phone: "", //新手机号码
    send: 1, //发送类型

    changing:0

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var th = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      is_zy: 1
    })
    getApp().getConfig2(function(ee) {
      var sms_conf = ee.sms_send_type;
      sms_conf = JSON.parse(sms_conf);
      th.setData({
        mobile: getApp().globalData.userInfo.mobile,
        wxopenid: getApp().globalData.userInfo.weapp_openid,
        time_len: sms_conf?sms_conf.time_out:60
      })
    })

  },
  //获取手机验证码
  Ver_phone: function() {
    var th = this;
    var mobile = th.data.mobile;
    var wxopenid = th.data.wxopenid;
    var change_phone = th.data.change_phone;
    var isver = th.data.isver;
    var send = th.data.send;
    var myreg = /^(((11[0-9]{1})|(12[0-9]{1})|(13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(change_phone){
      if (change_phone.length < 11) {
        getApp().my_warnning("请输入11位的手机号码", 0, th);
      } else if (!myreg.test(change_phone)) {
        getApp().my_warnning("手机号有误", 0, th);
        return false;
      }
    }else{
      getApp().my_warnning("请输入11位的手机号码", 0, th);
      return false;
      if (mobile.length < 11) {
        getApp().my_warnning("请输入11位的手机号码", 0, th);
      } else if (!myreg.test(mobile)) {
        getApp().my_warnning("手机号有误", 0, th);
        return false;
      }
    }

    if (change_phone == mobile) {
      getApp().my_warnning("新手机号码和旧手机号码不能一样", 0, th);
      return false;
    }
    if (change_phone != "") {
      mobile = change_phone;
    }
    if (isver == 0) {
      th.setData({
        isver: 1
      })
    } else {
      return false
    }

    wx.showLoading({
       mask:true
    });

    getApp().request.post("/api/weshop/smslog/sendsms", {
      data: {
        store_id: r.stoid,
        scene: send, //发送类型(1短信 2语音)
        mobile: mobile,
        wxopenid: wxopenid
      },
      success: function(res) {
        wx.hideLoading();

        if (res.data.code == 0) {
          th.setData({
            isver: 0,
            getvcode: th.data.time_len,
            currentTime: setInterval(th.gettime, 1000),
            send: 2
          })
        } else {
          getApp().my_warnning(res.data.msg, 0, th);
          th.setData({
            isver: 0,
            getvcode: "重新获取",
          })
          return false;
        }
      }
    })

  },
  gettime: function() {
    var that = this;
    if (that.data.getvcode == 1) {
      //清除js定时器  
      clearInterval(that.data.currentTime);
      that.setData({
        getvcode: "重新获取",
      });
    } else {
      that.setData({
        getvcode: --that.data.getvcode,
      });
    }
  },
  change_phone: function() {

    if(this.data.changing) return false;
    this.data.changing=1;


    var th = this;
    var code = th.data.code;
    var mobile = th.data.mobile;
    var change_phone = th.data.change_phone;
    if (change_phone.length > 1) {
      mobile = change_phone
    }
    
    if(!change_phone){
      this.data.changing=0;
      return  getApp().my_warnning("请输入手机号", 0, th);
    }

    var ob=util.check_mobile(change_phone);
    if(ob.code==-1){
      this.data.changing=0;
      return  getApp().my_warnning(ob.title, 0, th);
    }

    if(!code){
      this.data.changing=0;
      return  getApp().my_warnning("请输入验证码", 0, th);
    }

    wx.showLoading();
    getApp().request.put("/api/weshop/users/updateMobile/" + r.stoid + '/' + o.user_id + '/' + code + '/' + mobile, {
      success: function(res) {
        th.data.changing=0;
        wx.hideLoading();

        if (res.data.code == 0) {
          var url = "/packageG/pages/user/userinfo/userinfo?change_phone" + change_phone
          getApp().globalData.userInfo.mobile = mobile;
          getApp().my_warnning("更换成功", 0, th);
          getApp().goto(url);

        } else {
          getApp().my_warnning(res.data.msg, 0, th);
          return false;
        }

      }
    })

  },
  //判断验证码是否正确
  ver_code: function(e) {
    var th = this;
    var code = th.data.code,
      mobile = th.data.mobile,
      wxopenid = th.data.wxopenid,
      getvcode = th.data.getvcode,
      chang_phone = th.data.change_phone;
    if (code.length < 3 && code == "") {
      getApp().my_warnning("请输入正确的验证码", 0, th);
      return false;
    } else if (getvcode.length < 3) {
      getApp().my_warnning("验证超时", 0, th);
      return false;
    } else {
      getApp().request.put("/api/weshop/users/updateMobileFirst/" + r.stoid + '/' + o.user_id + '/' + code, {
        success: function(res) {
          if (res.data.code == 0) {
            th.setData({
              inputxphone: 1,
              getvcode: "获取验证码",
              code: ""
            })
            clearInterval(th.data.currentTime);
          } else {
            getApp().my_warnning(res.data.msg, 0, th);
            return false;
          }
        }
      })
    }
  },
  getCodeValue: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  getphoneValue: function(e) {
    var th = this;
    th.setData({
      change_phone: e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
      getApp().check_can_share();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
      getApp().globalData.no_clear=1
  }
})
