{% extends "admin/admin_base.html" %}
{% load i18n %}

{% block admin_title %}{% trans "Recipes" %}{% endblock %}

{% block content %}
<div class="row align-center mb-2">
    <h5 class="max bold">{% trans "Recipe Management" %}</h5>
    <a href="{% url 'admin_recipe_add' %}" class="button primary round">
        <i>add</i>
        <span>{% trans "Add Recipe" %}</span>
    </a>
</div>

    <table class="border striped no-space">
        <thead>
            <tr>
                <th class="min">{% trans "ID" %}</th>
                <th class="min">{% trans "Image" %}</th>
                <th class="max">
                    <a href="?sort={% if current_sort == 'title' %}-title{% else %}title{% endif %}" class="row align-center">
                        {% trans "Title" %}
                        {% if current_sort == 'title' %}<i>arrow_upward</i>{% elif current_sort == '-title' %}<i>arrow_downward</i>{% endif %}
                    </a>
                </th>
                <th class="min">
                    <a href="?sort={% if current_sort == 'price' %}-price{% else %}price{% endif %}" class="row align-center">
                        {% trans "Price" %}
                        {% if current_sort == 'price' %}<i>arrow_upward</i>{% elif current_sort == '-price' %}<i>arrow_downward</i>{% endif %}
                    </a>
                </th>
                <th class="min">
                    <a href="?sort={% if current_sort == 'orders' %}-orders{% else %}orders{% endif %}" class="row align-center">
                        {% trans "Orders" %}
                        {% if current_sort == 'orders' %}<i>arrow_upward</i>{% elif current_sort == '-orders' %}<i>arrow_downward</i>{% endif %}
                    </a>
                </th>
                <th class="min">{% trans "Rating" %}</th>
                <th class="min">{% trans "Approved" %}</th>
                <th class="m l">{% trans "Tags" %}</th>
                <th>
                    <a href="?sort={% if current_sort == 'uploader' %}-uploader{% else %}uploader{% endif %}" class="row align-center">
                        {% trans "Uploader" %}
                        {% if current_sort == 'uploader' %}<i>arrow_upward</i>{% elif current_sort == '-uploader' %}<i>arrow_downward</i>{% endif %}
                    </a>
                </th>
                <th>
                    <a href="?sort={% if current_sort == 'created_at' %}-created_at{% else %}created_at{% endif %}" class="row align-center">
                        {% trans "Created" %}
                        {% if current_sort == 'created_at' %}<i>arrow_upward</i>{% elif current_sort == '-created_at' %}<i>arrow_downward</i>{% endif %}
                    </a>
                </th>
                <th class="right-align">{% trans "Actions" %}</th>
            </tr>
        </thead>
        <tbody>        {% for recipe in recipes %}
        <tr class="pointer" onclick="location.href='{% url 'admin_recipe_edit' recipe.pk %}'">
            <td class="min">{{ recipe.id }}</td>
            <td class="min">
                {% if recipe.image %}
                <img src="{{ recipe.image_thumbnail.url }}" class="admin-thumb round">
                {% else %}
                <div class="admin-thumb round gray1 middle-align center-align">
                    <i class="extra">restaurant</i>
                </div>
                {% endif %}
            </td>
            <td class="max">
                {% if recipe.is_highlighted %}
                <i class="tiny amber-text">star</i>
                {% endif %}
                <b>{{ recipe.title }}</b>
            </td>
            <td class="min no-wrap">
                {% if recipe.price %}
                {{ recipe.price }} €
                {% else %}
                -
                {% endif %}
            </td>
            <td class="min no-wrap">
                {{ recipe.daily_orders_count }} / {{ recipe.max_daily_orders|default:"∞" }}
            </td>
            <td class="min center-align">
                <div class="row align-center">
                    <i class="primary-text">star</i>
                    <span>{{ recipe.avg_rating|default:0|floatformat:1 }}</span>
                </div>
            </td>
            <td class="min center-align">
                {% if recipe.is_approved %}
                <i class="primary-text">check_circle</i>
                {% else %}
                <a href="{% url 'admin_recipe_approve' recipe.pk %}" class="button tiny primary round" onclick="event.stopPropagation();">
                    <i>check</i>
                    <span>{% trans "Approve" %}</span>
                </a>
                {% endif %}
            </td>
            <td class="m l">
                {% for tag in recipe.tags.all %}
                <span class="chip tiny">{{ tag.name }}</span>
                {% endfor %}
            </td>
            <td>{{ recipe.uploaded_by.username|default:"-" }}</td>
            <td>{{ recipe.created_at|date:"SHORT_DATE_FORMAT" }}</td>
            <td class="right-align">
                <a href="{% url 'admin_recipe_delete' recipe.pk %}" class="button circle transparent" onclick="event.stopPropagation();" title="{% trans 'Delete' %}"><i>delete</i></a>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}
