/**
 * 楼栋回填列表
 */
$(function(){

	//点击清空按钮
	$('#popBuildListTableDiv #clearbuild-btn').click(function(){
		var data =buildList.data;
		data.communityId = "";
		data.buildName = "";
		buildList.backfillFun(data);
		layer.closeAll();
	});

	
	/**
	 * 实现分页条件查询
	 */
	console.log('popBuildListTableDiv');
	$('#popBuildListTableDiv div.my-pagination > div > a').click(function(){
		var data = {};
		data.communityId=$('#buildPageCommunityId').val();
		data.buildName = $('#buildPageBuildId').val();
		var href = $(this).attr('href');
		//截取href中#之后的值
		var str = href.substring(1,href.Length);
		data.page=parseInt(str);
		getPopTablePageData(data);
	});
	$('#popBuildListTableDiv div.my-pagination .page-current').change(function(){
		var data = {};
		data.communityId=$('#buildPageCommunityId').val();
		data.buildName = $('#buildPageBuildId').val();
		var page = $(this).val();
		data.page=parseInt(page);
		getPopTablePageData(data);
	});
	var radioboxs = $("table.my-table-body tbody ").find(':input[type=radio]');

	alreadyChoose(radioboxs,buildList.data);
	adjustTableCellWidth('popBuildListTableDiv');
	
	/**
	 * 获取回填数据
	 */
	$('#popBuildListTableDiv > div > table.build_backfill > tbody > tr ').click(function(){		
		var data = {};
		data.id = $(this).find(':input[type=radio]').val();
		data.name = $(this).find('td:eq(1)').html();
		buildList.backfillFun(data);
		layer.closeAll();
	});
});

function getPopTablePageData(data){
	doDataPost(data, false, ctx+"/admcommon/build/backfill_radio", function(msg){
		$($('.layui-layer-content')[0]).empty();
		$($('.layui-layer-content')[0]).append(msg);
		layer.iframeAuto(indexLayer);
	});
}


/**
 * 已经选择的楼栋
 */
function alreadyChoose(radioboxs,data){	
	var radios = radioboxs;
	var checkedIds = data.checkedIds;
	$(radios).each(function(){
		if(isChecked(this.value,checkedIds)){			
			$(this).attr("checked","checked");			
		}
	});
}

function isChecked(v,ids){	
	if(typeof(v) == "undefined"){
		return false;
	}else if(typeof(ids) == "undefined"){
		return false;
	}
	for(var i=0,a;a=ids[i++];){		
	  if(v == a){
		  return true;
	  }
	}
	return false;
}