var userAgent = window.localStorage.getItem('wlyyAgentForDoc')
var $eyeIcon = $('.psw-eye'),
	$mobile1 = $('#mobile1'),
	$password = $('#password'),
	$mobile2 = $('#mobile2'),
	$captcha = $('#captcha'),
	$mobile = $('input[mobile]'),
	$tabs = $('.nav-tabs'),
	$getCaptcha = $('#getCaptcha'),
	$submit = $('button[type="submit"]');
var timer = null;
$(function() {
		bindEvent()	
	function bindEvent(){
		//密码隐藏
		$eyeIcon.click(function() {
			$el = $(this);
			if($el.hasClass('fa-eye')) {
				$el.removeClass('fa-eye').addClass('fa-eye-slash');
				$password.attr('type','password')
			} else {
				$el.removeClass('fa-eye-slash').addClass('fa-eye');
				$password.attr('type','text')
			}
		})
		//验证码定时
		function setTimer() {
			$getCaptcha.addClass('disabled');
			var seconds = 59;
			timer = setInterval(function() {
				if(seconds == 0) {
					clearInterval(timer);
					timer = null;
					seconds = 59;
					$getCaptcha.text("获取验证码");
					$getCaptcha.removeClass('disabled');
					return ;
				}
				$getCaptcha.text((seconds--)+"s后重新获取");
			}, 1000)
		}
		//表单验证
		var validator1 = $("#form1").validate({
			onsubmit: false,
			onfocusout: function(ele) {
				validator1.element(ele)
			},
		    rules: {
		        mobile1: {
					required: true,
					mobile: true,
				},
			　　　password: "required",
			}
		});			
		var validator2 = $("#form2").validate({
			onsubmit: false,
			onfocusout: function(ele) {
				validator2.element(ele)
			},
		    rules: {
		        mobile2: {
					required: true,
					mobile: true,
				},
			　　　captcha: "required",
			}
		});
		jQuery.validator.addMethod("mobile", function(value, element, param) {
			var reg=/^[1][3,4,5,7,8][0-9]{9}$/;  
		  return reg.test(value);  
		}, "手机号码有误");
		//切换tab
		$tabs.on('click','li',function() {
			var idx = $(this).index(),
				mob = '';
				
			if(idx == 1) { // 点击验证码登录选项卡
				mob = $mobile1.val();
				if(mob) {
					$mobile.val(mob);
					validator2.element($mobile2)
				}
			} else { // 点击手机登陆选项卡
				mob = $mobile2.val();
				if(mob) {
					$mobile.val(mob);
					validator1.element($mobile1)
				}
			}
		})
		//发送短信
		$getCaptcha.click(function() {
			if($getCaptcha.hasClass('disabled')) {
				return ;
			}
			if(validator2.element($mobile2)) {
				setTimer();
				sendPost('common/captcha',{mobile: $mobile2.val(),type: 5,captchaToken: 5},function(){
					ask('请求失败')
				},function(res){
					if(res.status == 200){
						tip('发送成功','add')
					}else{
						tip('获取验证码失败')
					}
				})
			}
		})
		//提交
		$submit.click(function() {
			var index = $('.nav-tabs>li.active').index();
			var mobile = '',
				password = '',
				captcha = '';
			if(index == 0) { // 点击手机登陆选项卡
				if($("#form1").valid()){
					$(this).attr('disabled','disabled')
					mobile = $mobile1.val();
					password = $password.val();
					sendPost('login/public_key',{},function(){
						tip('请求失败')
						$submit.removeAttr('disabled')
					},function(res){
						if (res.status == 200) {
							var mod = res.data.modulus;
							var exp = res.data.exponent;
							key = RSAUtils.getKeyPair(exp, "", mod);
							if (key) {
								encryedPwd = RSAUtils.encryStr(key, password);
								login({
									mobile: mobile,
									password: encryedPwd,
									platform: 4
								})
							}else {
							   tip("获取数据失败")
							   $submit.removeAttr('disabled')
							}
						}
					})
				}
			}else{ // 点击验证码登录选项卡
				if($("#form2").valid()) {
					$(this).attr('disabled','disabled')
					mobile = $mobile2.val();
					captcha = $captcha.val();
					login({
						mobile: mobile,
						captcha: captcha,
						platform: 4
					})
				}
			}
		})
		
	}
})

//登录
function login(data) {
	sendPost('login/doctor',data,function(){
		$submit.removeAttr('disabled')
		tip('请求失败')
	},function(res){
		$submit.removeAttr('disabled')
		if(res.status == 200) {
			var data = res.data;
			//添加医生到userRole
			localStorage.setItem('wlyyAgentForDoc',JSON.stringify(data))
		   	alert("登录成功")
		   	location.href = "app/record/html/record.html"
		}else{
			tip(res.msg)			
		}
	})
}