var language = 0;
var time = 60;
var time2 = 60;
var ifSend = true;
var ifSend2 = true;
var way = 0;
var mobile1 = "";
var mobile2 = "";
var pin1 = "";
var pin2 = "";
var loginTo = sessionStorage.getItem("miaomuLoginTo");
sessionStorage.removeItem("miaomuLoginTo");
$(function () {
	var session = sessionStorage.getItem("MiaoMuAction");
	if(session=="forget"){
		sessionStorage.removeItem("MiaoMuAction");
		setTimeout(function(){
			forget1();
		}, 100);
	}
	$(".Send a").click(function () {
		checkMobile1();
	});
	$("#change2").click(function () {
		if(way == 0){
			$(".Send").show();
			if(language==0){
				$(this).html("输入密码登录");
				$("#password").attr("placeholder", "请输入验证码");
			}else if(language==1){
				$(this).html("輸入密碼登入");
				$("#password").attr("placeholder", "請輸入驗證碼");
			}else {
				$(this).html("By password");
				$("#password").attr("placeholder", "Input PIN");
			}
			$(".login")[0].reset();
			$("#password").css("background-image","url(../images/验证.png)");
			$("#password").attr("maxlength","6");
			$("#password").attr("type","text");
			way = 1;
		}else{
			$(".Send").hide();
			if(language==0){
				$(this).html("手机短信登录");
				$("#password").attr("placeholder", "请输入密码");
			}else if(language==1){
				$(this).html("手機短信登入");
				$("#password").attr("placeholder", "請輸入密碼");
			}else {
				$(this).html("By SMS");
				$("#password").attr("placeholder", "Input password");
			}
			$("#password").val("");
			$("#password").css("background-image","url(../images/密码.jpg)");
			$("#password").attr("maxlength","16");
			$("#password").attr("type","password");
			way = 0;
		}
	});
	$("#change3").click(function () {
		if(language==0){
			forget1();
		}else if(language==1){
			forget2();
		}else {
			forget3();
		}
	});
	$(".other-img img").click(function () {
		if (language == 0) {
			layer.msg('抱歉!正在开发中,敬请期待!', { icon: 4 });
		} else if (language == 1) {
			layer.msg('抱歉!正在開發中,敬請期待!', { icon: 4 });
		} else {
			layer.msg('Sorry! Developing now, coming soon!', { icon: 4 });
		}
	});
	$("#changeA").click(function () {
		if(way==0){
			$("#password").attr("placeholder", "请输入密码");
			$("#change2").html("手机短信登录");
		}else{
			$("#password").attr("placeholder", "请输入验证码");
			$("#change2").html("输入密码登录");
		}
		$(".Send a").html("发送验证码");
		$("#change1").html("账号登录");
		$("#login").attr("value", "登    录");
		$("#change3").html("忘记密码");
		$("#change4").html("立即注册");
		$("#change5").html("其他方式登录");
		$("#change6").html("常见问题");
		$("#change7").html("隐私政策");
		$("#change6").css("width", "100px");
		$("#change7").css("width", "100px");
		$(".m3-2").css("width", "28%");
		language = 0;
	});
	$("#changeB").click(function () {
		if(way==0){
			$("#password").attr("placeholder", "請輸入密碼");
			$("#change2").html("手機短信登入");
		}else{
			$("#password").attr("placeholder", "請輸入驗證碼");
			$("#change2").html("輸入密碼登入");
		}
		$(".Send a").html("發送驗證碼");
		$("#change1").html("賬號登入");
		$("#login").attr("value", "ç™»    å…¥");
		$("#change3").html("忘記密碼");
		$("#change4").html("立即註冊");
		$("#change5").html("其他方式登入");
		$("#change6").html("常見問題");
		$("#change7").html("隱私政策");
		$("#change6").css("width", "100px");
		$("#change7").css("width", "100px");
		$(".m3-2").css("width", "28%");
		language = 1;
	});
	$("#changeC").click(function () {
		if(way==0){
			$("#password").attr("placeholder", "Input password");
			$("#change2").html("By SMS");
		}else{
			$("#password").attr("placeholder", "Input PIN");
			$("#change2").html("By password");
		}
		$(".Send a").html("Send PIN");
		$("#change1").html("Login");
		$("#login").attr("value", "Login");
		$("#change3").html("Forget PWD");
		$("#change4").html("Register");
		$("#change5").html("Other way");
		$("#change6").html("Common problem");
		$("#change7").html("Privacy policy");
		$("#change6").css("width", "9%");
		$("#change7").css("width", "8%");
		$(".m3-2").css("width", "34%");
		language = 2;
	});
	$("#login").click(function () {
		if(way==1){
			loginBySMS();
		}else{
			loginTest();
		}
	});
});
function loginTest() {
	var username = $("#username").val();
	var password = $("#password").val();
	if (username == null || username == "" || password == null || password == "") {
		if (language == 0) {
			layer.msg('手机号或密码不能为空!', { icon: 2 });
		} else if (language == 1) {
			layer.msg('手機號或密碼不能為空!', { icon: 2 });
		} else {
			layer.msg('Mobile or password cannot be empty', { icon: 2 });
		}
	} else {
		var data = { "phone": username, "passWord": password ,"platform":'Web',"equipment":getBrowser()};
		$.ajax({
			url:url+"/user/loginUser",
			data: data,
			type: "post",
			dateType: "json",
			success: function (response, status, xhr) {
				if (response.length == 1) {
					if (language == 0) {
						layer.msg('登录成功,即将跳转', { icon: 1 });
					} else if (language == 1) {
						layer.msg('登入成功,即將跳轉', { icon: 1 });
					} else {
						layer.msg('Login succeeded, about to jump', { icon: 1 });
					}
					localStorage.setItem("miaomu_pc_user_id", response[0].id);
					localStorage.setItem("miaomu_pc_login_time",new Date().getTime());
					/*var userInfo = loadUserInfo(response[0].id);*/
					getLast(response[0].id);
					setLast(response[0].id);
					setTimeout(function () {
						if(loginTo){
							window.location.href = loginTo;
						}else{
							window.location.href = "../manage/";
						}
					}, 2000);
				} else {
					if (language == 0) {
						layer.msg('手机号或密码错误!', { icon: 2 });
					} else if (language == 1) {
						layer.msg('手機號或密碼錯誤!', { icon: 2 });
					} else {
						layer.msg('Wrong mobile or password', { icon: 2 });
					}
				}
			},
			error: function () {
				if (language == 0) {
					layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
				} else if (language == 1) {
					layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
				} else {
					layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
				}
			}
		});
	}
}

function loginBySMS() {
	var yzm = $("#password").val();
	if(yzm==pin1){
		$.ajax({
			url:url+"/user/loginBySMS",
			data: {"phone":mobile1,"platform":'Web',"equipment":getBrowser()},
			type: "post",
			dateType: "json",
			success: function (response, status, xhr) {
				if (language == 0) {
					layer.msg('登录成功,即将自动跳转', { icon: 1 });
				} else if (language == 1) {
					layer.msg('登入成功,即將跳轉', { icon: 1 });
				} else {
					layer.msg('Login succeeded, about to jump', { icon: 1 });
				}
				//sessionStorage.setItem("MiaoMuUserID", response[0].id);
				localStorage.setItem("miaomu_pc_user_id", response[0].id);
				localStorage.setItem("miaomu_pc_login_time",new Date().getTime());
				/*var userInfo = loadUserInfo(response[0].id);
				sessionStorage.setItem("miaomuUserInfo", JSON.stringify(userInfo));*/
				getLast(response[0].id);
				setLast(response[0].id);
				setTimeout(function () {
					if(loginTo){
						window.location.href = loginTo;
					}else{
						window.location.href = "../manage/";
					}
				}, 2000);
			},
			error: function (e) {
				if (language == 0) {
					layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
				} else if (language == 1) {
					layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
				} else {
					layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
				}
				console.log("checkMobile2");
				console.log(e.status);
				console.log(e.responseText);
			}
		});
	}else{
		if(language==0){
			layer.msg("验证码错误", { icon: 2 });
		}else if(language==1){
			layer.msg("驗證碼錯誤", { icon: 2 });
		}else{
			layer.msg("PIN Error", { icon: 2 });
		}
	}
}

function sendPin() {
	if(language==0){
		layer.msg("验证码已发送", { icon: 1 });
	}else if(language==1){
		layer.msg("驗證碼已發送", { icon: 1 });
	}else{
		layer.msg("PIN Sent", { icon: 1 });
	}
	ifSend = false;
	reSend();
}
function reSend() {
	if (time > 0 && !ifSend) {
		if(language==0){
			$(".Send").html("<span style=\"color: #666666\">重新发送(" + time + "s)</span>");
		}else if(language==1){
			$(".Send").html("<span style=\"color: #666666\">重新發送(" + time + "s)</span>");
		}else {
			$(".Send").html("<span style=\"color: #666666\">Resend(" + time + "s)</span>");
		}
		time--;
	} else {
		if(language==0){
			$(".Send").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">发送验证码</a>");
		}else  if(language==1){
			$(".Send").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">發送驗證碼</a>");
		}else {
			$(".Send").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">Send PIN</a>");
		}
		ifSend = true;
		time = 60;
	}
	if (!ifSend) {
		setTimeout(reSend, 1000);
	}
}

function forget1() {
	layer.open({
		title:'找回密码',
		scrollbar: false,
		btnAlign: 'c',
		area: ['375px', '325px'], //宽高
		content: content1
		,btn: ['确认', '取消']
		,yes: function(){
			findPWD();
		}
		,btn2: function(index, layero){
		}
		,cancel: function(){
		}
	});
}

function forget2() {
	layer.open({
		title:'找回密碼',
		scrollbar: false,
		btnAlign: 'c',
		area: ['375px', '325px'], //宽高
		content: content2
		,btn: ['確認', '取消']
		,yes: function(){
			findPWD();
		}
		,btn2: function(index, layero){
		}
		,cancel: function(){
		}
	});
}

function forget3() {
	layer.open({
		title:'Find password',
		scrollbar: false,
		btnAlign: 'c',
		area: ['375px', '325px'], //宽高
		content: content3
		,btn: ['Sure', 'Cancel']
		,yes: function(){
			findPWD();
		}
		,btn2: function(index, layero){
		}
		,cancel: function(){
		}
	});
}

var content1 = "<ul class='layer'>" +
	"<li><input placeholder='请输入手机号' maxlength='11' id='sjh'></li>"+
	"<li><input placeholder='请输入验证码' maxlength='6' id='yzm'><div class='Send2'><a onclick='checkMobile2()' href=\"javascript:void(0);\">发送验证码</a></div></li>"+
	"<li><input placeholder='请输入新密码' maxlength='16' id='xmm' type='password'></li>"+
	"</ul>";

var content2 = "<ul class='layer'>" +
	"<li><input placeholder='請輸入手機號' maxlength='11' id='sjh'></li>"+
	"<li><input placeholder='請輸入驗證碼' maxlength='6' id='yzm'><div class='Send2'><a onclick='checkMobile2()' href=\"javascript:void(0);\">發送驗證碼</a></div></li>"+
	"<li><input placeholder='請輸入新密碼' maxlength='16' id='xmm' type='password'></li>"+
	"</ul>";

var content3 = "<ul class='layer'>" +
	"<li><input placeholder='Input mobile' maxlength='11' id='sjh'></li>"+
	"<li><input placeholder='Input PIN' maxlength='6' id='yzm'><div class='Send2'><a onclick='checkMobile2()' href=\"javascript:void(0);\">Send PIN</a></div></li>"+
	"<li><input placeholder='Input new PWD' maxlength='16' id='xmm' type='password'></li>"+
	"</ul>";

function sendPin2() {
	if(language==0){
		layer.tips("验证码已发送", ".Send2");
	}else if(language==1){
		layer.tips("驗證碼已發送", ".Send2");
	}else{
		layer.tips("PIN Sent", ".Send2");
	}
	ifSend2 = false;
	reSend2();
}
function reSend2() {
	if (time2 > 0 && !ifSend2) {
		if(language==0){
			$(".Send2").html("<span style=\"color: #666666\">重新发送(" + time2 + "s)</span>");
		}else if(language==1){
			$(".Send2").html("<span style=\"color: #666666\">重新發送(" + time2 + "s)</span>");
		}else {
			$(".Send2").html("<span style=\"color: #666666\">Resend(" + time2 + "s)</span>");
		}
		time2--;
	} else {
		if(language==0){
			$(".Send2").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">发送验证码</a>");
		}else  if(language==1){
			$(".Send2").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">發送驗證碼</a>");
		}else {
			$(".Send2").html("<a style=\"color: #66C3B9\" href=\"javascript:void(0);\">Send PIN</a>");
		}
		ifSend2 = true;
		time2 = 60;
	}
	if (!ifSend2) {
		setTimeout(reSend2, 1000);
	}
}

//校验手机号
function checkMobile1() {
	mobile1 = $("#username").val();
	var reg = /[1][3-9][\d]{9}/;
	if(!reg.test(mobile1)){
		if(language==0){
			layer.tips("请输入正确的手机号","#username");
		}else if(language==1){
			layer.tips("請輸入正確的手機號","#username");
		}else {
			layer.tips("error mobile","#username");
		}
	}else{
		$.ajax({
			url:url+"/user/checkMobile2",
			data: {"phone":mobile1},
			type: "post",
			dateType: "json",
			async: false,
			success: function (response, status, xhr) {
				if(response==""){
					if(language==0){
						layer.tips("该手机号不存在","#username");
					}else if(language==1){
						layer.tips("該手機號碼不存在","#username");
					}else {
						layer.tips("this mobile is`t exist","#username");
					}
				}else{
					pin1 = response;
					sendPin();
				}
			},
			error: function (e) {
				if (language == 0) {
					layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
				} else if (language == 1) {
					layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
				} else {
					layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
				}
				console.log("checkMobile1");
				console.log(e.status);
				console.log(e.responseText);
			}
		});
	}
}

//校验手机号
function checkMobile2() {
	mobile2 = $("#sjh").val();
	var reg = /[1][3-9][\d]{9}/;
	if(!reg.test(mobile2)){
		if(language==0){
			layer.tips("请输入正确的手机号","#sjh");
		}else if(language==1){
			layer.tips("請輸入正確的手機號","#sjh");
		}else {
			layer.tips("error mobile","#sjh");
		}
	}else{
		$.ajax({
			url:url+"/user/checkMobile2",
			data: {"phone":mobile2},
			type: "post",
			dateType: "json",
			async: false,
			success: function (response, status, xhr) {
				if(response==""){
					if(language==0){
						layer.tips("该手机号不存在","#sjh");
					}else if(language==1){
						layer.tips("該手機號碼不存在","#sjh");
					}else {
						layer.tips("this mobile is`t exist","#sjh");
					}
				}else{
					pin2 = response;
					sendPin2();
				}
			},
			error: function (e) {
				if (language == 0) {
					layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
				} else if (language == 1) {
					layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
				} else {
					layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
				}
				console.log("checkMobile2");
				console.log(e.status);
				console.log(e.responseText);
			}
		});
	}
}

function findPWD() {
	var newPWD = $("#xmm").val();
	var yzm = $("#yzm").val();
	var reg = /[\w]{8,16}/;
	if(!reg.test(newPWD)){
		if(language==0){
			layer.tips("密码为8-16位数字或字母或下划线","#xmm");
		}else if(language==1){
			layer.tips("密碼為8-16位數字或字母或下劃線","#xmm");
		}else {
			layer.tips("need 8-16 digits, letters or _","#xmm");
		}
	}else if(yzm!=pin2){
		if(language==0){
			layer.tips("验证码错误", "#yzm");
		}else if(language==1){
			layer.tips("驗證碼錯誤", "#yzm");
		}else{
			layer.tips("PIN Error", "#yzm");
		}
	}else{
		$.ajax({
			url:url+"/user/findPWD",
			data: {"phone":mobile2,"passWord":newPWD},
			type: "post",
			dateType: "json",
			async: false,
			success: function (response, status, xhr) {
				if(response==1){
					if(language==0){
						layer.msg("修改成功,请牢记您的密码!", { icon: 1 });
					}else if(language==1){
						layer.msg("修改成功,請牢記您的密碼!", { icon: 1 });
					}else{
						layer.msg("Modification succeeded, please remember your password!", { icon: 1 });
					}
				}else{
					if (language == 0) {
						layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
					} else if (language == 1) {
						layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
					} else {
						layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
					}
					console.log("findPWD!=1");
				}
			},
			error: function (e) {
				if (language == 0) {
					layer.msg('抱歉!系统错误!请联系客服!', { icon: 5 });
				} else if (language == 1) {
					layer.msg('抱歉!系統錯誤!請聯繫客服!', { icon: 5 });
				} else {
					layer.msg('Sorry!System error!Please contact customer service!', { icon: 5 });
				}
				console.log("findPWD");
				console.log(e.status);
				console.log(e.responseText);
			}
		});
	}
}

function loadUserInfo(id) {
	var result = null;
	$.ajax({
		url:url+"/user2019/qyml/listUsers",
		data: { "id": id },
		type: "get",
		dateType: "json",
		async: false,
		success: function success(response, status, xhr) {
			result = response[0];
		},
		error: function error() {
			layer.msg("系统错误,请联系客服!", { icon: 5 });
			console.log("loadUserInfo error:ajax run failed");
		}
	});
	return result;
}

function getLast(id) {
	var obj = loadUserInfo(id);
	var lastTime = obj.dlsj;
	var lastCity = obj.dlcs;
	if(lastTime==null||lastTime==""){
		lastTime="æ— ";
	}
	if(lastCity==null||lastCity==""){
		lastCity="æ— ";
	}
	sessionStorage.setItem("MiaoMuTime",lastTime);
	sessionStorage.setItem("MiaoMuCity",lastCity);
}

function setLast(id) {
	$.ajax({
		url:url+"/user2019/qyml/setLast",
		data: {"id": id,"dlsj":new Date(),"dlcs":returnCitySN['cname']},
		type: "get",
		dateType: "json",
		async: false,
		success: function success(response, status, xhr) {
			console.log("setLast-response:"+response);
		},
		error: function error() {
			console.log("setLast");
		}
	});
}