{% extends "base.html" %}
{% block title %}{% if not exchange %}Create{% else %}Edit{% endif %} exchange{% endblock %}

{% block javascript %}
{{ exchange_form.media|safe }}
{% endblock %}

{% block content %}

<h2>{% if not exchange %}Create{% else %}Edit{% endif %} {{ exchange_type }}
{% if request.project %}
	for {{ request.project }}
{% else %}
	{% if request.business %}
		for {{ request.business }}
	{% endif %}
{% endif %}
</h2>
{% if exchange.id %}
	<ul class='header-actions-left'>
		<li><a href='{% url edit_exchange_type exchange_id=exchange.id,exchange_type_slug=exchange.type.slug %}'>Edit Type</a></li>
	</ul>
{% endif %}

<form id="create-edit-exchange" action="" method="post" accept-charset="utf-8">
	<div style='display: none;'>{{ transaction_formset.management_form }}</div>
	<table id='create-exchange' class='vertical'>
		{{ exchange_form }}
		<tr>
			<td colspan='2'>
				<h2>Transactions</h2>
			</td>
		</tr>
		<tr>
			<td colspan='2'>
			<table class="horizontal">
				<tr>
					<th>Date</th>
					{% if exchange_type.debit %}
						<th>Debit</th>
					{% endif %}
					{% if exchange_type.credit %}
						<th>Credit</th>
					{% endif %}
					<th>Memo</th>
					<th>Count</th>
					<th>Rate</th>
					<th>R (C/D)</th>
					<th>Delete</th>
				</tr>
				{% for transaction_form in transaction_formset.forms %}
				<tr>
					{% include 'books/ledger/exchange/transaction.html' %}
				</tr>
				{% endfor %}
			</table>
			</td>
		</tr>
		<tr>
			<td colspan='2'>
				<h2>Repeat</h2>
			</td>
		</tr>
		<tr>
			<td colspan='2'>
			<table class="horizontal">
			{% if repeat_period_form.errors %}
				<tr>
					<td>{{ repeat_period_form.active.errors }}</td>
					<td>{{ repeat_period_form.count.errors }}</td>
					<td>{{ repeat_period_form.interval.errors }}</td>
				</tr>
			{% endif %}
				<tr>
					<td>{{ repeat_period_form.active }} <label for="id_repeat-active">repeat this {{ exchange_type|lower }} every</label></td>
					<td>{{ repeat_period_form.count }}</td>
					<td>{{ repeat_period_form.interval }}</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td class='remove'>
				{% if exchange.id %}
				<a href="{% url remove_exchange exchange_id=exchange.id %}{% if request.GET.next %}?next={{ request.GET.next }}{% endif %}"><img src='{{ MEDIA_URL }}/images/icons/cross.png' alt='cross'/> Remove {{ exchange.type }}</a>
				{% endif %}
			</td>
			<td class='submit'>
				<input type='button' value='Cancel' onclick='history.go(-1)' />
				&nbsp;
				<input type='submit' name='submit-button' value='Save &rarr;' />
			</td>
		</tr>
	</table>
</form>

{% endblock %}
