//模糊查询电视剧资源的 评价分析列表
var cur_name = "";
function getList(page){
	page = page || 1;
	//分页+传参+页面html
	var pageSize = 10, data = {
		"resourceName" : $("#resourceName").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
	//
	$("#analysisPage, #analysisList,#analysisCount").empty();
	ajax("ajax_resource_evaluation_analysis_list.do", data, function(res){
		if (res.code == 1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				html += '<tr data-name='+item.resourceName+'>'+
					'<td>'+item.resourceName+'</td>'+'<td>'+item.evaluationNumber+'</td>'+
					'<td>'+item.ip+'</td>'+'<td>'+item.theme+'</td>'+'<td>'+item.episode+'</td>'+
					'<td>'+item.schedule+'</td>'+'<td>'+item.platform+'</td>'+'<td>'+item.flow+'</td>'+
					'<td>'+item.actor+'</td>'+'<td>'+item.director+'</td>'+'<td>'+item.scriptwritter+'</td>'+
					'<td>'+item.produced+'</td>'+'<td>'+item.issue+'</td>'+'<td>'+item.similar+'</td>'+
					'<td>'+item.dissem+'</td>'+'<td>'+item.sumScore+'</td>'+
				'</tr>';
			});
			$("#analysisList").html(html);
			$.checkbox();
			$("#analysisCount").text(res.count);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#analysisPage").html(newpage.pageHtml);
			$("#analysisList>tr").on("click",function(){
				$(this).addClass("active").siblings().removeClass("active");
			});
			/**
			 * 点击事件
			 */
			$("#analysisList>tr>td:not(td:last-child)").on("click",function(){
				$(this).closest("tr").addClass("active").siblings().removeClass("active");
				detail($(this).closest("tr").data("name"));
			});
			$("#analysisList>tr:eq(0)>td:eq(1)").click();
		} else {
			detail("");
		}
	});
}
pageGo = getList;

//选择某一行 查询该电视剧的所有评价列表 和图表信息
function detail(resourceName){
	cur_name = resourceName;
	$("#evaluateListHeader").html("《"+resourceName+"》评价列表");
	$("#evaluateListChart").html("《"+resourceName+"》指标分析");
	
	//查询该电视剧的所有评价列表
	executeEvaluationList(resourceName);
	
	//图1 用户评价排行
	executeEvaluationRanking(resourceName);
	
	//图2 用户评价分歧
	executeEvaluationDivergence(resourceName);
	
	//图3 同类剧分组比较
	executeEvaluationSimilarPlayScoreCompare(resourceName);
	
	//图4 该剧在总分榜中的位置
	executeEvaluationListRanking(resourceName);
}

//查询该电视剧的所有评价列表
function executeEvaluationList(resourceName){
	var page = page || 1;
	//分页 参数 html
	var pageSize = 10,data = {
		"resourceName" : resourceName,
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
	//清空列表和page
	$("#evaluatePage, #evaluateList").empty();
	//得到数据并展示
	ajax("ajax_resource_evaluation_list.do", data, function(res){
		if (res.code == 1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				html += '<tr>'+
					'<td>'+item.crtName+'</td>'+'<td>'+item.ip+'</td>'+'<td>'+item.theme+'</td>'+
					'<td>'+item.episode+'</td>'+'<td>'+item.schedule+'</td>'+'<td>'+item.platform+'</td>'+
					'<td>'+item.flow+'</td>'+'<td>'+item.actor+'</td>'+'<td>'+item.director+'</td>'+
					'<td>'+item.scriptwritter+'</td>'+'<td>'+item.produced+'</td>'+'<td>'+item.issue+'</td>'+
					'<td>'+item.similar+'</td>'+'<td>'+item.dissem+'</td>'+'<td>'+item.sumScore+'</td>'+
				'</tr>';
			});
			$("#evaluateList").html(html);
			$.checkbox();
			$("#evaluateCount").text(res.count);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#evaluatePage").html(newpage.pageHtml);
		} else {
			$("#evaluateCount").text(0);
			var newpage = new createPage(0, page, pageSize, 3);
			$("#evaluatePage").html(newpage.pageHtml);
			$("#evaluateList").info("暂无数据!");
		}
	});
}

//图1 用户评价排行
function executeEvaluationRanking(resourceName){
	ajax("ajax_resource_evaluation_user_ratings_ranking.do",{"resourceName":resourceName},function(res){
		// 基于准备好的dom，初始化echarts实例
		var evaluationRanking = echarts.init(document.getElementById('evaluationRanking'));
		if (res.code == 1 && res.xAxis.length>0){
			var option = {
				color: ['#FF7F50'],
				/*title: {text: '评价分值排名'},*/
			    tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'}},
				legend: {data:['评价分值排名']},
			    xAxis: {name:'评价人',type: 'category',data: res.xAxis,
			    		axisPointer: {type: 'shadow'}},
			    yAxis: {name:'评价分',type: 'value',min: 0,max: 100},
			    series: [{name: '评价分值排名',type: 'bar',
				        label: {normal: {show: true,position: 'top'}},
				        data: res.yAxis}]
			};
			// 使用刚指定的配置项和数据显示图表。
			evaluationRanking.setOption(option);
		}else {
			console.log(res.msg);
			$("#evaluationRanking").info("暂无数据");
		}
	});
}

//图2 用户评价分歧
function executeEvaluationDivergence(resourceName){
	var divergenceCompare = echarts.init(document.getElementById('divergenceCompare'));
	ajax("ajax_resource_evaluation_user_evaluation_disagreements.do",{"resourceName":resourceName},function(res){
		if (res.code == 1 && res.yAxis.length>0){
			var option = {
				color: ['#FF7F50'],
				/*title: {text: '评价分歧比较'},*/
			    tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'}},
				legend: {data:['评价分歧比较']},
			    xAxis: {name:'类别',type: 'category',
			        	data: ['IP','题材','集数','档期','平台','流量预估','演员','导演','编剧','出品','发行','同期竞品','前期宣发'],
			        	axisLabel:{interval:0,rotate:-30},//横轴信息全部显示  -30度角倾斜显示  
			        	axisPointer: {type: 'shadow'}},
			    yAxis: {name:'标准差',type: 'value'},
			    series: [{name: '评价分歧比较',type: 'bar',
				        label: {normal: {show: true,position: 'top'}},
				        data: res.yAxis}]
			};
			// 使用刚指定的配置项和数据显示图表。
			divergenceCompare.setOption(option);
		}else {
			console.log(res.msg);
			$("#divergenceCompare").info("暂无数据");
		}
	});
}

//图3 同类剧分组比较
function executeEvaluationSimilarPlayScoreCompare(resourceName){
	var scoreCompare = echarts.init(document.getElementById('scoreCompare'));
	ajax("ajax_resource_evaluation_similar_score_value_comparison.do",{"resourceName":resourceName},function(res){
		if(res.code == 1 && res.xAxis.length>0){
			var option = {
					color: ['#FF7F50'],
					/*title: {text: '同类剧分值比较', },*/
				    tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'}},
				    legend: {data:['同类剧分值比较']},
				    xAxis: {name: '电视剧',type: 'category',data: res.xAxis},
				    yAxis: {name:'评价分',min: 0,max: 100,type: 'value'},
				    series: [{name: '同类剧分值比较',barWidth: '60%',data: res.yAxis,type: 'bar',
				    		//label表示显示数据在图上top位置
				        	label: {normal: {show: true,position: 'top'}}}]
				};
			scoreCompare.setOption(option);
		}else {
			console.log(res.msg);
			$("#scoreCompare").info("暂无数据");
		}
	});
}

//图4 该剧在总分榜中的位置
function executeEvaluationListRanking(resourceName){
	var scoreRanking = echarts.init(document.getElementById('scoreRanking'));
	ajax("ajax_resource_evaluation_play_list_score_ranking.do",{"resourceName":resourceName},function(res){
		var colors = [];
		if(res.code == 1 && res.list.length>0){
			var list = sortProgramRankList(res.list);
			var xdata = [],ydata = [];
			$.each(list, function(index, item){
				xdata.push(item.score);
				ydata.push(item.id + "-" + item.name);
				colors.push(item.name == cur_name ? '#3398DB' : '#FF7F50');
			});
			colors = colors.reverse();
			var option = {
					color: ['#FF7F50'],
					title: {text: '剧榜分值排行', },
				    tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'}},
				    grid: {left: '4%',right: '8%',bottom: '3%',containLabel: true},
				    xAxis: {name:'均分',type: 'value',min: 0,max: 100},
				    yAxis: {name:'电视剧',type: 'category',data: ydata.reverse()},
				    series: [{name: '剧榜分值排行',barWidth: '60%',data: xdata.reverse(),type: 'bar',
				    		label: {normal: {show: true,position: 'top'}},itemStyle: {   
				            normal:{color: function (params){var colorList = colors;
				                    return colorList[params.dataIndex];}},
				            emphasis: {shadowBlur: 10,shadowOffsetX: 0,shadowColor: 'rgba(0, 0, 0, 0.5)'}
				            }}]
				};
			scoreRanking.setOption(option);
		}else{
			console.log(res.msg);
			$("#scoreRanking").info("暂无数据");
		}
	});
}

/**
 * 电视剧均分排名
 * @param oldList 传入全部数组
 * @returns {Array} 返回排名数组
 */
function sortProgramRankList(oldList){
	var newList = new Array(),top = 0;
	for(var i = 0; i < oldList.length ; i++){
		if(oldList[i].name == cur_name){
			top = oldList[i].id;
			break;
		}
	}
	var start = (top - 9  > 0) ? top - 9 : 1;
	var end = (top + 10 > oldList.length) ? oldList.length : top + 10;
	if(end - start + 1 < 20){
		start = start - (20 - 1 - end + start);
	}
	
	for(var i = 0,j = 0;i < oldList.length; i++){
		if(oldList[i].id >= start && oldList[i].id <= end){
			newList[j] = oldList[i];
			j ++;
		}
	}
	return newList;
}