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

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

{% block breadcrumbs %} 	
<ul class="breadcrumb">   	
    <li>  	
        <a href="{% url dashboard:index %}">{% trans "Dashboard" %}</a>   	
        <span class="divider">/</span>   	
    </li>   	
    <li>   	
        <a href="{% url dashboard:offer-list %}">{% trans "Offer management" %}</a>
        <span class="divider">/</span>
    </li>
    <li class="active">{{ offer.name }}</li>
</ul>
{% endblock %}

{% block header %}
<div class="page-header">
    <form id="status_form" class="pull-right" method="post" action=".">
        {% csrf_token %}
        {% if offer.is_suspended %}
        <button type="submit" class="btn btn-success" name="unsuspend">{% trans "Reinstate offer" %}</button>
        {% else %}
        <button type="submit" class="btn" name="suspend">{% trans "Suspend offer" %}</button>
        {% endif %}
        <a class="btn btn-danger" href="{% url dashboard:offer-delete offer.pk %}">{% trans "Delete offer" %}</a>
    </form>
    <h1>{{ offer.name }}</h1>
</div>
{% endblock header %}

{% block dashboard_content %}

    <table class="table table-bordered">
        <tr>
            <td>
                {% if offer.is_available %}
                    <span class="label label-success">{% trans "Offer currently available" %}</span>
                {% else %}
                    <span class="label label-important">{% 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|currency }}</strong></td>
            <td>{% trans "Number of uses:" %} <strong>{{ offer.num_applications|currency }}</strong></td>
        </tr>
    </table>

    <div class="table-header">
        <div class="pull-right" style="font-weight: normal">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="2"><a id="edit_metadata" href="{% url dashboard:offer-metadata offer.pk %}" class="btn">Edit</a></td>
            </tr>
            <tr>
                <th>{% trans "Description" %}</th>
                <td>{{ offer.description|safe }}</td>
            </tr>
            <tr>
                <th>{% trans "Incentive" %}</th>
                <td>{{ offer.benefit.description|safe }}</td>
                <td><a href="{% url dashboard:offer-benefit offer.pk %}" class="btn">Edit</a></td>
            </tr>
            <tr>
                <th>{% trans "Condition" %}</th>
                <td>{{ offer.condition.description|safe }}</td>
                <td><a href="{% url dashboard:offer-condition offer.pk %}" class="btn">Edit</a></td>
            </tr>
            <tr>
                <th>{% trans "Restrictions" %}</th>
                <td>
                    {% for restriction in offer.availability_restrictions %}
                        {% if not restriction.is_satisfied %}
                        <span class="label label-important">
                            {{ restriction.description }}
                        </span><br/>
                        {% else %}
                            {{ restriction.description }}</br>
                        {% endif %}
                    {% endfor %}
                </td>
                <td><a href="{% url dashboard:offer-restrictions offer.pk %}" class="btn">Edit</a></td>
            </tr>
        </tbody>
    </table>

    {% if order_discounts %}
    <div class="table-header">
        <a class="pull-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 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 'partials/pagination.html' %}
    {% endif %}

{% endblock dashboard_content %}
