$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    权限管理列表页    ***************************/

	//开启,暂停
	$("#btn-enable,#btn-disable").click(function(){
		if($(this).attr("id") == "btn-enable"){
			url = ctx+"/operator/main/authority/enable";
		}else if($(this).attr("id") == "btn-disable"){
			url = ctx+"/operator/main/authority/disable";
		}
		singleOper($(this),url);
	});
			
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/operator/main/authority/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/operator/main/authority/disable";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑账号    ***************************/
	
	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	
	$("#footBtn .btn-save").click(function(){
		var authority = "";
		$("#checkboxs input[type=checkbox]:checked").each(function(){
		    	authority+=$(this).val()+",";
		});
		authority = authority.substring(0,authority.length-1);
		$("#authority").val(authority);
		if($("#accountId").val() == "0"){//创建账号时校验账号是否重复
			var flag = isExistName();//判断账号名称是否重复
			if(flag){
				return false;
			}
		}
		if($("#ensurePassword").val() !="" && $("#ensurePassword").val() !=$("#password").val()){
			$("#ensurePassword").css("box-shadow","0 0 6px #ff0000").attr('title','确认密码必须与密码一致！');
			$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("确认密码必须与密码一致！");
			return false;
		}
		if (!$("#validateForm").validate()) {
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$("#footBtn .btn-cancel").click(function(){
		window.location.href = ctx+"/operator/main/authority/list";
	});
	
	//个人信息保存按钮
	$("#btn-save-account").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		var myform = $("#validateForm").serialize();
		$.post(ctx+"/operator/main/authority/saveloginperson",myform,function(data){
			if($("#password").val() == "" && $("#ensurePassword").val() == ""){
				window.location.href = ctx+"/operator/main/dashboard";
			}else{
				window.location.href = ctx+"/login";
			}
		})
		return false;
	});
	
	//个人信息取消按钮
	$("#btn-cancel-account").unbind('click').click(function(){
		location.href = ctx+"/operator/main/dashboard";
	});
	
	$("#accountName").blur(function(){
		if($(this).val() != ""){
			var reg=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if($(this).val().match(reg)!=null){
				$(this).css("box-shadow","none").attr('title','');
				$(this).parent("div").find("span.spanVaildate").html("");
				isExistName();
			}else{
				$(this).css("box-shadow","0 0 6px #ff0000").attr('title','格式必须为邮箱类型， 如: example@email.com');
				$(this).parent("div").find("span.spanVaildate").html("格式必须为邮箱类型， 如: example@email.com");
				return false;
			}
		}else{
			$(this).css("box-shadow","none");
		}
	});
	
	$("#accountName").focus(function(){
		$(this).css("box-shadow","0 0 6px #ff0000");
	});
	
	//显示密码点击事件
	$("#input-password-create").click(function(){
		if($(this).attr("checked")){
			$("#password-span").html('<input class="input-text" tabindex="2" id="password" name="password" type="text" value="'+$("#password").val()+'" verify="unspecial/100/1/0"/>');
			$("#ensurePassword-span").html('<input class="input-text" tabindex="3" id="ensurePassword" name="ensurePassword" type="text" value="'+$("#ensurePassword").val()+'" verify="unspecial/100/1/0"/>');
		}else{
			$("#password-span").html('<input class="input-text" tabindex="2" id="password" name="password" type="password" value="'+$("#password").val()+'" verify="unspecial/100/1/0"/>');
			$("#ensurePassword-span").html('<input class="input-text" tabindex="3" id="ensurePassword" name="ensurePassword" type="password" value="'+$("#ensurePassword").val()+'" verify="unspecial/100/1/0"/>');
		}
		//改变密码框样式时触发
		$("#ensurePassword").blur(function(){
			validPassword();
		});
	});
	
	//显示密码点击事件
	$("#input-password-edit").click(function(){
		if($(this).attr("checked")){
			$("#password").attr("disabled",false);
			$("#ensurePassword").attr("disabled",false);
			$("#password").attr("verify","unspecial/100/1/0");
			$("#ensurePassword").attr("verify","unspecial/100/1/0");
		}else{
			$("#password").val("");
			$("#ensurePassword").val("");
			$("#password").attr("disabled",true);
			$("#ensurePassword").attr("disabled",true);
			$("#password").attr("verify","unspecial/100/0/0");
			$("#ensurePassword").attr("verify","unspecial/100/0/0");
			$("#password").css("box-shadow","none").attr('title','');
			$("#password").parent("span").parent("div").find("span.spanVaildate").html("");
			$("#ensurePassword").css("box-shadow","none").attr('title','');
			$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("");
		}
	});
		
	//确认密码与密码一致
	$("#ensurePassword").blur(function(){
		validPassword();
	});
	
	$("#ensurePassword, #password").focus(function(){
		$(this).css("box-shadow","0 0 6px #ff0000");
	});
	
	$("#ensurePassword, #password").blur(function(){
		$(this).css("box-shadow","none");
	});
		
	function validPassword(){
		var password = $("#password").val();
		var ensurePassword = $("#ensurePassword").val();
		if(ensurePassword != ""){
			if(ensurePassword != password){
				$("#ensurePassword").css("box-shadow","0 0 6px #ff0000").attr('title','确认密码必须与密码一致！');
				$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("确认密码必须与密码一致！");
				return false;
			}else{
				$("#ensurePassword").css("box-shadow","none").attr('title','');
				$("#ensurePassword").parent("span").parent("div").find("span.spanVaildate").html("");
			}
		}
	}
	
	//权限控制
	if($(".authority-edit").html()){
		var authority = $("#authority").val();
		var authArr = authority.split(",");
		var checkboxs = $("#checkboxs input[type='checkbox']");
		checkboxs.each(function(i){
			for(var i = 0;i<authArr.length;i++){
				if($(this).val() == authArr[i]){
					$(this).attr("checked","true");
				}
			}
		});
	}
});
		
function exportExcel(){
	var searchValue = $("#searchValue").val();
	var param = "";
	if(searchValue == "请输入关键字"){
		param = "";
	}else{
		param = "?value="+searchValue;
	}
	$("#exportExcel").attr("href",$("#ctx").val()+"/operator/main/authority/exportDataReportExcel"+param);
	return false;
}

function isExistName(){
	var flag = false;
	var accountName = $("#accountName").val();
	$.ajax({
       	type: "POST",
        url: $("#ctx").val()+"/operator/main/authority/isExist",
        async: false,
        data: {accountName:accountName},
         success: function(data){
	 		if(data == "YES"){
	 			$("#accountName").css("box-shadow","0 0 6px #ff0000").attr('title','你输入的账号已存在！');
	 			$("#accountName").parent("div").find("span.spanVaildate").html("你输入的账号已存在！");
	 			flag = true;
	 		}else{
	 			$("#accountName").css("box-shadow","none").attr('title','');
	 			$("#accountName").parent("div").find("span.spanVaildate").html("");
	 			flag = false;
	 		}
         }
	});
	return flag;
}
