{% extends 'oscar/dashboard/layout.html' %}
{% load currency_filters %}
{% load i18n %}

{% block title %}
    {% blocktrans with name=offer.name %}
        {{ name }} | Offers
    {% endblocktrans %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'dashboard:index' %}">{% trans "Dashboard" %}</a></li>
            <li class="breadcrumb-item"><a href="{% url 'dashboard:offer-list' %}">{% trans "Offers" %}</a></li>
            <li class="breadcrumb-item active" aria-current="page">{{ offer.name }}</li>
        </ol>
    </nav>
{% endblock %}

{% block header %}
    <div class="page-header">
        <form id="status_form" class="float-right" method="post">
            {% csrf_token %}
            {% if offer.is_suspended %}
                <button type="submit" class="btn btn-success" name="unsuspend" data-loading-text="{% trans 'Reinstating...' %}">{% trans "Reinstate offer" %}</button>
            {% else %}
                <button type="submit" class="btn btn-secondary" name="suspend" data-loading-text="{% trans 'Suspending...' %}">{% trans "Suspend offer" %}</button>
            {% endif %}
            {% if not offer.vouchers.exists %}
            <a class="btn btn-danger" href="{% url 'dashboard:offer-delete' pk=offer.pk %}">{% trans "Delete offer" %}</a>
            {% endif %}
        </form>
        <h1>{{ offer.name }}</h1>
    </div>
{% endblock header %}

{% block dashboard_content %}

    <table class="table table-bordered">
        <tr>
            <td>
                {% if offer.is_available %}
                    <span class="badge badge-success">{% trans "Offer currently available" %}</span>
                {% else %}
                    <span class="badge badge-danger">{% trans "Offer not available due to restrictions!" %}</span>
                {% endif %}
            </td>
            <td>{% trans "Total cost:" %} <strong>{{ offer.total_discount|currency }}</strong></td>
            <td>{% trans "Number of orders:" %} <strong>{{ offer.num_orders }}</strong></td>
            <td>{% trans "Number of uses:" %} <strong>{{ offer.num_applications }}</strong></td>
        </tr>
    </table>

    <div class="table-header">
        <div class="float-right" style="font-weight: normal">{% trans "Date created:" %} {{ offer.date_created }}</div>
        <h2>{% trans "Offer details" %}</h2>
    </div>
    <table class="table table-striped table-bordered">
        <tbody>
            <tr>
                <th>{% trans "Name" %}</th>
                <td>{{ offer.name }}</td>
                <td rowspan="3"><a id="edit_metadata" href="{% url 'dashboard:offer-metadata' pk=offer.pk %}" class="btn btn-secondary">{% trans "Edit" %}</a></td>
            </tr>
            <tr>
                <th>{% trans "Description" %}</th>
                <td>{{ offer.description|safe|default:"-" }}</td>
            </tr>
            <tr>
                <th>{% trans "Type" %}</th>
                <td>{{ offer.get_offer_type_display }}</td>
            </tr>
            <tr>
                <th>{% trans "Incentive" %}</th>
                <td>{{ offer.benefit.description|safe }}</td>
                <td><a href="{% url 'dashboard:offer-benefit' pk=offer.pk %}" class="btn btn-secondary">{% trans "Edit" %}</a></td>
            </tr>
            <tr>
                <th>{% trans "Condition" %}</th>
                <td>{{ offer.condition.description|safe }}</td>
                <td><a href="{% url 'dashboard:offer-condition' pk=offer.pk %}" class="btn btn-secondary">{% trans "Edit" %}</a></td>
            </tr>
            <tr>
                <th>{% trans "Restrictions" %}</th>
                <td>
                    {% for restriction in offer.availability_restrictions %}
                        {% if not restriction.is_satisfied %}
                            <span class="badge badge-danger">
                                {{ restriction.description }}
                            </span><br/>
                        {% else %}
                            {{ restriction.description }}<br/>
                        {% endif %}
                    {% endfor %}
                </td>
                <td><a href="{% url 'dashboard:offer-restrictions' pk=offer.pk %}" class="btn btn-secondary">{% trans "Edit" %}</a></td>
            </tr>
            {% if offer.is_voucher_offer_type %}
            <tr>
                <th>{% trans "Num of vouchers" %}</th>
                <td>{{ offer.vouchers.count }}</td>
            </tr>
            {% endif %}
        </tbody>
    </table>

    {% if offer.is_voucher_offer_type %}
    <div class="table-header">
        <h2>{% trans "Attached vouchers" %}</h2>
    </div>
    <table class="table table-striped table-bordered table-hover">
        {% if offer.vouchers.exists %}
        <thead>
            <tr>
                <th>{% trans "Name" %}</th>
                <th>{% trans "Code" %}</th>
                <th>{% trans "Status" %}</th>
            </tr>
        </thead>
        <tbody>
            {% for voucher in offer.vouchers.all %}
            <tr>
                <td>
                    <a href="{% url 'dashboard:voucher-stats' voucher.id %}">{{ voucher.name }}</a>
                </td>
                <td>
                    <span class="badge badge-secondary">{{ voucher.code }}</span>
                </td>
                <td>
                    {% if voucher.is_active %}
                    <span class="badge badge-success">{% trans "Active" %}</span>
                    {% else %}
                    <span class="badge badge-secondary">{% trans "Inactive" %}</span>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
        {% else %}
        <tr>
            <td>{% trans "No vouchers are attached to this offer." %}</td>
        </tr>
        {% endif %}
    </table>
    {% endif %}

    {% if order_discounts %}
        <div class="table-header">
            <a class="float-right btn" href=".?format=csv">{% trans "Export to CSV" %}</a>
            <h2>{% trans "Orders that used this offer" %}</h2>
        </div>
        <table class="table table-bordered table-striped">
            <thead>
                <th>{% trans "Order number" %}</th>
                <th>{% trans "Order date" %}</th>
                <th>{% trans "Order total" %}</th>
                <th>{% trans "Cost" %}</th>
            </thead>
            <tbody>
                {% for discount in order_discounts %}
                    {% with order=discount.order %}
                        <tr>
                            <td><a href="{% url 'dashboard:order-detail' number=order.number %}">{{ order.number }}</a></td>
                            <td>{{ order.date_placed }}</td>
                            <td>{{ order.total_incl_tax|currency }}</td>
                            <td>{{ discount.amount|currency }}</td>
                        </tr>
                    {% endwith %}
                {% endfor %}
            </tbody>
        </table>
        {% include 'oscar/dashboard/partials/pagination.html' %}
    {% endif %}

{% endblock dashboard_content %}
