{% extends "pretixpresale/event/base.html" %}
{% load i18n %}
{% load l10n %}
{% load eventurl %}
{% load eventsignal %}
{% load thumbnail %}
{% load rich_text %}
{% block title %}{% trans "Voucher redemption" %}{% endblock %}

{% block content %}
    <h2>{% trans "Voucher redemption" %}</h2>
    {% if subevent %}
        <h3>{{ subevent.name }}</h3>
    {% endif %}
    <p>
        {% blocktrans trimmed %}
            You entered a voucher code that allows you to buy one of the following products at the specified price:
        {% endblocktrans %}
    </p>
    {% if event.presale_is_running or event.settings.show_items_outside_presale_period %}
        <form method="post" data-asynctask
                action="{% eventurl request.event "presale:event.cart.add" cart_namespace=cart_namespace %}?next={% eventurl request.event "presale:event.index" cart_namespace=cart_namespace %}">
            {% csrf_token %}
            <input type="hidden" name="subevent" value="{{ subevent.id|default_if_none:"" }}" />
            <input type="hidden" name="_voucher_code" value="{{ voucher.code }}">
            {% for tup in items_by_category %}
                <section>
                    {% if tup.0 %}
                        <h3>{{ tup.0.name }}</h3>
                        {% if tup.0.description %}
                            <p>{{ tup.0.description|localize|rich_text }}</p>
                        {% endif %}
                    {% endif %}
                    {% for item in tup.1 %}
                        {% if item.has_variations %}
                            <div class="item-with-variations">
                                <div class="row-fluid product-row headline">
                                    <div class="col-md-8 col-xs-12">
                                        {% if item.picture %}
                                            <a href="{{ item.picture.url }}" class="productpicture"
                                                    data-title="{{ item.name|force_escape|force_escape }}"
                                                    {# Yes, double-escape to prevent XSS in lightbox #}
                                                    data-lightbox="{{ item.id }}">
                                                <img src="{{ item.picture|thumbnail_url:'productlist' }}"
                                                        alt="{{ item.name }}"/>
                                            </a>
                                        {% endif %}
                                        <div class="product-description {% if item.picture %}with-picture{% endif %}">
                                            <strong>{{ item.name }}</strong>
                                            {% if item.description %}
                                                <div class="product-description">
                                                    {{ item.description|localize|rich_text }}
                                                </div>
                                            {% endif %}
                                        </div>
                                    </div>
                                    <div class="col-md-2 col-xs-6 price">
                                        {% if item.min_price != item.max_price or item.free_price %}
                                            {% blocktrans trimmed with minprice=item.min_price|floatformat:2 currency=event.currency %}
                                                from {{ currency }} {{ minprice }}
                                            {% endblocktrans %}
                                        {% elif not item.min_price and not item.max_price %}
                                            {% trans "FREE" context "price" %}
                                        {% else %}
                                            {{ event.currency }} {{ item.min_price|floatformat:2 }}
                                        {% endif %}
                                    </div>
                                    <div class="col-md-2 col-xs-6 availability-box">
                                    </div>
                                    <div class="clearfix"></div>
                                </div>
                                <div class="">
                                    {% for var in item.available_variations %}
                                        <div class="row-fluid product-row variation">
                                            <div class="col-md-8 col-xs-12">
                                                {{ var }}
                                                {% if var.description %}
                                                    <div class="variation-description">
                                                        {{ var.description|localize|rich_text }}
                                                    </div>
                                                {% endif %}
                                            </div>
                                            <div class="col-md-2 col-xs-6 price">
                                                {% if item.free_price %}
                                                    <div class="input-group input-group-price">
                                                        <span class="input-group-addon">{{ event.currency }}</span>
                                                        <input type="number" class="form-control input-item-price"
                                                                placeholder="0"
                                                                min="{% if event.settings.display_net_prices %}{{ var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
                                                                name="price_{{ item.id }}_{{ var.id }}"
                                                                value="{% if event.settings.display_net_prices %}{{var.display_price.net|stringformat:"0.2f" }}{% else %}{{ var.display_price.gross|stringformat:"0.2f" }}{% endif %}"
                                                                step="any">
                                                    </div>
                                                {% elif not var.display_price.gross %}
                                                    {% trans "FREE" context "price" %}
                                                {% elif event.settings.display_net_prices %}
                                                    {{ event.currency }} {{ var.display_price.net|floatformat:2 }}
                                                {% else %}
                                                    {{ event.currency }} {{ var.display_price.gross|floatformat:2 }}
                                                {% endif %}
                                                {% if var.display_price.rate and var.display_price.gross and event.settings.display_net_prices %}
                                                    <small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
                                                        <strong>plus</strong> {{ rate }}% taxes
                                                    {% endblocktrans %}</small>
                                                {% elif var.display_price.rate and var.display_price.gross %}
                                                    <small>{% blocktrans trimmed with rate=var.display_price.rate name=var.display_price.name %}
                                                        incl. {{ rate }}% taxes
                                                    {% endblocktrans %}</small>
                                                {% endif %}
                                            </div>
                                            {% if var.cached_availability.0 == 100 %}
                                                <div class="col-md-2 col-xs-6 availability-box available radio-box">
                                                    <label>
                                                        {% if max_times > 1 %}
                                                            <input type="number" class="form-control input-item-count" placeholder="0" min="0"
                                                                    max="{{ item.order_max }}" name="variation_{{ item.id }}_{{ var.id }}">
                                                        {% else %}
                                                            <input type="radio" name="_voucher_item"
                                                                    {% if options == 1 %}checked="checked"{% endif %}
                                                                    value="variation_{{ item.id }}_{{ var.id }}">
                                                        {% endif %}
                                                    </label>
                                                </div>
                                            {% else %}
                                                {% include "pretixpresale/event/fragment_availability.html" with avail=var.cached_availability.0 %}
                                            {% endif %}
                                            <div class="clearfix"></div>
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                        {% else %}
                            <div class="row-fluid product-row simple">
                                <div class="col-md-8 col-xs-12">
                                    {% if item.picture %}
                                        <a href="{{ item.picture.url }}" class="productpicture"
                                                data-title="{{ item.name|force_escape|force_escape }}"
                                                {# Yes, double-escape to prevent XSS in lightbox #}
                                                data-lightbox="{{ item.id }}">
                                            <img src="{{ item.picture|thumbnail_url:'productlist' }}"
                                                    alt="{{ item.name }}"/>
                                        </a>
                                    {% endif %}
                                    <div class="product-description {% if item.picture %}with-picture{% endif %}">
                                        <strong>{{ item.name }}</strong>
                                        {% if item.description %}
                                            <div class="product-description">
                                                {{ item.description|localize|rich_text }}
                                            </div>
                                        {% endif %}
                                    </div>
                                </div>
                                <div class="col-md-2 col-xs-6 price">
                                    {% if item.free_price %}
                                        <div class="input-group input-group-price">
                                            <span class="input-group-addon">{{ event.currency }}</span>
                                            <input type="number" class="form-control input-item-price" placeholder="0"
                                                    min="{% if event.settings.display_net_prices %}{{ item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
                                                    name="price_{{ item.id }}"
                                                    value="{% if event.settings.display_net_prices %}{{ item.display_price.net|stringformat:"0.2f" }}{% else %}{{ item.display_price.gross|stringformat:"0.2f" }}{% endif %}"
                                                    step="any">
                                        </div>
                                    {% elif not item.display_price.gross %}
                                        {% trans "FREE" context "price" %}
                                    {% elif event.settings.display_net_prices %}
                                        {{ event.currency }} {{ item.display_price.net|floatformat:2 }}
                                    {% else %}
                                        {{ event.currency }} {{ item.display_price.gross|floatformat:2 }}
                                    {% endif %}
                                    {% if item.display_price.rate and item.display_price.gross and event.settings.display_net_prices %}
                                        <small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
                                            <strong>plus</strong> {{ rate }}% {{ name }}
                                        {% endblocktrans %}</small>
                                    {% elif item.display_price.rate and item.display_price.gross%}
                                        <small>{% blocktrans trimmed with rate=item.display_price.rate name=item.display_price.name %}
                                            incl. {{ rate }}% {{ name }}
                                        {% endblocktrans %}</small>
                                    {% endif %}
                                </div>
                                {% if item.cached_availability.0 == 100 %}
                                    <div class="col-md-2 col-xs-6 availability-box available radio-box">
                                        <label>
                                            {% if max_times > 1 %}
                                                <input type="number" class="form-control input-item-count" placeholder="0" min="0"
                                                        max="{{ item.order_max }}" name="item_{{ item.id }}">
                                            {% else %}
                                                <input type="radio" name="_voucher_item"
                                                        {% if options == 1 %}checked="checked"{% endif %}
                                                        value="item_{{ item.id }}">
                                            {% endif %}
                                        </label>
                                    </div>
                                {% else %}
                                    {% include "pretixpresale/event/fragment_availability.html" with avail=item.cached_availability.0 %}
                                {% endif %}
                                <div class="clearfix"></div>
                            </div>
                        {% endif %}
                    {% endfor %}
                </section>
            {% endfor %}
            {% eventsignal event "pretix.presale.signals.voucher_redeem_info" voucher=voucher %}
            {% if event.presale_is_running %}
                <div class="row-fluid checkout-button-row">
                    <div class="col-md-4 col-md-offset-8 col-xs-12">
                        <button class="btn btn-block btn-primary btn-lg" type="submit">
                            <i class="fa fa-shopping-cart"></i> {% trans "Add to cart" %}
                        </button>
                    </div>
                    <div class="clearfix"></div>
                </div>
            {% endif %}
        </form>
    {% endif %}
{% endblock %}
