var bench_type = 1 , curTab = 1;

function delBenId(obj){
	var somearray = decodeURI($.getParam("ids")).split('|');
	if(somearray.length == 1){
		 top.alert('至少保留一个广告');
	     return false;
	}
	$(obj).closest("li").remove();
	var startDate = $("#start_time").val(), endDate = $("#end_time").val() ,day = $("#day").val();
	removeByValue(somearray, obj.dataset.id);
	window.location.href='benchmarking.do?type='+$("#type").val()+'&ids='+encodeURI(somearray.join('|'))+'&startDate='+startDate+'&endDate='+endDate+'&day='+day;
}
function addBenId(){
	var ids = $.getParam("ids"), startDate = $("#start_time").val(), endDate = $("#end_time").val(),day = $("#day").val();
	if(ids == null || ids == undefined){
		ids = '';
	}
	var url = 'add_benchmarking.do?type='+$("#type").val()+'&ids='+ids+'&startDate='+startDate+'&endDate='+endDate+'&day='+day;
	$.openWindow('请添加对标广告',url, 470, 260, true, null, 'auto');
}

function changeBenchTab(){
	getBenchmarkingPieData();
}

/**
 * 切换时间
 */
function changeDateTab(day){
	var end_date = '', start_date = '';
	if(day > 0){
		end_date = new Date().addDate(-1).Format("yyyy-MM-dd"),
		start_date = new Date(end_date).addDate(-day).Format("yyyy-MM-dd");
		$('#start_time').val(start_date);
		$('#end_time').val(end_date);
	}else{
		$('#start_time').val('');
		$('#end_time').val('');
	}
	$("#day").val(day); //存储选择天数
	$(".text").input();
	changeBenchTab();
	
}

// 竞品卫视投放频次/时长分布比较
function getBenchmarkingPieData(){
    var brandNameList = $("#brandNameList").val().split('|');
	var total = brandNameList.length,
	type = $("#type").val() , startDate = $("#start_time").val(), endDate = $("#end_time").val();
	for(var index = 0; index < total; index++) {
       getMediaChartsData(type, brandNameList[index] ,index+1 , startDate, endDate);
       getTVChartsData(type, brandNameList[index] ,index+1 , startDate, endDate);
    }
}
 
// 媒体分布
function getMediaChartsData(type,productName,index, startDate,endDate){
	var data = {
			"type" : type,
			"productName": productName,
			"startDate": startDate,
			"endDate": endDate
		};
	program_arr = [];
	ajax("media_dist.do",data,function(res){
		var chartFrequency = "chart1_"+(index);
		var chartDuration = "chart2_"+(index);
		if(res.code == 1 && res.data.length > 0){
		  var legend= [], series = [],xdata = [],ydata = [];
			var xOther = 0, yOther = 0;

			data1 = res.data.sort(function(a,b){
                return b.duration-a.duration;
            });

			$.each(data1, function(index, item){
				if (index >= 10){
					yOther += item.duration;
					return true;
				}
			    legend.push(item.name);
                // 时长
                var lv2 = {
                    value: item.duration,
                    name : item.name
                };
                ydata.push(lv2);
			});

            data2 = res.data.sort(function(a,b){
                return b.total-a.total;
            });
			$.each(data2, function(index, item){
                if (index >= 10){
                    xOther += item.total;
                    return true;
                }
                legend.push(item.name);
                // 频次
                var lv1 = {
                    value: item.total,
                    name : item.name
                };
                xdata.push(lv1);
            });

			// 其它
			if (xOther > 0){
				var temp = {
					value: xOther,
					name : '其它'
				};
				xdata.push(temp);
			}
			if (yOther > 0){
				var temp = {
					value: yOther,
					name : '其它'
				};
				ydata.push(temp);
			}
			drawPie("频次分布",xdata,chartFrequency) ;
			drawPie("时长分布",ydata,chartDuration);
		}else{
			$('#'+chartFrequency).info("暂无数据。", true);
			$('#'+chartDuration).info("暂无数据。", true);
		}
		
	},false);
}


// 电视剧分布
function getTVChartsData(type,productName,index, startDate,endDate){
	var data = {
			"type" : type,
			"productName": productName,
			"startDate": startDate,
			"endDate": endDate
		};
	program_arr = [];
	ajax("tv_dist.do",data,function(res){
	    var chartFrequency = "chart3_"+ (index);
		var chartDuration = "chart4_"+(index);
		if(res.code == 1 && res.data.length > 0){
	         var legend= [], series = [],xdata = [],ydata = [];
			var xOther = 0, yOther = 0;

			data1 = res.data.sort(function(a,b){
                return b.duration-a.duration;
            });

			$.each(data1, function(index, item){
				if (index >= 10){			
					yOther += item.duration;
					return true;
				}
			    legend.push(item.name);
                // 时长
                var lv2 = {
                    value: item.duration,
                    name : item.name
                };
                ydata.push(lv2);
			});

            data2 = res.data.sort(function(a,b){
                return b.total-a.total;
            });
			$.each(data2, function(index, item){
                if (index >= 10){
                    xOther += item.total;
                    return true;
                }
                legend.push(item.name);
                // 频次
                var lv1 = {
                    value: item.total,
                    name : item.name
                };
                xdata.push(lv1);
            });

			// 其它
			if (xOther > 0){
				var temp = {
					value: xOther,
					name : '其它'
				};
				xdata.push(temp);
			}
			if (yOther > 0){
				var temp = {
					value: yOther,
					name : '其它'
				};
				ydata.push(temp);
			}
			drawPie("频次分布",xdata,chartFrequency);
			drawPie("时长分布",ydata,chartDuration);
		}else{
			$('#'+chartFrequency).info("暂无数据。", true);
			$('#'+chartDuration).info("暂无数据。", true);
		}
	},false);
}

// 扇形图显示
function drawPie(title,series, divId){
option = {
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    series: [
        {
			color: ['#1890FF','#F6BD16','#E8684A','#6DC8EC','#9270CA','#FF9D4D','#269A99','#73A0FA','#5AD8A6','#5D7092','#6DC8EC','#9270CA','#FF9D4D','#269A99'],
            name:title,
            type: 'pie',
            radius: '75%',
            center: ['50%', '50%'],
            data: series,
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
};
var myChart = echarts.init(document.getElementById(divId));
myChart.clear();
myChart.setOption(option);

}

function changeTagName(){
	var brandNameList = $("#brandNameList").val().split('|');
	var total = brandNameList.length;
	for(var index = 0; index < total; index++) {
		var name = "brand_"+(index+1);
	    $('span[name='+name+']').html("“"+brandNameList[index]+"”");
	}
	$("span[name='title_1']").html("卫视投放频次分布");
	$("span[name='title_2']").html("卫视投放时长分布");
	$("span[name='title_3']").html("电视剧投放频次分布");
	$("span[name='title_4']").html("电视剧投放时长分布");	
}