var areaOneVal = helper.cookies.get("areaOneVal");
var areaOneText = helper.cookies.get("areaOneText");
if (areaOneVal) {
	var areaOneVal = helper.cookies.get("areaOneVal");
	
} else{
	var areaOneVal="sqs12345"
	
}
$(function() {
	laydate.render({
		elem: '#time1',
		range: '~',
		format: 'yyyy-MM',
		type: 'month',
		theme: '#114a97',
		done: function(value, date) {
			var areaOneVal=$(".areaOne").val();
			partOne(value && value.split(' ~ ')[0], value && value.split(' ~ ')[1],areaOneVal)
			var start=value.split(' ~ ')[0];
			var end=value.split(' ~ ')[1];
		}
	});
	laydate.render({
		elem: '#time2',
		range: '~',
		format: 'yyyy-MM-dd',
		theme: '#114a97',
		done: function(value, date) {
			var areaOneVal=$(".areaOne").val();
			partTwo(value && value.split(' ~ ')[0], value && value.split(' ~ ')[1],areaOneVal)
		}
	});
	laydate.render({
		elem: '#time3',
		range: '~',
		theme: '#114a97',
		done: function(value, date) {
			var areaOneVal=$(".areaOne").val();
			partThree(value && value.split(' ~ ')[0], value && value.split(' ~ ')[1],areaOneVal)
		}
	});
	Ajax()
	$(".areaOne").change(function() {
      	var areaOneVal=$(this).val();
  		var areaOneText=$(".areaOne").find("option:selected").text();
  		helper.cookies.set("areaOneVal", areaOneVal, 7);
  		helper.cookies.set("areaOneText", areaOneText, 7);
      	partOne($('#time1').val() && $('#time1').val().split(' ~ ')[0], $('#time1').val() && $('#time1').val().split(' ~ ')[1],areaOneVal);
		partTwo($('#time2').val() && $('#time2').val().split(' ~ ')[0], $('#time2').val() && $('#time2').val().split(' ~ ')[1],areaOneVal);
		partThree($('#time3').val() && $('#time3').val().split(' ~ ')[0], $('#time3').val() && $('#time3').val().split(' ~ ')[1],areaOneVal);
//  	Ajax()
	});
	$(".keys").change(function() {
		var areaOneVal = helper.cookies.get("areaOneVal");
		partTwo($('#time2').val() && $('#time2').val().split(' ~ ')[0], $('#time2').val() && $('#time2').val().split(' ~ ')[1],areaOneVal);
	})
	
	keylist ();
	//部门下拉框
	function keylist () {
		$.getJSON( huayi.config.callcenter_url +"info/GetKeyList", function(result) {
			if(result.state.toLowerCase() == "success") {
				goodslist = result.data;
				$(".keys").empty();
				$('<option value="">请选择关键词</option>').appendTo($(".keys"));
				$(goodslist).each(function(i, n) {
					$('<option value="' + n.F_ValueId 
					+ '">' + n.F_Value 
					+ '</option>').appendTo($(".keys"));
				})
			}
		});
	}
})

//区县筛选
deprtment ();
function deprtment () {
	$.getJSON( huayi.config.callcenter_url +"CountyBranch/GetBranchListS", function(result) {
		if(result.state.toLowerCase() == "success") {
			goodslist = result.data;
//			第五屏
			$(".areaOne").empty();
			$(goodslist).each(function(i, n) {
				$('<option value="' + n.F_Code 
				+ '">' + n.F_Name 
				+ '</option>').appendTo($(".areaOne"));
			});
			if (areaOneVal) {
				$(".areaOne").val(areaOneVal);
			}
		}
	});
}
//part 1 //投诉类别月统计  对接完毕
var part_1 = echarts.init(document.getElementById('part_1'));
part_1.setOption({
	color: ['#69e1a9', '#6a91e0', '#ceba5f', '#f06e84', '#569d4e', '#7d55ba', '#ce5aba', '#345cdd', '#569d4e'],
	tooltip: {
		trigger: 'axis',
		axisPointer: {
			type: 'cross',
			label: {
				show: true,
				backgroundColor: '#333'
			}
		}
	},
	grid: {
		left: '2%',
		right: '5%',
		bottom: '6%',
		containLabel: true
	},
	legend: {
		top: 'top',
		left: '180px',
		data: [],
		textStyle: {
			color: '#00e9ff'
		}
	},
	xAxis: {
		name: '月份',
		data: [],
		axisLine: {
			lineStyle: {
				color: '#3061a2'
			}
		},
		axisTick: {
			alignWithLabel: true,
			show: false
		},
		axisLabel: { //横轴字体颜色
			show: true,
			textStyle: {
				color: '#eff0f4'
			}
		}
	},
	yAxis: {
		name: '（数量）',
		splitLine: {
			show: false
		},
		axisLine: {
			lineStyle: {
				color: '#3061a2'
			}
		},
		axisLabel: { //横轴字体颜色
			show: true,
			textStyle: {
				color: '#eff0f4'
			}
		}
	},
	series: []
});

function partOne(start, end,areaOneVal) {
	//		var index = layer.load(1, {
	//			shade: [0.5, '#030303'] //0.1透明度的白色背景
	//		});
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "info/GetTypeCountMonthByDate",
		async: true,
		dataType: 'json',
		data: {
			start: start,
			end: end,
			branchcode:areaOneVal
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				//							layer.close(index);
				var con = data.data;
				var counts = con.counts;
				var sers = [];
				$(counts).each(function(i, n) {
					var obj = {};
					obj.name = con.cols[i];
					obj.type = "line";
					obj.smooth = true;
					obj.showAllSymbol = true;
					obj.symbol = "emptyCircle";
					obj.symbolSize = 10;
					obj.data = n;
					sers.push(obj)
				});
				part_1.setOption({
					xAxis: {
						data: con.months
					},
					legend: {
						data: con.cols
					},
					series: sers
				});

			}
		}
	});
}
//part2 //投诉关键字统计

function partTwo(starts, ends, areaOneVal) {
	var keyid=$('.keys').val()
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Info/GetKeyCountByDateNew",
		dataType: 'json',
		async: true,
		data: {
			start: starts,
			end: ends,
			keyid: keyid,
			branchcode:areaOneVal
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				$('.thead tr').html('');
				$('#scroll_table tbody').html('');
				var con = data.data;
				var thead = con[0];
				$.each(thead, function(k, val) {
					$('<td>' + k + '</td>').appendTo('.thead tr');
				})
				$(con).each(function(j, n) {
					var str = '<tr>'
					$.each(thead, function(j, m) {
						$.each(n, function(k, val) {
							if(j == k) {
								if(val == '') {
									val = 0;

								}
								str += '<td>' + val + '</td>'
							}
						})
					})
					str += '</tr>';

					$('#scroll_table tbody').append(str);
				})

			}
		}
	});
}
// part3 投诉分类统计
var part3 = echarts.init(document.getElementById('part3'));
part3.setOption({
	color: ['#6ce7ac', '#6a91e0', '#ceba5f', '#cb5f79', '#ae765a', '#569d4e'],
	tooltip: {
		trigger: 'item',
		formatter: "{a} <br/>{b} : {c} ({d}%)",
		axisPointer: {
			crossStyle: {
				color: '#fff'
			}
		}
	},
	//			legend: {
	//				//      orient: 'vertical',
	//				bottom: 'bottom',
	//				data: [],
	//				textStyle: {
	//					color: '#fff'
	//				}
	//			},
	series: [{
		name: '数据',
		type: 'pie',
		radius: ['30%', '60%'],
		center: ['40%', '50%'],
		data: [],
		itemStyle: {
			emphasis: {
				shadowBlur: 10,
				shadowOffsetX: 0,
				shadowColor: 'rgba(0, 0, 0, 0.5)'
			},
			normal: {
				label: {
					show: true,
					//                                 formatter: '{b} : {c} ({d}%)' 
					formatter: '{d}%'
				},
				labelLine: {
					show: true
				}
			}
		},
		label: {
			normal: {
				textStyle: {
					color: '#fff'
				}
			}
		}
	}]
});

function partThree(start, end,areaOneVal) {
	//		var index = layer.load(1, {
	//			shade: [0.5, '#030303'] //0.1透明度的白色背景
	//		});
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "info/GetTypeCountByDate",
		async: true,
		dataType: 'json',
		data: {
			start: start,
			end: end,
			branchcode:areaOneVal
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				//						layer.close(index);
				$('.complaint_type').html('');
				var con = data.data;
				var part_1_legend = [];
				var part_1_ser = [];
				$(con).each(function(i, n) {
					part_1_legend.push(n.TypeName)
					var part_1_obj = {};
					part_1_obj.value = n.Count;
					part_1_obj.name = n.TypeName;
					part_1_ser.push(part_1_obj)
					$('<li>' +
						'<span class="type_name">' + n.TypeName + '</span>   <span class="type_count">' + n.Count + '</span>' +
						'</li>').appendTo('.complaint_type')
				})
				part3.setOption({
					//							legend: {
					//								data: part_1_legend
					//							},
					series: [{
						data: part_1_ser
					}]
				})

			}
		}
	});
}

//表格滚动
var area = document.getElementById('scroll_table');
var iliHeight = 34; //单行滚动的高度  
var speed = 20; //滚动的速度  
var time;
var delay = 1000;
area.scrollTop = 0;
area.innerHTML += area.innerHTML; //克隆一份一样的内容  
function startScroll() {
	time = setInterval("scrollUp()", speed);
	area.scrollTop++;
	console.log(area.scrollTop);
}

function scrollUp() {
	if(area.scrollTop % iliHeight == 0) {
		clearInterval(time);
		setTimeout(startScroll, delay);
	} else {
		area.scrollTop++;
		if(area.scrollTop >= area.scrollHeight / 2) {
			area.scrollTop = 0;
		}
	}
}
setTimeout(startScroll, delay)

function Ajax() {
	var areaOneVal = helper.cookies.get("areaOneVal");
	var areaOneText = helper.cookies.get("areaOneText");
	partOne($('#time1').val() && $('#time1').val().split(' ~ ')[0], $('#time1').val() && $('#time1').val().split(' ~ ')[1],areaOneVal)
	partTwo($('#time2').val() && $('#time2').val().split(' ~ ')[0], $('#time2').val() && $('#time2').val().split(' ~ ')[1],areaOneVal);
	partThree($('#time3').val() && $('#time3').val().split(' ~ ')[0], $('#time3').val() && $('#time3').val().split(' ~ ')[1],areaOneVal)
}