var initLoad = function(){
	window.global = {};
	initVUE();
	changeBehaviourTime($("#behaviourTimeSelect")[0].value);
	initDoAndRepoCount();
};

var initVUE = function () {
	toastVue = new Vue({
		el: "#alertDiv",
		data: {
			toastTitle: "提示",
			toastMessage: "消息"
		}
	})
}

var myToast = function (title, msg) {
	toastVue.toastTitle = title;
	toastVue.toastMessage = msg;
	$("#alertDiv").toast("show");
}

var jumpToQuery = function(){
	var ss = window.location.href;
	var s = ss.split(":");
	console.log(s);
	var p = s[2].split("/")[0];
	var port = parseInt(p);
	var url = s[0] + ":" + s[1] + ":" + port + "/queryIdentifier.html";
	window.open(url);
};

var queryIdentifierInit = function () {
	window.global = {};
	initVUE();
	analysis();
};

var analysisInit = function(){
	console.log("analysisInit");
	var str = "";
	if($("#analysisInput")[0].value == undefined || $("#analysisInput")[0].value == ""){
		myToast("注意","请输入标识解析!");
	}else {
		str = $("#analysisInput")[0].value;
		var ss = window.location.href;
		var s = ss.split(":");
		console.log(s);
		var p = s[2].split("/")[0];
		var port = parseInt(p);
		var url = s[0] + ":" + s[1] + ":" + port + "/queryIdentifier.html?id=" + str;
		window.location.href = url;
		window.open(url);
	}
};

var analysis = function(){
	console.log("analysis");
	var ss = window.location.href;
	var s = ss.split("?");
	var s2 = s[s.length - 1].split("=");
	var identifier = s2[1];
	global.identifier = identifier;
	var str = getUrl() + "/resolve?identifier=" + identifier;
	console.log(str);

	$.ajax({
		url : str,
		type: "GET",
		dataType : "json"
	}).done(function(result) {
		showAnalysisResult(result);
	});
};

var dtLangHandleRecord = {
	"sProcessing" : "处理中...",
	"lengthMenu" : '显示 <select>' + '<option value="10">10</option>'
		+ '<option value="20">20</option>'
		+ '<option value="100">50</option>'
		+ '<option value="-1">全部</option>' + '</select> 项结果',
	"sZeroRecords" : "没有匹配结果",
	"sInfo" : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
	"sInfoEmpty" : "显示第 0 至 0 项结果，共 0 项",
	"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
	"sInfoPostFix" : "",
	"sSearch" : "搜索:",
	"sUrl" : "",
	"sEmptyTable" : "表中数据为空",
	"sLoadingRecords" : "载入中...",
	"sInfoThousands" : ",",
	"oPaginate" : {
		"sFirst" : "首页",
		"sPrevious" : "上页",
		"sNext" : "下页",
		"sLast" : "末页"
	},
	"oAria" : {
		"sSortAscending" : ": 以升序排列此列",
		"sSortDescending" : ": 以降序排列此列"
	}
};

var showAnalysisResult = function(result){
	console.log(result);

	if(result.hasOwnProperty("response") && result["response"] == "not exist"){
		var content = "未查询到标识 " + global.identifier + " !";
		var html1 = "<div class=\"card-title\" style=\"margin-bottom: 30px\">\n" +
			"<h5 class=\"card-title\" style=\"float:left\">解析结果</h5>\n" +
			"</div>\n" +
			"<div class=\"card-body\">\n" +
			"<div class=\"d-flex justify-content-center\">\n" +
			"<h6 class=\"card-title\" style=\"float:left;font-weight: bold\">" + content + "</h6>\n" +
			"</div>\n" +
			"</div>";
		$("#handleRecordBody").html(html1);
		return;
	}

	var html1 = "<div class=\"card-title\" style=\"margin-bottom: 30px\">\n" +
		"<h5 class=\"card-title\" style=\"float:left\">解析结果</h5>\n" +
		"</div>\n" +
		"<div class=\"card-body\" id=\"HandleRecordDiv\">\n" +
		"<div class=\"d-flex justify-content-center\">\n" +
		"<div class=\"spinner-border text-primary\"\n" +
		"style=\"width: 4rem; height: 4rem;\" role=\"status\">\n" +
		"<span class=\"sr-only\">Loading...</span>\n" +
		"</div>\n" +
		"</div>\n" +
		"</div>";

	//record 结果
	var tableHead = "<table id='HandleRecordDivTab' class='table display row-border'><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>";
	var tableTail = "</tbody></table>";
	var html = tableHead;
	for(var k in result){
		html += "<tr><td>";
		html += k;
		html += ("</td><td>");
		html += result[k];
		html += "</td></tr>";
	}
	html += tableTail;

	$("#handleRecordBody").html(html1);
	$("#HandleRecordDiv").html(html);
	$("#HandleRecordDivTab").DataTable(
		{
			"language" : dtLangHandleRecord,
			"rowCallback" : function(row, data, displayNum, displayIndex,
									 dataIndex) {

			},
			"headerCallback" : function(thead, data, start, end, display) {
				$(thead).css("background", primaryColor);
				$(thead).css("color", "white");
				$(thead).children().css("background", primaryColor);
				$(thead).children().css("color", "white");
			},
			scrollCollapse : false,
			paging : true,
			pageLength : 10,
			order : [ [ 0, 'desc' ] ],
			"columnDefs" : [
				{
					"targets" : 1,
					"render" : function(data, type, row, meta) {
						if (data.length > 35)
							return "<span title='" + data + "'>"
								+ data.substring(0, 35)
								+ "  ...</span>";
						else
							return data;
					}
				},
			]
		});
};