{% load i18n %}
{% load thumbnail %}
{% load currency_filters %}
{% load purchase_info_tags %}

{% 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 %}
    <div class="well">
        <h3>{% trans "You could be missing out on offers!" %}</h3>
        {% for upsell in upsell_messages %}
            {% blocktrans with message=upsell.message url=upsell.offer.get_absolute_url offer_name=upsell.offer.name %}
                <div class="warning">{{ message }} to qualify for the <a href="{{ url }}">{{ offer_name }}</a> special offer</div>
            {% endblocktrans %}
        {% endfor %}
    </div>
{% endif %}

{% if not basket.is_empty %}
    {% block basket_form_headers %}
        <div class="basket-title hidden-phone">
            <div class="row-fluid">
                <h4 class="span6">{% trans "Items to buy now" %}</h4>
                <h4 class="span3">{% trans "Quantity" %}</h4>
                <h4 class="span1 align-right">{% trans "Price" %}</h4>
                <h4 class="span2 align-right">{% trans "Total" %}</h4>
            </div>
        </div>
    {% endblock %}

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

            {% for form in formset %}
                {% with line=form.instance product=form.instance.product %}
                    {% purchase_info_for_line request line as session %}
                    <div class="basket-items">
                        <div class="row-fluid">
                            <div class="span2">
                                {{ form.id }}
                                {% with image=product.primary_image %}
                                    {% thumbnail image.original "100x100" upscale=False as thumb %}
                                        <a href="{{ product.get_absolute_url }}">
                                            <img class="thumbnail" src="{{ thumb.url }}" alt="{{ product.get_title }}"/>
                                        </a>
                                    {% endthumbnail %}
                                {% endwith %}
                            </div>
                            <div class="span4">
                                <h4><a href="{{ product.get_absolute_url }}">{{ line.description }}</a></h4>
                                <p class="availability {{ session.availability.code }}">{{ session.availability.message }}</p>
                            </div>
                            <div class="span3">
                                <div class="checkout-quantity control-group {% if form.errors %}error{% endif %}">
                                    {{ form.quantity }}
                                    <button class="btn" type="submit">{% trans "Update" %}</button>
                                    <p>
                                        <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>
                                        {% for field_errors in form.errors.values %}
                                            {% for error in field_errors %}
                                                <span class="error-block"><i class="icon-exclamation-sign"></i> {{ error }}</span>
                                            {% endfor %}

                                        {% endfor %}
                                    </p>
                                </div>
                            </div>
                            <div class="span1">
                                <p class="price_color align-right">
                                    {% if line.is_tax_known %}
                                        {{ line.unit_price_incl_tax|currency:line.price_currency }}
                                    {% else %}
                                        {{ line.unit_price_excl_tax|currency:line.price_currency }}
                                    {% endif %}
                                </p>
                            </div>
                            <div class="span2">
                                <p class="price_color align-right">
                                    {% if line.is_tax_known %}
                                        {{ line.line_price_incl_tax|currency:line.price_currency }}
                                    {% else %}
                                        {{ line.line_price_excl_tax|currency:line.price_currency }}
                                    {% endif %}
                                </p>
                            </div>
                        </div>
                    </div>
                {% endwith %}
            {% endfor %}
        </form>
    {% endblock %}

    <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 btn-full">{% trans "I have a voucher code..." %}</a></p>

                <div id="voucher_form_container" style="display:none">
                    <h4>{% trans "Voucher code" %}</h4>
                    <form id="voucher_form" action="{% url 'basket:vouchers-add' %}" method="post">
                        {% csrf_token %}
                        {% include "partials/form_fields.html" with form=voucher_form %}
                        <button type="submit" class="btn btn-info">{% trans "Add voucher" %}</button>
                        {% trans "or" %} <a href="#" id="voucher_form_cancel">{% trans "cancel" %}</a>
                    </form>
                </div>
            </div>
        {% endblock vouchers %}

        {% block baskettotals %}
            <div class="span6">
                <div class="sub-header">
                    <h2>{% trans "Totals" %}</h2>
                </div>
                {% include 'basket/partials/basket_totals.html' with editable=1 %}
            </div>
        {% endblock baskettotals %}
    </div>


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

{% else %}
    {% block emptybasket %}
        <p>
            {% trans "Your basket is empty." %}
            <a href="{{ homepage_url }}">{% trans "Continue shopping" %}</a>
        </p>
    {% endblock %}
{% endif %}

{% block savedbasket %}
    {% if request.user.is_authenticated and saved_formset %}
        <div class="well">
            <div class="sub-header">
                <h3>{% trans "To buy later" %}</h3>
            </div>
            <div class="row-fluid basket-title hidden-phone">
                <h4 class="span8">{% trans "Items" %}</h4>
                <h4 class="span2 align-center">{% trans "Price" %}</h4>
                <div class="span2">&nbsp;</div>
            </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 %}
                    {% purchase_info_for_product request form.instance.product as session %}
                    <div class="basket-items">
                        <div class="row-fluid">
                            <div class="span2">
                                {{ form.id }}
                                {% with image=form.instance.product.primary_image %}
                                    {% thumbnail image.original "100x100" upscale=False as thumb %}
                                        <a href="{{ form.instance.product.get_absolute_url }}">
                                            <img class="thumbnail" src="{{ thumb.url }}" alt="{{ form.instance.product.get_title }}">
                                        </a>
                                    {% endthumbnail %}
                                {% endwith %}
                            </div>
                            <div class="span6">
                                <h4><a href="{{ form.instance.product.get_absolute_url }}">{{ form.instance.description }}</a></h4>
                                <p class="availability {{ session.availability.code }}">{{ session.availability.message }}</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>
                            {% purchase_info_for_product request form.instance.product as saved %}
                            <div class="span2 align-center">
                                <p class="price_color">
                                    {% if saved.price.is_tax_known %}
                                        {{ saved.price.incl_tax|currency:saved.price.currency }}
                                    {% else %}
                                        {{ saved.price.excl_tax|currency:saved.price.currency }}
                                    {% endif %}
                                </p>
                            </div>
                            <div class="span2">
                                <a href="#" data-id="{{ forloop.counter0 }}" class="btn pull-right btn-block" data-behaviours="move">{% trans "Move to basket" %}</a>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </form>
        </div>
    {% endif %}
{% endblock %}
