function checkLogin(){
	var data = $("#loginForm").serializeJSON();
	if(data.loginName.isEmpty()){
		alert("请输入登录用户名。", 0, function(){
			$("input[name='loginName']").focus();
		});
		return false;
	}
	if(data.password.isEmpty() || !/^[\w-_.]{6,20}$/i.test(data.password)){
		alert("请输入由6-20位的字母、数字和下划线组成的密码。", 0, function(){
			$("input[name='password']").focus();
		});
		return false;
	}
	if($("input[name='rememberme']").is(":checked") == true){
		$.cookie.set("rememberme", "true", { expires: 7 });
		$.cookie.set("usernameCookie", data.loginName,{expires:7});
		$.cookie.set("passwordCookie", data.password,{expires:7});
    }else{
    	$.cookie.set("rememberme", "false", { expires: -1 });
    	$.cookie.set("usernameCookie", '', { expires: -1 });
    	$.cookie.set("passwordCookie", '', { expires: -1 });
    }
	data.password = $.md5(data.password);
	ajax("doLogin.do",data,function(res){
		if(res.code == 1){
			window.location.href = res.redirect;
		}else{
			alert(res.msg);
		}
	});
	return false;
}

function checkPwd(){
	var data = $("#pwdModfiy").serializeJSON();
	if(data.password.isEmpty()){
		top.alert("请输入当前登录密码。");
		$("#password").focus();
		return false;
	}
	if(data.new_password.isEmpty() || !/^[\w-_.]{6,16}$/i.test(data.new_password)){
		top.alert("请输入由6-20位的字母、数字和下划线组成的新密码。");
		$("#new_password").focus();
		return false;
	}
	if(data.confirm_password.isEmpty() || !/^[\w-_.]{6,16}$/i.test(data.confirm_password)){
		top.alert("请再次输入新密码。");
		$("#confirm_password").focus();
		return false;
	}
	if(data.new_password != data.confirm_password){
		top.alert("新登录密码不一致。");
		$("#confirm_password").focus();
		return false;
	}
	data.password = $.md5(data.password);
	data.new_password = $.md5(data.new_password);
	console.log(data);
	data.confirm_password = "";
	ajax("updpassword.do",data,function(res){
		if(res.code == 1)
			top.alert({title:"密码修改提示",nav:res.msg,flag:1,next:function(){
				top.window.location.href = _PUBLIC_ + "logout.do";
				top.$.closeWindow();
			}});
		else
			top.alert(res.msg);
	});
	return false;
}

function checkUser(){
	var data = $("#addUserForm").serializeJSON();
	if(data.nickName.isEmpty()){
		top.alert("姓名不能为空。");
		$("#nickName").focus();
		return false;
	}
	if(data.loginName.isEmpty()){
		top.alert("用户名不能为空。");
		$("#loginName").focus();
		return false;
	}
	if(data.loginPwd.isEmpty() || !/^[\w-_.]{6,16}$/i.test(data.loginPwd)){
		top.alert("请输入由6-20位的字母、数字和下划线组成的密码。");
		$("#loginPwd").focus();
		return false;
	}
	
	if(data.loginRePwd.isEmpty() || data.loginPwd!=data.loginRePwd){
		top.alert("确认密码为空或与登录密码不一致。");
		$("#loginRePwd").focus();
		return false;
	}
	if(data.roleId.isEmpty()){
		top.alert("角色功能不能为空。");
		return false;
	}
	data.loginPwd = $.md5(data.loginPwd);
	ajax("saveuser.do",data,function(res){
		if(res.code == 1)
			top.alert(res.msg,1,function(){
				top.window.location.href = "userlist.do";
				top.$.closeWindow();
			});
		else
			alert(res.msg);
	});
	return false;
}

function checkUserModfiy(){
	var data = $("#modfiyUserForm").serializeJSON();
	if(data.nickName.isEmpty()){
		top.alert("姓名不能为空。");
		$("#nickName").focus();
		return false;
	}
	if(!data.loginPwd.isEmpty() && !/^[\w-_.]{6,16}$/i.test(data.loginPwd)){
		top.alert("请输入由6-20位的字母、数字和下划线组成的密码。");
		$("#loginPwd").focus();
		return false;
	}
	
	if(!data.loginPwd.isEmpty() && (data.loginRePwd.isEmpty() || data.loginPwd!=data.loginRePwd)){
		top.alert("确认密码为空或与登录密码不一致。");
		$("#loginRePwd").focus();
		return false;
	}
	if(data.loginName.isEmpty()){
		top.alert("手机号不能为空。");
		$("#loginName").focus();
		return false;
	}
	if(data.roleId.isEmpty()){
		top.alert("角色功能不能为空。");
		return false;
	}
	if(!data.loginPwd.isEmpty()){
		data.loginPwd = $.md5(data.loginPwd);
	}
	ajax("updateuser.do",data,function(res){
		if(res.code == 1)
			top.alert(res.msg,1,function(){
				top.window.location.href = "userlist.do";
				top.$.closeWindow();
			});
		else
			alert(res.msg);
	});
	return false;
}

// 修改当前用户的信息（密码）
function saveCurrentAccount(){
	var data = $("#dataForm").serializeJSON();
	if(data.password.isEmpty()){
		top.alert("原密码不能为空。");
		$("#password").focus();
		return false;
	}
	if(data.new_password.isEmpty()){
		top.alert("新密码不能为空。");
		$("#new_password").focus();
		return false;
	}
	if(!/^[\w-_.]{6,20}$/i.test(data.new_password)){
		top.alert("密码长度应为 6~20 位。");
		$("#new_password").focus();
		return false;
	}
	if(data.new_passwords.isEmpty()){
		top.alert("请再次输入新密码。");
		$("#new_passwords").focus();
		return false;
	}
	if(data.new_password != data.new_passwords){
		top.alert("两次密码不一致。");
		return false;
	}
	ajax("updpassword.do",data,function(res){
		if(res.code == 1){
			top.alert(res.msg,1,function(){
//				top.window.location.href = "userlist.do";
				top.$.closeWindow();
			});
		}else{
			top.alert(res.msg);
		}
	});
}

function delUser(){
	var uids = "";
	$('input:checkbox[name=item]:checked').each(function(i){
		if(0==i){
			uids = $(this).val();
		}else{
			uids += (","+$(this).val());
		}
	});
	if(uids == ''){
		alert('请选择至少一条记录');
		return;
	}
	confirm("确认删除吗",function(){
		var data ={"uids" : uids};
		ajax("deleteuser.do",data,function(res){
			if(res.code == 1)
				alert(res.msg,1,function(){
					window.location.href = "userlist.do";
				});
			else
				alert(res.msg);
		});
	});
	//return false;
}

function delUserByUid(uid){
	confirm("确认删除吗",function(){
		var data ={"uids" : uid};
		ajax("deleteuser.do",data,function(res){
			if(res.code == 1)
				alert(res.msg,1,function(){
					window.location.href = "userlist.do";
				});
			else
				alert(res.msg);
		});
	});
}



function changeInput(){
	var val = $(this).val(), textArr = ["输入登录名查询","输入用户名称查询"];
    $("#key_word").siblings("label").text(textArr[(val == "loginName" ? "0":"1")]);
}

function getList(page){
	page = page || 1;
	var pageSize = "10", data = {
		"pageNo": page,
		"pageSize": pageSize,
		"nickName": $("#nickName").val(),
		"loginName": $("#loginName").val(),
		"telephone": $("#telephone").val(),
		"email": $("#email").val(),
		"userSex": $("#userSex").val(),
		"userRole": $("#userRole").val()
	}, html = "";
	$("#page, #list").empty();
	ajax("ajaxSearchUserList.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				var btnHTML="<a href=\"javascript:$.openWindow('修改用户', 'edituser.do?uid="+item.uid+"', 650, 270, true);\" >编辑</a>&nbsp;&nbsp;" +
					"<a href=\"javascript:delUserByUid("+item.uid+");\" >删除</a>";
				html += "<tr>\n\
					<td><input type='checkbox' class='checkbox {click:itemCheck}' name='item' value='"+item.uid+"' /><label></label></td>\n\
					<td>"+item.loginName.toNull()+"</td>\n\
					<td>"+item.nickName.toNull()+"</td>\n\
					<td>"+item.telephone.toNull()+"</td>\n\
					<td>"+item.email.toNull()+"</td>\n\
					<td>"+(item.sex).toString().decode(0,"男",1,"女","")+"</td>\n\
					<td>"+item.roleName.toNull()+"</td>\n\
					<td class='edit'>" +btnHTML+"</td>\n\
				</tr>";
			});
			$("#list").html(html);
			$.checkbox();
			var newpage = new createPage(res.count,page,pageSize,3);
			$("#page").html(newpage.pageHtml);
			$('#count').text(res.count);
		}else{
			$("#list").info("暂无数据。");
			$('#count').text(0);
		}
	});
}

pageGo = getList;

/**
 * 导入数据
*/
function uploadExcel(){
	var url = "uploadExcelForUser.do";
	$("#excelInfo").attr("action",url).submit();
}



//导入结果
function excelCallback(code, msg) {
    if (code == 1) {
        // 导入成功
    	alert("导入成功。", 1, function(){
			window.location.href = "userlist.do";
		});
    } else {
    	// 导入失败
    	alert(msg, 0, function(){
        	location.reload();
		});
    }
}
