$(function() {
	
	// 初始化加载校区
	$.ajax({
		type : 'POST',
		url : '../siteApplication/findOrganization',
		dataType : 'json',// 返回值类型 一般设置为json
		success : function(data) // 服务器成功响应处理函数
		{
			$("#organization").empty();
			var subs = data.msg;// 获取data中list的数组
			var _array = [];
//			_array.push("<option value=''>--请选择--</option>");
			_array.push("");
			for ( var i = 0; i < subs.length; i++) {
				_array.push("<option value='" + subs[i].id + "'>"+ subs[i].name + "</option>");
				$("#organization").html(_array.join(""));
			}
			$("#organization").html(_array.join(""));
			// 初始化加载楼栋
//			var organizationId = $("#organization").find("option:selected").text();
			var organizationId = $("#organization").val();
			$.ajax({
				type : 'POST',
				url : '../siteApplication/findBuilding',
				data : {"organizationId":organizationId},
				dataType : 'json',// 返回值类型 一般设置为json
				success : function(data) // 服务器成功响应处理函数
				{
					$("#building").empty();
					var subs = data.msg;// 获取data中list的数组
					var _array = [];
					_array.push("");
					for ( var i = 0; i < subs.length; i++) {
						_array.push("<option value='" + subs[i].id + "'>"+ subs[i].name + "</option>");
						$("#building").html(_array.join(""));
					}
					$("#building").html(_array.join(""));
					
					//初始化预定数据
					initListInfo(1);
				}
			});
			
		}
	
	});
	
	
    //场地预定
    $("#addSiteApplication").click(function(){
    	var arrs = $("#timeArrs").val();
    	var organizationName = $("#organization").find("option:selected").text();
        var buildingId = $("#building").val();
        var organizationId = $("#organization").val();
        var buildingName = $("#building").find("option:selected").text();
        var siteName = $("#siteName").find("option:selected").text();
        var siteId = $("#siteId").val();
        var dateTime = $("#dateTime").val();
        window.location.href="../siteApplication/findAddSiteApplication?siteId="+siteId+"&buildingId="+buildingId+"&dateTime="+dateTime+
        "&siteName="+siteName+"&organizationName="+organizationName+"&buildingName="+buildingName+"&organizationId="+organizationId;
    });
	
	//查询
	$("#btn-search").click(function(){
		initListInfo(1);
	});
})

//加载楼栋
function initBuilding(){
		var organizationId = $("#organization").val();
		$.ajax({
			type : 'POST',
			url : '../siteApplication/findBuilding',
			data : {"organizationId":organizationId},
			dataType : 'json',// 返回值类型 一般设置为json
			success : function(data) // 服务器成功响应处理函数
			{
				$("#building").empty();
				var subs = data.msg;// 获取data中list的数组
				var _array = [];
				_array.push("");
				for ( var i = 0; i < subs.length; i++) {
					_array.push("<option value='" + subs[i].id + "'>"+ subs[i].name + "</option>");
					$("#building").html(_array.join(""));
				}
				$("#building").html(_array.join(""));
				
			}
		});
}

function initListInfo(pageNo){
		var pageNo = $("#pageNo").val();
		if(pageNo == null || pageNo == ''){
			pageNo = 1;
		}
		var organizationId = $("#organization").val();//高校id
		var buildingId = $("#building").val();//楼栋id
		var type = $("#type").val();
		var dateTime = $("#dateTime").val();
		
		if(dateTime == null || dateTime ==""){
			layer.alert("请输申请时间！", "提示信息");
			return;
		}
		
		var organizationName = $("#organization").find("option:selected").text();
        var buildingName = $("#building").find("option:selected").text();
        
		//初始化加载
		$.ajax({
			type:'POST',
			url:'../siteApplication/listInfo',
			data:{"pageNo":pageNo,"organizationId":organizationId,"buildingId":buildingId,"type":type,"dateTime":dateTime,
				"organizationName":organizationName,"buildingName":buildingName},
			dataType: 'html',//返回值类型 设置为html
			success: function (data)  //服务器成功响应处理函数
			{
	            $("#siteApplicationList").html(data);
			}
		});
		
	}

function dele(id){
		if(confirm("确定删除？")){
			window.location.href="delete/"+id+"&"+$("#funcid").val();
		}	
	}

