$(document).ready(function() {
var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    自用分组列表页    ***************************/
	
	//提交框
	$("#createApgroup,#selectMac,#selectApmacList").focus(function(){
		$(this).val("");
	});
	if($("#createApgroup").val() == ""){
		$(this).val("请填写新增分组名称");
	}
	$("#createApgroup").blur(function(){
		if($(this).val()==""){
			$(this).val("请填写新增分组名称");
		}
	});
	if($("#selectMac").val() == "" || $("#selectApmacList").val() == ""){
		$("#selectMac").val("请填写MAC地址");
		$("#selectApmacList").val("请填写MAC地址");
	}
	$("#selectMac,#selectApmacList").blur(function(){
		if($(this).val()==""){
			$(this).val("请填写MAC地址");
		}
	});
	
	
	$("#btn-createApgroup").click(function(){
		var value = $("#createApgroup").val();
		if(value == "请填写新增分组名称" || value == ""){
			bootbox.alert("请填写新增分组名称!");
			return false;
		}else{
			$.post(ctx+"/operator/main/ap/apgroup/isExist",{name:value},function(data){
				if(data == true){
					bootbox.alert("新增的分组名称已存在!");
					return false;
				}else{
					$.post(ctx+"/operator/main/ap/apgroup/addGroupName",{name:value},function(data){
						refreshGroupFormData();
					});
				}
			});
		}
	});
	
	$(".chooseDiv img").click(function(){
		var id = $(this).attr("valueId");
		$.post(ctx+"/operator/main/ap/apgroup/removeWifiFromGroupByGroupId",{id:id},function(data){
			$("#groupName").val("");
			$("#groupId").val("");
			refreshGroupFormData();
		});
	});
	
	$(".group_left .single_chooseDiv").click(function(){
		var name = $(this).find("label").text();
		$("#groupName").val(name);
		$("#groupId").val($(this).find("label").attr("value"));
		$(this).addClass("lableChange");
		refreshGroupFormData();
	});
	
	$(".group_left .single_chooseDiv").each(function(){
		if($(this).find("label").attr("value") == $("#groupId").val()){
			$(this).addClass("lableChange");
		}else{
			$(this).removeClass("lableChange");
		}
	});
	
	$("#btn-selectMac").click(function(){
		refreshGroupFormData();
	});
	
	$("#btn-deleteMac").click(function(){
		var checkboxs = $("table tbody .checkboxStyle_change");
		var ids = [];
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				var id = parseInt($(this).attr("id"));
				ids.push(id);
			});
		}else{
			bootbox.alert("请选择需要删除的数据!");
			return false;
		}
		$.ajax({
			async: false, 
			traditional: true,
            type: "POST",
            url: ctx+"/operator/main/ap/apgroup/removeWifiFromGroupByWifiId",
            data:{"id":ids,groupId:$("#groupId").val()},
            success: function(data){
            	refreshGroupFormData();
            }
		});
		return false;
	});
	
	// 分页中超链接跳转
	$('.group_left .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#groupForm').find('input:hidden[name="page1"]').val(_page);
		refreshGroupFormData();
		return false;
	});
	
	// 分页中超链接跳转
	$('.group_right .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#groupForm').find('input:hidden[name="page"]').val(_page);
		refreshGroupFormData();
		return false;
	});
	
	/***************************    自用分组分类    ***************************/
	
	$("#btn_formSubmit").click(function(){
		if($("#selectApmacList").val() == "请填写MAC地址"){
			$("#selectApmacList").val("");
		}
		$("#myform").submit();
		return false;
	});
	
	$("#btn-submit").click(function(){
		var checkboxs = $("table tbody .checkboxStyle_change");
		var ids = [];
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				var id = parseInt($(this).attr("id"));
				ids.push(id);
			});
		}else{
			bootbox.alert("请选择表格中需要分组的数据!");
			return false;
		}
		var wifiGroupInfoId = $(".group_bottom input[type='radio']:checked").val();
		if(wifiGroupInfoId == undefined || wifiGroupInfoId == ""){
			bootbox.alert("请选择对应的分组名!");
			return false;
		}
		$.ajax({
			async: false, 
			traditional: true,
            type: "POST",
            url: ctx+"/operator/main/ap/apgroup/save",
            data:{"id":ids,"wifiGroupInfoId":wifiGroupInfoId},
            success: function(data){
            	if($("#selectApmacList").val() == "请填写MAC地址"){
        			$("#selectApmacList").val("");
        		}
            	$("#myform").submit();
            }
		});
		return false;
	});
	
});

function refreshGroupFormData(){
	if($("#selectMac").val() == "请填写MAC地址"){
		$("#selectMac").val("");
	}
	$("#groupForm").submit();
	return false;
}