{% extends "layout.html" %}

{% load currency_filters %}

{% block header %}
<h2>Basket</h2>
{% endblock header %}


{% block content %}

{% if request.basket.lines.count %}

<form action="." method="post">
	{% csrf_token %}
	
	{{ formset.management_form }}

	<table>
		<thead>
			<tr>
				<th>Product</th>
				<th>Availability</th>
				<th>Quantity</th>
				<th>Unit Price Excl. Tax</th>
				<th>Tax</th>
				<th>Line Price Excl. Tax</th>
				<th>Line Price Incl. Tax</th>
				<th>Save for later&hellip;</th>
				<th>Delete?</th>				
			</tr>
		</thead>
		<tbody>
			{% for form in formset %}
			<tr>
				<td>{{ form.id }}<a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></td>
				<td>{{ form.instance.product.stockrecord.availability }}</td>
				<td>{{ form.quantity }}</td>
		        <td>{{ form.instance.unit_price_excl_tax|currency }}</td>
		        <td>{{ form.instance.unit_tax|currency }}</td>
		        <td>{{ form.instance.line_price_excl_tax|currency }}</td>
		        <td>{{ form.instance.line_price_incl_tax|currency }}</td>
				<td>{{ form.save_for_later }}</td>
				<td>{{ form.DELETE }}</td>
			</tr>
			{% endfor %}
			{% for discount in basket.discounts %}
		    <tr>
		        <td colspan="6">{{ discount.name }}</td>
		        <td colspan="3">-{{ discount.discount|currency }}</td>
		    </tr>
		    {% endfor %}
		    <tr>
		        <td colspan="5">Total</td>
		        <td>{{ request.basket.total_excl_tax|currency }}</td>
		        <td colspan="3">{{ basket.total_incl_tax|currency }}</td>
		    </tr>
		</tbody>
	</table>
	

	<input type="submit" value="Apply Changes" />

</form>

<a href="{% url checkout:index %}">Checkout</a>

{% else %}

<p>Your basket is empty, you should probably add some items to buy.</p>

{% endif %}

{% if request.basket.pk %}
<h4>Basket vouchers</h4>
<ul>
{% for voucher in basket.vouchers.all %}
    <li>
        <form action="{% url basket:vouchers-remove voucher.id %}" method="POST">
            {% csrf_token %}
            {{ voucher.name }} ({{ voucher.code }})
            <input type="submit" value="Remove" />
        </form>
    </li>
{% endfor %}
</ul>
{% endif %}

<form action="{% url basket:vouchers-add %}" method="post">
    {% csrf_token %}
	{{ voucher_form }}
    <input type="submit" value="Add voucher" />
</form>


{% if saved_formset %}

<h3>To buy later</h3>

<form action="{% url basket:saved %}" method="post">
	{% csrf_token %}
	
	{{ saved_formset.management_form }}
	
	<table>
		<thead>
			<tr>
				<th>Product</th>
				<th>Unit Price Excl. Tax</th>
				<th>Tax</th>
				<th>Line Price Excl. Tax</th>
				<th>Line Price Incl. Tax</th>
				<th>Move to basket&hellip;</th>			
				<th>Delete?</th>
			</tr>
		</thead>
		<tbody>
			{% for form in saved_formset %}
			<tr>
				<td>{{ form.id }}<a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></td>
		        <td>{{ form.instance.unit_price_excl_tax|currency }}</td>
		        <td>{{ form.instance.unit_tax|currency }}</td>
		        <td>{{ form.instance.line_price_excl_tax|currency }}</td>
		        <td>{{ form.instance.line_price_incl_tax|currency }}</td>
				<td>{{ form.move_to_basket }}</td>
				<td>{{ form.DELETE }}</td>
			</tr>
			{% endfor %}
		</tbody>
	</table>

	

	<input type="submit" value="Apply Changes" />

</form>

{% endif %}

{% endblock content %}

