$(document).ready(function() {
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************        新增数聚场景列表页    ***************************/

	$("#btn-submit").click(function(){
		var checkboxs = $("#place_left input[type='checkbox']:checked");
		var placeId = "";
		if(checkboxs.length > 0){
			placeId = checkboxs.map(function(i,span){
				return $(span).val();
			}).toArray().join('-');
		}else{
			bootbox.alert("请选择待分类的子场景!");
		}
		var placeClassId = $("#place_right input[type='radio']:checked").val();
		if(placeClassId == undefined || placeClassId == ""){
			bootbox.alert("请选择对应的数据场景!");
		}
		$.post(ctx+"/back/main/ap/place/add",{placeId:placeId,placeClassId:placeClassId},function(data){
			$("#placeTopform").submit();
		});
		return false;
	});
	
	//获取所有的子场景
	if($("#main_placeClassIdul").html()){
		$("#placeClassIdul_son_ul").hide();
		$("#placeClassIdul_span").on("click",function(){
			if($('#placeClassIdul_son_ul').height()>210){
				$('#placeClassIdul_son_ul').addClass("son_ul_change");
	 		}
	 		$('#placeClassIdul_son_ul').show(); //ul显示
		});
		$("ul#placeClassIdul_son_ul li").live("click",function(){
	 		$("#placeClassIdul_span").text($(this).text());
	 		$("#placeClassId").val($(this).attr("value"));
	 		$("#placeClassIdul_son_ul").hide(); //ul隐藏
	 		var url = "";var param = null;
	 		if($("#placeClassId").val() == "-1"){
	 			url = ctx+"/back/main/ap/place/get-place";
	 			param = null;
	 		}else{
	 			url = ctx+"/back/main/ap/place/placeName";
	 			param = {
	 				'placeClassId':$("#placeClassId").val()
	 			};
	 		}
	 		refreshPlaceData();
		});
		$("#placeClassIdul_box").mouseleave(function(){
			$('#placeClassIdul_son_ul').hide(); //ul隐藏
			$('#placeClassIdul_son_ul').removeClass("son_ul_showchange");
		});
	}
	
	$("#btn-delete").click(function(){
		var checkboxs = $("#place_bottom input[type='checkbox']:checked");
		var id = [];
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				id.push(parseInt($(this).val()));
			});
		}else{
			bootbox.alert("请选择需要删除的数据子场景!");
		}
		$.ajax({
			async: false, 
			traditional: true, 
            type: "POST",
             url: ctx+"/back/main/ap/place/delete-place",
             data:{"id":id},
             success: function(data){
            	 refreshPlaceData();
             }
		});
		return false;
	});
	
	
});

function refreshPlaceData(){
	var url = "";var param = null;
	if($("#placeClassId").val() == "-1"){
		url = $("#ctx").val()+"/back/main/ap/place/get-place";
		param = null;
	}else{
		url = $("#ctx").val()+"/back/main/ap/place/placeName";
		param = {
			'placeClassId':$("#placeClassId").val()
		};
	}
	$.post(url,param,function(data){
		$("#place_bottom .input-checkboxsDiv").empty();
		var html = "";
		$.each(data,function(key, val){
			html += '<div class="single_div">';
			html += '<input class="single_div_checkbox" name="place" type="checkbox" value='+val.id+'><span class="single_div_span">'+val.name+'</span>';
			html += '</div>';
		});
		$("#place_bottom .input-checkboxsDiv").append(html);
	});
}