//Author @wanfu
//给下拉列表框准备数据
function loadDataForPullDownList(controlView, parentId, url, defaultLabel) {
	//清空下拉框列表
	
	controlView.empty();
	//增加一个默认的列
	controlView.append(
		'<option value="-1">' + defaultLabel + '</option>'
	);
	
	//json请求,请求地址是url, 返回数据为data,回调函数去处理
	$.getJSON(url, function(data) {
		//打印控制台信息
		console.log(data);
		console.log(data[parentId]);
		//遍历后端传回来的json数据，追加在select后面，value是id，label是值
		$.each(data[parentId], function(key, val) {
			controlView.append(
				'<option value=' + val.value + '>' + val.label + '</option>'
			);
		});
	});
	//设置默认显示的列是-1；
	controlView.val("-1");
}

//清除下拉框数据
function clearPullDownList(controlView, defaultLabel) {
	controlView.empty();
	controlView.append(
		'<option value="-1">' + defaultLabel + '</option>'
	);
}

//调用loadDataForPullDownList，给广告主准备数据
function loadAdvertiser() {
	loadDataForPullDownList($('#selectedAdvertiserId'), "advertiserList", "/ssp_web_back/widget/selector/provide/advertiser?parentId=1", "全部广告主");
}

////调用loadDataForPullDownList，给订单准备数据
function loadCampaignGroup(advertiserId) {
	loadDataForPullDownList($('#selectedCampaignGroupId'), "orderList", "/ssp_web_back/widget/selector/provide/order?parentId=" + advertiserId, "全部订单");
}

//调用loadDataForPullDownList，给活动准备数据
function loadCampaign(campaignGroupId) {
	loadDataForPullDownList($('#selectedCampaignId'), campaignGroupId, "/ssp_web_back/widget/selector/provide/campaign?parentId=" + campaignGroupId, "全部活动");
}

//调用loadDataForPullDownList，给策略准备数据
function loadAdgroup(campaignId) {
	loadDataForPullDownList($('#selectedAdgroupId'), campaignId, "/ssp_web_back/widget/selector/provide/ad-group?parentId=" + campaignId, "全部投放");
}


$(document).ready(function(){
	//进入页面首先将广告主的下拉列表数据拿出来
	loadAdvertiser();

	//广告主value change调用此函数， 清除活动列表和策略列表
	$('#selectedAdvertiserId').change(function() {
		clearPullDownList($('#selectedCampaignId'), "全部活动");
		clearPullDownList($('#selectedAdgroupId'), "全部投放");
		var id = $(this).val();
		loadCampaignGroup(id);
	});
	
	
	//订单value change调用此函数， 清除策略列表
	$('#selectedCampaignGroupId').change(function() {
		clearPullDownList($('#selectedAdgroupId'), "请选择投放");
		var id = $(this).val();
		loadCampaign(id);
	});
	
	//活动value change调用此函数
	$('#selectedCampaignId').change(function() {
		var id = $(this).val();
		loadAdgroup(id);
	});
});
