var m_OperMenuJson = {};//当前查询数据缓存
var m_strFirstLoad = 0;// isfirst=0代表初始化调用
var m_strSelectReportType = 1;//1登陆，2转移，3档案，4机器人，5设置
//var m_strRedPacket = [];  //登陆

// 好友数量统计页面加载
$(function () {
	$(".loadingcover").hide();
	$(".loading").hide();

	$("#myPageAlert").hide();
	listcss();
	//获取部门
	var paraJson = $.toJSON({
		type: "data"
	});
	dataObj.getdeptbyauth(paraJson);
});

function AsyncGetDeptByAuthResult(jsonResult) {
	if (jsonResult.success) {
		var deptlist = jsonResult.deptlist;
		$("#selDept").append("<option value='" + jsonResult.myself.userid + "' tag='user' name='" + jsonResult.myself.username + "'>本人</option>");
		if (jsonResult.deptlist.length > 0) {
			for (var index in deptlist) {
				var item = deptlist[index];
				$("#selDept").append("<option value='" + item.dgid + "'>" + item.dgname + "</option>");
			}
		} else {
			$("#selDept").hide();
		}
		BindCompanyUsers();
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}
	//初始化调用
	if (m_strFirstLoad == 0) {
		BindData();
		m_strFirstLoad = 1;
	}
}

function BindCompanyUsers() {
	nPageIndex = 0;
	var paraJson;
	$("#selUser").empty();
	alluserid = "";
	var selectDom = $("#selDept").find("option:selected");
	if (selectDom.attr("tag") == "user") {
		$("#selUser").append("<option value='" + selectDom.val() + "' tag='user' selected='true'>" + selectDom.attr("name") + "</option>");
		alluserid = selectDom.val();
	} else {
		$("#selUser").append("<option value=''>客服</option>");
		paraJson = $.toJSON({
			deptid: $("#selDept").val()
		});
		dataObj.getpersonbydeptid(paraJson);
	}
}

//绑定人员回调
function AsyncGetPersonByDeptidResult(jsonResult) {
	var selUser = $("#selUser");
	if (jsonResult.success) {
		var jsonObj = jsonResult.user;
		var jsonList = jsonObj.list;
		var currentId = jsonObj.loginuid;
		alluserid = "";
		for (var i = 0; i < jsonList.length; i++) {
			var item = jsonList[i];
			alluserid += item.userid + ",";
			selUser.append("<option value='" + item.userid + "'>" + item.name + "</option>");
		}
		alluserid = alluserid.substr(0, alluserid.length - 1);
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}
}


//数据查询
function BindData() {
	//验证
	var myDate = new Date();//当前日期
	var taday = myDate.getTime();
	var time1 = Date.parse(new Date("20" + $("#txtTimeFrom").val()));
	var time2 = Date.parse(new Date("20" + $("#txtTimeTo").val()));
	var diff = parseInt((time2 - time1) / 1000 / 3600 / 24);
	var SerchType = 0;//查询类型
	if (diff < 0) {
		ShowError("myPageAlert", "起始时间不能大于终止时间");
		return;
	} else if (time1 > taday || time2 > taday) {
		ShowError("myPageAlert", "选择的时间不能大于今天!");
		return;
	}

	var date1 = ($("#txtTimeFrom").val()).split("-");
	var date2 = ($("#txtTimeTo").val()).split("-");
	if (date1[1] != date2[1]) {
		ShowError("myPageAlert", "选择的时间不能跨月份！");
		return;
	}


	if ($("#selUser").val() == "") {
		ShowError("myPageAlert", "请选择客服人员！");
		return;
	}

	$(".loadingcover").show();
	$(".loading").show();

	name = $("#selUser").find("option:selected").html();
	$("#thName").html("客服名称");

	var uid = $("#selDept").val();

	
	if (m_strSelectReportType == 1) {
	    //登陆
		SerchType = 0;
	} else if (m_strSelectReportType == 2) {
	    //转移
		SerchType = 2;
	} else if (m_strSelectReportType == 3) {
	    //档案
		SerchType = 1;
	} else if (m_strSelectReportType == 4) {
	    //微信登陆/登出日志
	    SerchType = 3;
	}
		
	var paraJson = $.toJSON({
	    userid: $("#selUser").val(),
	    pageindex: nPageIndex,
	    pagesize: nPageSize,
	    dtstart: $("#txtTimeFrom").val(),
	    dtend: $("#txtTimeTo").val(),
	    type: SerchType
	});
	dataObj.getoperationlog(paraJson);

}

//操作日志回调
function AsyncGetOperationLogResult(jsonResult) {
	//m_OperMenuJson = {};
	if (jsonResult.success) {
		//m_OperMenuJson = jsonResult;
	    if (jsonResult.result.list != null && jsonResult.result.list.length > 0) {
			if (m_strSelectReportType == 1) {
				//登陆
				    SetFansShow(jsonResult);
					$("#tableLog").show();
			} else if (m_strSelectReportType == 3) {
				//档案
				    RecordChange(jsonResult);
					$("#tableRecord").show();
			} else if (m_strSelectReportType == 2) {
			    //转移
			        wxtransfer(jsonResult);
			        $("#transfer").show();
			} else if (m_strSelectReportType == 4) {
			    //微信登陆日志
			        weixinLog(jsonResult);
			        $("#weixinLog").show();
			}
			nPageCount = Math.ceil(jsonResult.result.total / nPageSize);
			$("#DivFooter").html(GetPageFootStr(nPageIndex, jsonResult.result.total));
			$("#DivFooter").show();
		} else {
			$("#DivFooter").hide();
			$("#tableLog").hide();
			$("#tableRecord").hide();
			$("#transfer").hide();
			$("#weixinLog").hide();
			$("#TbLogRecord").empty();
			$("#TbMessageChenge").empty();
			$("#TdTransfer").empty();
			$("#td_weixinLog").empty();
			$("#hint").show();
			$("#tips").show();
			$("#transferTips").show();
			$("#weixinLogTips").show();
		}
		$(".loadingcover").hide();
		$(".loading").hide();
	}
}


//登陆日志页面Html
function SetFansShow(json) {
	var strConrent = "";
	for (var i = 0; i < json.result.list.length; i++) {
		strConrent += "<tr>"
		strConrent += "<td>" + (parseInt(i) + 1) + "</td>";
		strConrent += "<td>" + json.result.list[i].username + "</td>";
		strConrent += "<td style=\"word-break:break-all\">" + json.result.list[i].remark + "</td>";
		strConrent += "<td>" + json.result.list[i].createtime + "</td>";
		strConrent += "</tr>";
	}
	$("#TbLogRecord").html(strConrent);
	$("#tips").hide();
	tableSlipTbLogin();//表头拖动
}


//档案变更
function RecordChange(json) {
	var strConrent = "";
	for (var i = 0; i < json.result.list.length; i++) {
		strConrent += "<tr>"
		strConrent += "<td>" + (parseInt(i) + 1) + "</td>";
		strConrent += "<td>" + json.result.list[i].username + "</td>";
		strConrent += "<td style=\"word-break:break-all\">" + json.result.list[i].remark + "</td>";
		strConrent += "<td>" + json.result.list[i].createtime + "</td>";
		strConrent += "</tr>";
	}
	$("#TbMessageChenge").html(strConrent);
	$("#hint").hide();
	tableSlip();//表头拖动
}

//微信号转移页面
function wxtransfer(json) {
    var strConrent = "";
    for (var i = 0; i < json.result.list.length; i++) {
        strConrent += "<tr>"
        strConrent += "<td>" + (parseInt(i) + 1) + "</td>";
        strConrent += "<td>" + json.result.list[i].username + "</td>";
        strConrent += "<td style=\"word-break:break-all\">" + json.result.list[i].remark + "</td>";
        strConrent += "<td>" + json.result.list[i].createtime + "</td>";
        strConrent += "</tr>";
    }
    $("#TdTransfer").html(strConrent);
    $("#transferTips").hide();
    tableSlipTbLogin();//表头拖动
}


//微信登陆登出日志
function weixinLog(json) {
    var strConrent = "";
    for (var i = 0; i < json.result.list.length; i++) {
        strConrent += "<tr>"
        strConrent += "<td>" + (parseInt(i) + 1) + "</td>";
        strConrent += "<td>" + json.result.list[i].username + "</td>";
        strConrent += "<td style=\"word-break:break-all\">" + json.result.list[i].remark + "</td>";
        strConrent += "<td>" + json.result.list[i].createtime + "</td>";
        strConrent += "</tr>";
    }
    $("#td_weixinLog").html(strConrent);
    $("#weixinLogTips").hide();
    tableSlipTbLogin();//表头拖动
}

//选择表格
function operationLogclick(index) {
	m_strSelectReportType = index;
	nPageIndex = 0;
	BindData();
}

//表头拖拽
function tableSlipTbLogin() {
    var tTD; //用来存储当前更改宽度的Table Cell,避免快速移动鼠标的问题
    if (m_strSelectReportType == 2) {
        var table = document.getElementById("tableTransfer");
    } else if (m_strSelectReportType == 1) {
        var table = document.getElementById("TbLogin");
    } else if (m_strSelectReportType == 4) {
        var table = document.getElementById("tbWeixinLog");
    }
	if (table.rows[0] == undefined) {
		return;
	}
	for (j = 0; j < table.rows[0].cells.length; j++) {
		table.rows[0].cells[j].onmousedown = function () {
			//记录单元格
			tTD = this;
			if (event.offsetX > tTD.offsetWidth - 10) {
				tTD.mouseDown = true;
				tTD.oldX = event.x;
				tTD.oldWidth = tTD.offsetWidth;
			}
			//记录Table宽度
			//table = tTD; while (table.tagName != ‘TABLE') table = table.parentElement;
			//tTD.tableWidth = table.offsetWidth;
		};
		table.rows[0].cells[j].onmouseup = function () {
			//结束宽度调整
			if (tTD == undefined) tTD = this;
			tTD.mouseDown = false;
			tTD.style.cursor = 'default';
		};
		table.rows[0].cells[j].onmousemove = function () {
			//更改鼠标样式
			if (event.offsetX > this.offsetWidth - 10)
				this.style.cursor = 'col-resize';
			else
				this.style.cursor = 'default';
			//取出暂存的Table Cell
			if (tTD == undefined) tTD = this;
			//调整宽度
			if (tTD.mouseDown != null && tTD.mouseDown == true) {
				tTD.style.cursor = 'default';
				if (tTD.oldWidth + (event.x - tTD.oldX) > 0)
					tTD.width = tTD.oldWidth + (event.x - tTD.oldX);
				//调整列宽
				tTD.style.width = tTD.width;
				tTD.style.cursor = 'col-resize';
				//调整该列中的每个Cell
				table = tTD; while (table.tagName != 'TABLE') table = table.parentElement;
				for (j = 0; j < table.rows.length; j++) {
					table.rows[j].cells[tTD.cellIndex].width = tTD.width;
				}
				//调整整个表
				//table.width = tTD.tableWidth + (tTD.offsetWidth – tTD.oldWidth);
				//table.style.width = table.width;
			}
		};
	}
}


//样式操作
function listcss() {
	$("#right").css({
		height: $(window).height()
	});
	$("#left").css({
		height: $(window).height()
	});
	$(".left-list-l").css({
		height: $(window).height()
	});
	$(".friends-table").css({
		height: $(window).height() - 180
	});
}

//初始化控件
$(document).ready(function () {
	$('#txtTimeFrom').datetimepicker({
		language: "zh-CN",
		weekStart: 1,
		todayBtn: 1,
		autoclose: true,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
		showMeridian: 1,
		minView: "month",
		format: "yy-m-d"
	});
	$('#txtTimeTo').datetimepicker({
		language: "zh-CN",
		weekStart: 1,
		todayBtn: 1,
		autoclose: true,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
		showMeridian: 1,
		minView: "month",
		format: "yy-m-d"
	});

	//获取今天日期
	var myDate = new Date();
	$('#txtTimeTo').val((myDate.getYear() - 100) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate());
	var endMonth = myDate.getMonth() + 1;
	var endYear = myDate.getYear() - 100;
	$('#txtTimeTo').datetimepicker('update');
	//设置日期，当前日期的前七天
	myDate.setDate(myDate.getDate() - 7);
	var startMonth = myDate.getMonth() + 1;
	var startYear = myDate.getYear() - 100;
	if (endYear != startYear) {
	    startYear = endYear;
	}

    //判断是否跨月
	if (startMonth != endMonth) {
	    $('#txtTimeFrom').val(endYear + "-" + endMonth + "-1");
	} else {
	    $('#txtTimeFrom').val(endYear + "-" + startMonth + "-" + myDate.getDate());
	}
	$('#txtTimeFrom').datetimepicker('update');
});

$(window).resize(function () {
	listcss();
});