/**
 * 每日呼叫数据统计
 * */
var dayCallTotal; //图形
var tabIndex = 0;
$(function() {
	$('#sc_time').val(helper.DateFormat.getNowDate());
	$('.now_day').text(helper.DateFormat.getNowDate());
	//初始化时间
	layui.use('laydate', function() {
		var laydate = layui.laydate;
		laydate.render({
			elem: '#sc_time',
			//range: '~',
			theme: '#1ab394',
			calendar: true,
			done: function(value, date, endDate) {
				$('.now_day').text(value);
				//console.log(value); //得到日期生成的值，如：2017-08-18
				//console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
				//console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
			}
		});
	});
	//tab切换
	$('.hu-tab li').click(function() {
		$(this).addClass('active')
			.siblings().removeClass('active');

		tabIndex = $(this).index();
		$('.hu-content >div').eq(tabIndex).show()
			.siblings().hide();
		loadDatas();
	});
	//初始化图形
	initChart();
	//加载数据
	loadDatas();
	//搜索
	$("#sc_btns").click(function() {
		loadDatas();
	})
});

//初始化图形
function initChart() {
	dayCallTotal = echarts.init(document.getElementById('dayCallTotal'));
	dayCallTotal.clear();
	dayCallTotal.showLoading();
	option = {
		tooltip: {
			trigger: 'axis',
		},
		legend: {
			data: ["呼入量",
				"呼出量",
				"接通量",
				"未接通量",
				"接通率"
			],
			bottom: 0
		},
		xAxis: [{
			name: '时间',
			type: 'category',
			data: (function() {
				var arr = [];
				for(var i = 1; i < 25; i++) {
					arr.push(i+'点');
				}
				return arr;
			})(),
			axisPointer: {
				type: ''
			}
		}],
		yAxis: [{
			type: 'value',
			name: '电话数量',
			nameLocation: 'end',
			nameGap: 40,
			axisLabel: {
				formatter: '{value} '
			}
		}, {
			type: 'value',
			name: '百分比',
			nameLocation: 'end',
			nameGap: 35,
			interval: 10,
			axisLabel: {
				formatter: '{value} '
			}
		}],
		series: [{
			name: '呼入量',
			type: 'bar',
			data: []
		}, {
			name: '呼出量',
			type: 'bar',
			data: []
		}, {
			name: '接通量',
			type: 'bar',
			data: []
		}, {
			name: '未接通量',
			type: 'bar',
			data: []
		}, {
			name: '接通率',
			type: 'line',
			yAxisIndex: 1,
			data: []
		}],
		color: ['#1ab394', '#adcbfd', '#fbbe5b', '#f26ea2', '#88ebc4']
	};
	dayCallTotal.setOption(option);
}

//获取图形数据
function getDataList() {
	$.ajax({
		type: 'get',
		url: huayi.config.callcenter_url + 'HourCallTotal/GetDataList',
		dataType: 'json',
		beforeSend: function() {
			loadindex = layer.load()
		},
		data: {
			stime: $('#sc_time').val(), //	否	string	时间 (2018-07-03)
			token: $.cookie("token"),
		},
		async: true,
		success: function(res) {
			dayCallTotal.hideLoading();
			var content = res.data;
			if(content && content.length > 0) {
				var arr1 = []; //呼入量
				var arr2 = []; //呼出量
				var arr3 = []; //接通量
				var arr4 = []; //未接通量
				var arr5 = []; //接通率
				for(var i = 0; i < content.length - 1; i++) {
					$.each(content[i], function(j, v) {
						switch(j) {
							case "呼入量":
								arr1.push(v);
								break;
							case "呼出量":
								arr2.push(v);
								break;
							case "接通量":
								arr3.push(v);
								break;
							case "未接通量":
								arr4.push(v);
								break;
							case "接通率":
								arr5.push(v.substring(0, 4));
								break;
							default:
								break;
						}
					});
				}
				dayCallTotal.setOption({
					series: [{
						name: '呼入量',
						type: 'bar',
						data: arr1
					}, {
						name: '呼出量',
						type: 'bar',
						data: arr2
					}, {
						name: '接通量',
						type: 'bar',
						data: arr3
					}, {
						name: '未接通量',
						type: 'bar',
						data: arr4
					}, {
						name: '接通率',
						type: 'line',
						yAxisIndex: 1,
						data: arr5
					}]
				});
			}
		}
	}).then(function() {
		layer.close(loadindex);
	});
}

//获取表格数据
function getTableDataList() {
	var colsArr = [];
	var loadindex;

	$.ajax({
		type: 'get',
		url: huayi.config.callcenter_url + "HourCallTotal/GetDataList",
		async: true,
		dataType: 'json',
		beforeSend: function() {
			loadindex = layer.load()
		},
		data: {
			stime: $('#sc_time').val(), //	否	string	时间 (2018-07-03)
			token: $.cookie("token"),
		},
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var res = data.data;
				var colsArr1 = [];
				if(res && res.length > 0) {
					colsArr1.push({
						field: '时间',
						title: '时间(点)',
						align: 'center',
						fixed: true,
						//						templet: function(d) {
						//							//return '<span>'+ (d.时间-1) +'点  ~ '+ d.时间 +'点</span>'
						//						},
						//sort: true,
						width: 150,
					});

					for(var i in res[0]) {
						if(i === "时间" || i === "接通率") {
							continue;
						} else {
							colsArr1.push({
								field: i,
								title: i,
								align: 'center',
							});
						}
					}
					colsArr1.push({
						field: '接通率',
						title: '接通率',
						align: 'center',
						fixed: 'right',
						//sort: true,
						templet: function(d) {
							return '<span class="color_73926">' + d.接通率 + '</span>'
						},
						width: 100,
					});
					colsArr.push(colsArr1);
					layui.use('table', function() {
						var table = layui.table;
						//方法级渲染
						table.render({
							elem: '#t_callTotal',
							skin: 'row', //line （行边框风格） row （列边框风格） nob （无边框风格）
							even: true, //开启隔行背景
							size: 'md', //sm,lg,md尺寸的表格
							cellMinWidth: 160,
							cols: colsArr,
							data: res,
							limit: res.length, //默认是10
							//height: 'full-150'
						});
					})

				}
			}
		},
	}).then(function() {
		layer.close(loadindex);
	});
}

//格式化序号
function setCode(val, row, index) {
	return ++index;
}
//导出
function dcexcel(obj) {
	var url = huayi.config.callcenter_url + "HourCallTotal/ExportExcel?token=" + $.cookie("token");
	url += "&stime=" + $('#sc_time').val(); //否	string	时间
	obj.href = url;
}

function loadDatas() {
	if(tabIndex == 0) {
		getDataList(); //加载图形
	} else if(tabIndex == 1) {
		getTableDataList(); //加载表格
	}
}