{% extends "tcms_base.html" %}

{% load extra_filters %}
{% load static %}

{% block subtitle %}Create new test run{% endblock %}

{% block custom_stylesheet %}
<link rel="stylesheet" type="text/css" href="{% static "style/dataTables/jquery.dataTables.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "style/dataTables/jquery.dataTables_themeroller.css" %}" />
{% endblock %}

{% block custom_javascript %}
<script type="text/javascript" src="{% static "js/lib/jquery.dataTables.js" %}"></script>
<script type="text/javascript" src="{% static "js/testrun_actions.js" %}"></script>
<script type="text/javascript" src="{% static "js/testcase_actions.js" %}"></script>
<script type="text/javascript" src="{% static "js/testplan_actions.js" %}"></script>
<script type="text/javascript" src="{% static "js/nitrate.toggledetail.js" %}"></script>
<script type="text/javascript" src="{% static "admin/js/admin/RelatedObjectLookups.js" %}"></script>

<script type="text/javascript">
Nitrate.Utils.after_page_load(Nitrate.TestRuns.New.on_load);
</script>
{% endblock %}

{% block contents %}
<div id="content">
	<div id="tcms_values" class="hidden">
		<input id="value_module" type="hidden" name="module" value="{{ module }}" />
		<input id="value_sub_module" type="hidden" name="sub_module" value="{{ sub_module }}" />
	</div>
	<div class="sprites crumble">
		<a href="{% url "nitrate-index" %}">Home</a>
		>> <a href="{% url "plans-all" %}?is_active=on">...</a>
		>> <a href="{{ test_plan.get_absolute_url }}">{{ test_plan.plan_id }}: {{ test_plan.name }}</a>
		>> New Test Run
	</div>
	<h2>Create New Test Run</h2>
	<form action="{% url "run-new" %}" method="post">{% csrf_token %}
		<input type="hidden" id="id_plan_id" name="from_plan" value="{{ from_plan }}" />
		{# Working around for the bug https://bugzilla.redhat.com/show_bug.cgi?id=715499 #}
		<input type="hidden" name="POSTING_TO_CREATE" value="YES" />
		<div class="Detailform border-1">
			<table class="editor" cellspan="0" cellspacing="0">
				<tr>
					<td width="15%" valign="top"><label class="strong" for="id_summary">Summary</label></td>
					<td>
						<span class="summary">{{ form.summary }}</span>
						<div class="errors">{{ form.summary.errors }}</div>
					</td>
				</tr>
				<tr>
					<td width="15%" valign="top"><label for="">Product</label></td>
					<td>{{ form.product }}</td>
				</tr>
				<tr>
					<td valign="top"><label>Product Version</label></td>
					<td>
						{{ form.product_version }}
						<span class="errors">{{ form.product_version.errors }}</span>
						<a href="{% url "admin:management_version_add" %}" class="addlink" id="add_id_product_version">Add Product Version</a>
					</td>
				</tr>
				<tr>
					<td valign="top"><label class="strong" for="id_build_id">Build </label></td>
					<td>
						{{ form.build }}
						<span class="errors">{{ form.build.errors }}</span>
						<a href="{% url "admin:management_testbuild_add" %}" class="addlink" id="add_id_build">Add Build</a>
					</td>
				</tr>
				<tr>
					<td valign="top" valign="top"><label for="id_manager">Run Manager </label></td>
					<td valign="top">{{ form.manager }}<div class="errors">{{ form.manager.errors }}</div></td>
				</tr>
				<tr>
					<td valign="top" valign="top"><label for="id_default_tester">Default Tester</label></td>
					<td>{{ form.default_tester }} <div class="errors">{{ form.default_tester.errors }}</div></td>
				</tr>
				<tr>
					<td valign="top" valign="top"><label for="auto_update_run_status">Set Status Automatically</label></td>
					<td>{{ form.auto_update_run_status }} <span class="system_help">{{ form.auto_update_run_status.help_text }}</span></td>
				</tr>
				<tr>
					<td valign="top" valign="top"><label class="strong" for="id_estimated_time">Estimated Time</label></td>
					<td>
						<div id="estimated_time" data-time="{{ run_estimated_time|timedelta2seconds }}">
							{{ form.estimated_time }}
							<div class="errors">{{ form.estimated_time.errors }}</div>
						</div>
					</td>
				</tr>
				<tr>
					<td valign="top"><label for="id_notes">Notes</label></td>
					<td>{{ form.notes }}</td>
				</tr>
				<tr>
					<td valign="top"><label>Environment</label></td>
					<td>
						{% for env_group in test_plan.env_group.all %}
						<fieldset class="">
							<legend>Environment Group: {{ env_group.name }}</legend>
							<ul class="ul-no-format">
								{% for property in env_group.property.all %}
								<li style="clear:both">
									<input class="lab-50" type="checkbox" name="select_property_id_{{ property.id }}" checked>
									<label class="lab-100">{{ property.name }}</label>
									<input type="hidden" name="env_property_id" value="{{ property.id }}">

									<select name="select_property_value_{{ property.id }}">
										{% for value in property.value.all %}
										<option value="{{ value.id }}">{{ value.value }}</option>
										{% endfor %}
									</select>
								</li>
								{% endfor %}
							</ul>
						</fieldset>
						{% empty%}
						<div class="system_help">
							<p>
								Environment group must be set for this test plan (<b>{{ test_plan.product.name }}</b>:<b>{{ test_plan.product_version.value }}</b>:<b><a href="{{ test_plan.get_absolute_url }}">{{ test_plan }}</a></b>) before you will be able to select environments for this test run.If you do not have permission to edit this plan, please contact the author of this plan, mailto <a href="mailto:{{ test_plan.author.email }}">{{ test_plan.author.email }}</a>
							</p>
						</div>
						{% endfor %}
					</td>
				</tr>
			</table>
			<div class="submit-row ">
				<input type="submit" name="" value="Save">
				<input type="button" name="cancel" value="cancel" class="js-cancel-button">
			</div>
		</div>
	{% if test_cases %}
	<input type="hidden" name="testcases" value="testcases" />
	<div class="Detailform border-1" id="testcases_selected">
		<div class="mixbar">
			<span class="title"><b>Selected cases:</b></span>
				<span class="notice">
					{% if  num_unconfirmed_cases %}
					<img src="{% static "images/warning.png" %}" style="float:none"/>
					{{ num_unconfirmed_cases }} unconfirmed cases removed
					{% endif %}
				</span>
			</div>
			<table id="testcases" class="list" cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
						<th align="left" width="2%"></th>
						<th class="widthID" align="left" width="4%">ID</th>
						<th align="left" width="26%">Test Case Summary</th>
						<th align="left" width="20%">Author</th>
						<th align="left" width="20%">Created Date</th>
						<th align="left" width="8%">Category</th>
						<th align="left"  width="8%">Priority</th>
						<th align="left" width="10%">Action</th>
					</tr>
				</thead>
				<tbody>
					{% for test_case in test_cases %}
					<tr class="{% cycle 'odd' 'even' %}" id="row_{{ forloop.counter }}">
						<td>
							<a id="blind_link_{{ forloop.counter }}" class="blind_link" href="javascript:void(0)">
								<img id="blind_icon_{{ forloop.counter }}" class="blind_icon" src="{% static "images/t1.gif" %}" border="0" alt="">
							</a>
						</td>
						<td>
							<a href="/case/{{ test_case.case_id }}/">{{ test_case.case_id }}</a>
							{% if test_case.case_status.name == 'CONFIRMED' %}
							<input type="hidden" name="case" value="{{ test_case.case_id }}">
							{% endif %}
						</td>
						<td{% if not test_case.case_status.name == 'CONFIRMED' %} style='text-decoration: line-through'{% endif %}>
							{% if not test_case.case_status.name == 'CONFIRMED' %} <img src="{% static "images/warning.png" %}"/>{% endif %}
							<a id="link_{{ forloop.counter }}" class="blind_title_link js-case-summary" data-param="{{ forloop.counter }}">
								{{ test_case.summary }}
							</a>
						</td>
						<td>{{ test_case.author.email }}</td>
						<td>{{ test_case.create_date }}</td>
						<td>{{ test_case.category }}</td>
						<td>{{ test_case.priority }}</td>
						<td>
							<a class="deletelink js-remove-case" data-params='["row_{{ forloop.counter }}", {{ test_case.estimated_time|timedelta2seconds }}]'>Remove</a>
						</td>
					</tr>
					<tr id="hidenRow_{{ forloop.counter }}" class="Detailform border-1 hide" style="display: none;">
						<td id="ajax_loading_{{ forloop.counter }}" colspan="8">
							<div class="ajax_loading"></div>
						</td>
					</tr>
					{% endfor %}
				</tbody>
			</table>
			{% else %}
			<span class="prompt-message">
				<center>
					No testcase in this testrun , you may <a href="{{ test_plan.get_absolute_url }}"><strong>Select Cases</strong></a> from test plan.
				</center>
			</span>
			<script type="text/javascript">
			var msg = "No valid cases are selected. Please select some cases or confirm selected ones before creating a run."
			alert(msg);
			window.history.go(-1);
			</script>
			{% endif %}
		</div>
	</form>
</div>
{% endblock %}
