{% extends "base.html" %}
{% macro render_pagination(pagination) %}
	<ul class="pagination custom">
		{% if pagination.has_prev %}
			<li><a href="{{ url_for_other_page(pagination.page - 1) }}">&laquo; Previous</a></li>
		{% endif %}
		{%- for page in pagination.iter_pages() %}
			{% if page %}

				{% if page != pagination.page %}
					<li><a href="{{ url_for_other_page(page) }}">{{ page }}</a></li>
				{% else %}
					<li class="active"><a href="#"><strong>{{ page }}</strong></a></li>
				{% endif %}
			{% else %}
				<li><span class=ellipsis>…</span></li>
			{% endif %}
		{%- endfor %}
		{% if pagination.has_next %}
			<li><a href="{{ url_for_other_page(pagination.page + 1) }}">Next &raquo;</a></li>
		{% endif %}
	</ul>
{% endmacro %}

{% block pagetitle -%}Package Review{%- endblock pagetitle %}
{% block stats %}{% endblock stats %}
{% block bottomcontent -%}

	<div class="col-md-12">
		<div id="bottom-wrap" class="block-flat">
			<div class="header">
				<h3>Packages Pending Review</h3>
			</div>
			<div class="content">
				{% if not completed or completed == "0" %}
					None
				{% else %}
					<table class="table no-border hover">
						<thead class="no-border">
						<tr>
							<th>Build&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
							<th>Name</th>
							<th>Version</th>
							<th>Start</th>
							<th>End</th>
							<th>Review Status</th>
							<th>Reviewed By</th>
							<th>Review Date</th>
						</tr>
						</thead>
						<tbody class="no-border-y">


						{% for build in completed|sort(attribute='bnum') %}
							{% if build.review_stat == "pending" %}
								{% if current_user.is_authenticated %}

									<tr>
										<td><a href="/build/{{ build.bnum }}">{{ build.bnum }}</a></td>
										<td><a href="/pkg/{{ build.pkgname }}">{{ build.pkgname }}</a></td>
										<td>{{ build.version_str }}</td>
										<td>{{ build.start_str }}</td>
										<td>{{ build.end_str }}</td>
										<td>{% if build.review_status == 'pending' %}
											<div class="btn-group">
												<button class="btn btn-default btn-xs" type="button">Set Result</button>
												<button data-toggle="dropdown"
														class="btn btn-xs btn-primary dropdown-toggle" type="button">
													<span class="caret"></span>
													<span class="sr-only">Toggle Dropdown</span></button>
												<ul role="menu" class="dropdown-menu set_result">
													<li><a href="#" id="passed" bnum="{{ build.bnum }}"><i
														class="fa fa-check"
														style="color:green;"></i>
														Pass</a></li>
													<li><a href="#" id="failed" bnum="{{ build.bnum }}"><i
														class="fa fa-exclamation" style="color:red;"></i>
														Fail</a>
													</li>
													<li style="font-style: italic;"><a href="#" id="skip"
																					   bnum="{{ build.bnum }}"><i
														class="fa fa-eye-slash" style="color:#777777;"></i>
														Skip</a>
													</li>
												</ul>
											</div>
											<span class="passed" style="display: none"><i class="fa fa-check"
																						  style="color:green;"></i> Passed</span>
											<span class="failed" style="display: none"><i
												class="fa fa-exclamation-circle"
												style="color:red;"></i> Failed</span>
										{% elif build.review_status == 'passed' %}
											<i class="fa fa-check" style="color:green;"></i>
											{{ build.review_status }}{% elif build.review_status == 'failed' %}
											<i class="fa fa-exclamation-circle" style="color:red;"></i>
											{{ build.review_status }} {% elif build.review_status == 'skip' %}
											<i class="fa fa-eye-slash" style="color:#999999;"></i>
											<span style="color:#999999;"><em>skipped</em></span>
										{% endif %}</td>
										<td>
											{% if build.review_dev != '' and build.review_dev is not none and build.review_dev != 'None' %}
												<i class="fa fa-user"></i>
												{{ build.review_dev }}{% else %}n/a{% endif %}</td>
										<td>
											{% if build.review_date != '' and build.review_date is not none and build.review_date != 'None' %}
												<i class="fa fa-calendar"></i>
												{{ build.review_date }}{% else %}n/a{% endif %}</td>
									</tr>

								{% else %}
									<tr>
										<td><a href="/build/{{ build.bnum }}">{{ build.bnum }}</a></td>
										<td>{{ build.pkgname }}</td>
										<td>{{ build.version_str }}</td>
										<td>{{ build.start_str }}</td>
										<td>{{ build.end_str }}</td>
										<td>{% if build.review_status == 'pending' %}
											<i class="fa fa-clock-o" style="color:yellow;"></i>
											{{ build.review_status }}{% elif build.review_status == 'passed' %}
											<i class="fa fa-check" style="color:green;"></i>
											{{ build.review_status }}{% elif build.review_status == 'failed' %}
											<i class="fa fa-exclamation-circle" style="color:red;"></i>
											{{ build.review_status }}{% elif build.review_status == 'skip' %}
											<i class="fa fa-eye-slash" style="color:#999999;"></i>
											<span style="color:#999999;"><em>skipped</em></span>{% endif %}</td>
										<td>
											{% if build.review_dev != '' and build.review_dev is not none and build.review_dev != 'None' %}
												<i class="fa fa-user"></i>
												{{ build.review_dev }}{% else %}n/a{% endif %}</td>
										<td>
											{% if build.review_date != '' and build.review_date is not none and build.review_date != 'None' %}
												<i class="fa fa-calendar"></i>
												{{ build.review_date }}{% else %}n/a{% endif %}</td>
									</tr>
								{% endif %}
							{% endif %}
						{% endfor %}


						</tbody>
					</table>
					{{ render_pagination(pagination) }}
				{% endif %}
			</div>
			<div class="loading"><i class="fa fa-refresh fa-spin"></i></div>
		</div>


	</div>

{%- endblock bottomcontent %}
{% block scripts %}
	{{ super() }}
	<script type="text/javascript">
		jQuery(document).ready(function () {
			jQuery('.set_result').on('click', 'a', function (e) {
				e.preventDefault();
				jQuery(this).addClass('clicked');
				var data = {
					bnum: jQuery(this).attr('bnum'),
					result: jQuery(this).attr('id'),
					dev: '{{ user.username }}'
				};
				jQuery.ajax({
					type: 'POST',
					url: '/pkg_review',
					data: JSON.stringify(data, null, '\t'),
					contentType: 'application/json;charset=UTF-8',
					dataType: 'json',
					success: function (result) {
						console.log(result);
						var res = $('.clicked').attr('id') == '2' ? 'span.passed' : 'span.failed';
						if (result['msg'] == 'ok') {
							jQuery('.clicked').parents('td').find(res).show();
							jQuery('.clicked').removeClass('clicked').parents('.btn-group').hide();
						} else {
							alert('Unable to save result. Server Error Msg:\n' + result['msg']);
						}
					}

				});
			});
		});
	</script>
{% endblock scripts %}