{% extends "layout.html" %}
{% load currency_filters %}
{% load thumbnail %}
{% load i18n %}

{% block title %}
{% trans "Basket" %} | {{ block.super }}
{% endblock %}

{% block checkout-nav %}
{% include 'checkout/nav.html' with step=1 %}
{% endblock %}

{% block header %}
<div class="page-header action">
    <h1>{% trans "Basket" %}</h1>
</div>
{% endblock header %}

{% block content %}

{% if basket_warnings %}
    <h5>{% trans "Important messages about items in your basket" %}</h5>
	{% for warning in basket_warnings %}
	<div class="alert">{{ warning }}</div>
	{% endfor %}
{% endif %}

{% if upsell_messages %}
<h2>{% trans "You could be missing out on offers!" %}</h2>
	{% for upsell in upsell_messages %}
    {% blocktrans with message=upsell.message url=upsell.offer.get_absolute_url offer_name=upsell.offer.name %}
	<div class="warning">{{ upsell_message }} to qualify for the <a href="{{ url }}">{{ offer_name }}</a> special offer</div>
    {% endblocktrans %}
	{% endfor %}
{% endif %}

{% if not basket.is_empty %}
<div class="basket-title">
    <div class="row-fluid">
        <h4 class="span6">{% trans "Items to buy now" %}</h4>
        <h4 class="span5">{% trans "Quantity" %}</h4>
        <h4 class="span1">{% trans "Price" %}</h4>
    </div>
</div>

<form action="." method="post" class="basket_summary" id="basket_formset">
	{% csrf_token %}
	{{ formset.management_form }}

	{% for form in formset %}
    <div class="basket-items">
        <div class="row-fluid">
    	    <div class="span6">
    	        {{ form.id }}
    		    <div class="image_container">
					{% with image=form.instance.product.primary_image %}
					{% thumbnail image.original "200x200" upscale=False as thumb %}
					<a href="{{ form.instance.product.get_absolute_url }}"><img class="thumbnail" src="{{ thumb.url }}" alt="{{ product.get_title }}"></a>
					{% endthumbnail %}
					{% endwith %}
    		    </div>

    		    <h4><a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></h4>
    		    <p class="app-ico instock avaliability ">{{ form.instance.product.stockrecord.availability }}</p>
    	    </div>
    	    <div class="span5">
    		    <div class="checkout-quantity">
    		        {{ form.quantity }} 
                    <button class="btn" type="submit">{% trans "Update" %}</button>
                    <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="remove" class="inline">{% trans "Remove" %}</a>
					{% if request.user.is_authenticated %}
                    | <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="save" class="inline">{% trans "Save for later" %}</a>
					{% endif %}
    			    <div style="display:none">
    				    {{ form.save_for_later }}
    				    {{ form.DELETE }}
    			    </div>
    		    </div>
    	    </div>
    	    <div class="span1">
    		    <p class="price">{{ form.instance.unit_price_incl_tax|currency }}<p>
    	    </div>
        </div>
    </div>
    {% endfor %}
</form>

<div class="row-fluid">
	{% block vouchers %}
	<div class="span6">
		<div class="sub-header">
            <h2>{% trans "Voucher/Promo Code" %}</h2>
		</div>
        <p id="voucher_form_link"><a href="#voucher" class="btn">{% trans "I have a voucher code..." %}</a></p>

	        <div id="voucher_form_container" style="display:none">
                <h4>{% trans "Voucher code" %}</h4>
	        	<form action="{% url basket:vouchers-add %}" method="post" class="form-horizontal">
	        		{% csrf_token %}
	        		{% include "partials/form_fields.html" with form=voucher_form %}
	        		<div class="form-actions">
                        <button type="submit" class="btn btn-info">{% trans "Add voucher" %}</button>
                        or <a href="#" id="voucher_form_cancel">{% trans "cancel" %}</a>
	        		</div>
	        	</form>
	        </div>
	</div>
	{% endblock vouchers %}
	
	{% block baskettotals %}
	<div class="span6">
		<div class="sub-header">
            <h2>{% trans "Basket Totals" %}</h2>
		</div>
		<div id="basket_totals">
			<table class="table table-striped table-condensed">
				<tbody>
				{% with offer_discounts=basket.offer_discounts voucher_discounts=basket.grouped_voucher_discounts %}
				{% if offer_discounts or voucher_discounts %}
					<tr>
                        <th>{% trans "Basket total (before discounts)" %}</th>
						<td>{{ basket.total_incl_tax_excl_discounts|currency }}</td>
					</tr>
					{% if offer_discounts %}
						{% for discount in offer_discounts %}
						<tr>
                            <td class="basket-discount">{% trans "Special offer:" %} {{ discount.name }}</td>
							<td>-{{ discount.discount|currency }}</td>
						</tr>
						{% endfor %}
					{% endif %}
					{% if voucher_discounts %}
						<tr>
                            <th>{% trans "Vouchers" %}</th>
							<td></td>
						</tr>
						{% for discount in voucher_discounts %}
						<tr>
							<td>
								{{ discount.voucher.name }} ({{ discount.voucher.code }})
								<form action="{% url basket:vouchers-remove discount.voucher.id %}" method="POST">
									{% csrf_token %}
									<input type="submit" value="Remove" class="btn btn-small btn-danger"/>
								</form>
							</td>
							<td>-{{ discount.discount|currency }}</td>
						</tr>
						{% endfor %}
					{% endif %}
					<tr>
                        <th>{% trans "Basket total (after discounts)" %}</th>
						<td>{{ basket.total_incl_tax|currency }}</td>
					</tr>
				{% else %}
					<tr>
                        <th>{% trans "Basket total" %}</th>
						<td>{{ basket.total_incl_tax|currency }}</td>
					</tr>
				{% endif %}
				{% endwith %}
				<tr>
                    <th>{% trans "Shipping" %} ({{ shipping_method.name }})</th>
					<td>{{ shipping_charge_incl_tax|currency }}</td>
				</tr>
				<tr>
                    <th class="total"><h4>{% trans "Order total" %}</h4></th>
					<td class="total"><h4>{{ order_total_incl_tax|currency }}</h4></td>
				</tr>
				</tbody>
			</table>
		</div>
	</div>
	{% endblock baskettotals %}
</div>

	
{% block formactions %}
	<div class="form-actions clearfix" >
        <a href="{% url checkout:index %}" class="pull-right btn btn-large btn-primary">{% trans "Proceed to checkout" %}</a>
	</div>
{% endblock formactions %}

{% else %}
    {% block emptybasket %}
    <p>{% trans "Your basket is empty, you should probably add some items to buy." %}</p>
    <a href="/" class="btn btn-primary">{% trans "Continue shopping" %}</a>
    {% endblock %}
{% endif %}

{% if request.user.is_authenticated %}
<div class="well">
    <div class="sub-header">
        <h3>{% trans "To buy later" %}</h3>
    </div>

    {% if not saved_formset %}
    <p>{% trans "Your saved basket is empty." %}</p>

    {% else %}
    <div class="row-fluid basket-title">
        <h4 class="span8">{% trans "To buy an item now, click 'Move to basket'" %}</h4>
        <h4 class="span4">{% trans "Price" %}</h4>
    </div>
    <form action="{% url basket:saved %}" method="post" class="form-stacked later_summary" id="saved_basket_formset">
        {% csrf_token %}
        {{ saved_formset.management_form }}
        {% for form in saved_formset %}
        <div class="row-fluid basket-items">
    	    <div class="span8">
    			{{ form.id }}
    		    <div class="image_container">
					{% with image=form.instance.product.primary_image %}
					{% thumbnail image.original "200x200" upscale=False as thumb %}
					<a href="{{ form.instance.product.get_absolute_url }}"><img class="thumbnail" src="{{ thumb.url }}" alt="{{ product.get_title }}"></a>
					{% endthumbnail %}
					{% endwith %}
    		    </div>
    			<h4><a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></h4>
                <p class="app-ico {{ form.instance.product.stockrecord.availability_code }}">{{ form.instance.product.stockrecord.availability }}</p>
                <a href="#" data-id="{{ forloop.counter0 }}" data-behaviours="remove">{% trans "Remove" %}</a>
    			<div style="display:none">
    				{{ form.move_to_basket }}
    				{{ form.DELETE }}
    			</div>
    		</div>
            <div class="span2"><p class="price">{{ form.instance.unit_price_incl_tax|currency }}</p></div>
            <div class="span2"><a href="#" data-id="{{ forloop.counter0 }}" class="btn btn-success pull-right" data-behaviours="move">{% trans "Move to basket" %}</a></div>
        </div>
        {% endfor %}
    </form>
    {% endif %}
</div>
{% endif %}
{% endblock content %}

{% block onbodyload %}
oscar.basket.init();
{% endblock %}

{% block extrascripts %}
    {{ block.super }}
    <script src="{{ STATIC_URL }}oscar/js/oscar/checkout.js" type="text/javascript" charset="utf-8"></script>
{% endblock %}
