<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>seo analysis</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
	<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
	<style>
	th {
		cursor: pointer;
	}
	td{
		word-break:break-word;
	}
	</style>
</head>
<body>
	<div class="container">
		<div class="page-header">
			<h1>python-seo-analyzer results</h1>
		</div>

		{%if result['fatal_error']%}
		<p>There was a fatal error when trying to format the output file as a webpage. Confirm that there is an output.json file in the directory. If not, you can reference the 
			<a href="https://github.com/sethblack/python-seo-analyzer">python-seo-analyzer documentation</a></p>
			{% else %}
			<p>total time: {{result['total_time']|round(2)}} seconds </p>
			<div>
				<h2>go to:</h2>
				<ul>
					<li><a href="#page-section">page analysis</a></li>
					<li><a href="#keyword-section">keyword analysis</a></li>
					<li><a href="https://github.com/sethblack/python-seo-analyzer">GitHub</a></li>
				</ul>
			</div>

			{% if result['errors']%}
			<h2>errors:</h2>
			<ul>
				{% for e in result['errors'] %}
				<li>{{e}}</li>
				{% endfor %}
			</ul>
			{% endif %}

			{% if result['pages'] %}
			<h2 id="page-section">page analysis:</h2>
			<input type="button" value="hide all notices " onclick="toggleDetail()" class="btn btn-info" id="ebutton">

			<div class="table-responsive">
			<table id="pages" class="table table-striped table-hover">
				<thead>
					<tr>
						<th></th>
						<th>page</th>
						<th>word count</th>
						<th>number of notices</th>
					</tr>
				</thead>

				<tbody>
				{% for page in result['pages']%}
				{% set outer_loop = loop %}
				<tr onclick="fullerrors({{ outer_loop.index }})">
					<td><i class="material-icons" id="i{{ outer_loop.index }}">keyboard_arrow_down</i></td>
					<td><a href="{{ page['url'] }}" target ="_blank">{{page['url']}}</a></td>
					<td>{{page['word_count']}}</td>
					<td>{{page["warnings"]|length}}</td>
				</tr>
				<tr id="{{ outer_loop.index }}" class="error-detail">
					<td colspan="4"><p>{{page["title"]}}</p><p>{{page["description"]}}</p><ul>{% for err in page["warnings"] %}<li>{{err|e}}</li>{% endfor %}</ul></td>
				</tr>
				{% endfor %}
			</tbody>
			</table>
			</div>

			{% endif %}


			{% if result['keywords'] %}
			<h2 id="keyword-section">keyword analysis:</h2>
			<div class="table-responsive">
			<table id = "keywords" class="table table-striped">
				<thead>
					<tr>
						<th class="sortable_th">keywords</th>
						<th class="sortable_th">count</th>
					</tr>
				</thead>
				<tbody>
				{% for key in result['keywords']%}
				<tr>
					<td> {{ key['word'] }} </td>
					<td> {{ key['count'] }} </td>
				</tr>

				{% endfor %}
			</tbody>
			</table>
		</div>
			{% endif %}
			{% endif %}
		</div>

		<script type='text/javascript'>
		var showall = true;
		function toggleDetail(){
			if(showall){
				$(".error-detail").hide();
				$(".material-icons").text('keyboard_arrow_right');
				$("#ebutton").val('show all notices');
				showall = false;
			}else{
				$(".error-detail").show();
				$(".material-icons").text('keyboard_arrow_down');
				$("#ebutton").val('hide all notices ');
				showall = true;
			}
		}
		function fullerrors(errorid){
			var cellid = "#" + errorid;
			var pressedid = "#i" + errorid;
			$(cellid).toggle();

			if($(pressedid).html() == 'keyboard_arrow_down'){
				$(pressedid).text('keyboard_arrow_right');
			}else{
				$(pressedid).text('keyboard_arrow_down');
			}
		}
		// https://stackoverflow.com/questions/3160277/jquery-table-sort
		$(document).ready(function(){
			$('.sortable_th').click(function(){
				var table = $(this).parents('table').eq(0)
				var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
				this.asc = !this.asc
				if (!this.asc){rows = rows.reverse()}
					for (var i = 0; i < rows.length; i++){table.append(rows[i])}
				})
			function comparer(index) {
				return function(a, b) {
					var valA = getCellValue(a, index), valB = getCellValue(b, index)
					return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
				}
			}
			function getCellValue(row, index){ return $(row).children('td').eq(index).text() }
		});
		</script>
	</body>
	</html>
