$(function () {
  var ERROR_OK = 200;
  var contextPath = $("#contextPath").val();
  var openId = $("#openId").val();
  var developId = $("#developId").val();
  var oldUrl = $("#oldUrl").val();
  var virId = "";
  var orgId = "";
  if (oldUrl == "/wechat/neigou/meetingDo") {
    oldUrl = contextPath + "/wechat/neigou/meetingDo"
  }
  
  if (oldUrl == "/wechat/neigou/wjdcDo") {
	    oldUrl = contextPath + "/wechat/neigou/wjdcDo"
  }

  if (oldUrl == "/wechat/neigou/tsbyDo") {
	    oldUrl = contextPath + "/wechat/neigou/tsbyDo"
  }

  var timer; //timer变量，控制时间
  var count = 60; //间隔函数，1秒执行
  var curCount = 60; //当前剩余秒数
  var code = ""; //验证码
  var canClick = true;

  function sendMessage() { //设置button效果，开始计时
    curCount = count;
    $("#getCode").text(curCount + "s 重新获取");
    timer = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
  };
  /**
   * timer处理函数
   * 
   */
  function SetRemainTime() {
    if (curCount == 0) {
      window.clearInterval(timer); //停止计时器
      // $("#getCode").removeAttr("disabled"); //启用按钮
      canClick = true;
      $("#getCode").text("获取验证码");
      code = ""; //清除验证码。如果不清除，过时间后，输入收到的验证码依然有效   
      $("#code").val(code);
    } else {
      curCount--;
      $("#getCode").text(curCount + "s 重新获取");
    }
  };
  /**
   * 验证手机号
   * @param phone 参数名
   */
  function isPhoneNo(phone) {
    var pattern = /^1[3456789]\d{9}$/;
    return pattern.test(phone);
  };
  /**
   * 获取url的参数
   * @param name 参数名
   */
  function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  };
  /**
   * 绑定提交
   * @param mobile, mobileCode 参数名
   */
  function ajaxBind(mobile, mobileCode) {
    $.ajax({
      url: contextPath + "/wechat/index/neigouLogin",
      type: "POST",
      dataType: "json",
      data: {
        openId: openId,
        mobile: mobile,
        mobileCode: mobileCode,
        developId: developId,
        oldUrl: oldUrl
      },
      success: function (data) {
        if (data.code == ERROR_OK) {
          console.log(data);
          window.location.href = contextPath + data.data.oldUrl + "?openId=" + openId + "&developId=" + developId + "&virId=" +virId+ "&orgId=" + orgId;
        } else {
          layer.open({
            content: data.msg,
            skin: 'msg',
            time: 2
          });
        }
      },
      error: function (data) {
        alert("系统繁忙..");
      }
    });
  };
  /**
   * 获取验证码
   * @param mobile 参数名
   */
  function ajaxCode(mobile) {
    $.ajax({
      url: contextPath + "/wechat/index/getNeiGouSmsCode",
      type: "POST",
      dataType: "json",
      data: {
        openId: openId,
        mobile: mobile,
        developId: developId,
        oldUrl: oldUrl
      },
      success: function (data) {
        if (data.code == ERROR_OK) {
          sendMessage();
        } else {
          canClick = true;
          layer.open({
            content: data.msg,
            skin: 'msg',
            time: 2 //2秒后自动关闭
          });
        }
      },
      error: function (data) {
        canClick = true;
        alert("系统繁忙..");
      }
    });
  }
  $("#phone").bind("input propertychange", function () {
    // console.log($("#phone").val().length);
    if ($("#code").val() && $("#phone").val().length == 11) {
      $(".bind-btn").addClass("active")
    } else {
      $(".bind-btn").removeClass("active")
    }
  });
  $("#code").bind("input propertychange", function () {
    // console.log($("#code").val().length);
    if ($("#phone").val() && $("#code").val().length == 4) {
      $(".bind-btn").addClass("active")
    } else {
      $(".bind-btn").removeClass("active")
    }
  });
  // 点击获取验证码
  document.getElementById("getCode").addEventListener("click", getCode);

  function getCode() {
    if (!canClick) {
      return false
    }
    var mobile = $("#phone").val();
    if (!mobile) {
      layer.open({
        content: '请输入手机号码',
        skin: 'msg',
        time: 2 //2秒后自动关闭
      });
      return false
    }
    if (!isPhoneNo(mobile)) {
      layer.open({
        content: '请输入正确的手机号码',
        skin: 'msg',
        time: 2
      });
      return false
    }
    canClick = false;
    ajaxCode(mobile);
  };
  $(".bind-btn").click(function () {
    var mobile = $("#phone").val();
    var codeNumber = $("#code").val();
    if (!mobile) {
      layer.open({
        content: '请输入手机号码',
        skin: 'msg',
        time: 2
      });
      return false
    }
    if (!isPhoneNo(mobile)) {
      layer.open({
        content: '请输入正确的手机号码',
        skin: 'msg',
        time: 2
      });
      return false
    }
    if (!codeNumber) {
      layer.open({
        content: '请输入有效的验证码',
        skin: 'msg',
        time: 2
      });
      return false
    }
    ajaxBind(mobile, codeNumber);
  })
})