{% extends 'dashboard/layout.html' %}
{% load i18n %}
{% load thumbnail %}
{% load staticfiles %}
{% load sorting_tags %}

{% block body_class %}{{ block.super }} catalogue{% endblock %}

{% block title %}
    {% trans "Product management" %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
    <ul class="breadcrumb">
        <li>
            <a href="{% url 'dashboard:index' %}">{% trans "Dashboard" %}</a>
            <span class="divider">/</span>
        </li>
        <li class="active">{% trans "Product management" %}</li>
    </ul>
{% endblock %}

{% block header %}
    <div class="page-header action">
        <h1>{% trans "Product management" %}</h1>
    </div>
{% endblock header %}

{% block dashboard_content %}
    {% block search_products %}
        <div class="table-header">
            <h3><i class="icon-search icon-large"></i>{% trans "Search Products" %}</h3>
        </div>
        <div class="well">
            <form action="." method="get" class="form-inline">
                {% comment %}
                    Add the current query string to the search form so that the
                    sort order is not reset when searching.
                {% endcomment %}
                {% for name, value in request.GET.items %}
                    {% if name not in form.fields %}
                        <input type="hidden" name="{{ name }}" value="{{ value }}"/>
                    {% endif %}
                {% endfor %}

                {% include "partials/form_fields_inline.html" with form=form %}
                <button type="submit" class="btn btn-primary">{% trans "Search" %}</button>
            </form>
        </div>
    {% endblock %}

    {% block create_product %}
        <div class="table-header">
            <h3><i class="icon-sitemap icon-large"></i> {% trans "Create new product" %}</h3>
        </div>
        <div class="well">
            <form action="{% url 'dashboard:catalogue-product-create' %}" method="get" class="form-inline">
                {% include "partials/form_fields_inline.html" with form=productclass_form %}
                <button class="btn btn-primary" type="submit"><i class="icon-plus"></i> {% trans "New Product" %}</button>
            </form>
        </div>
    {% endblock %}

    {% if products %}
        {% block product_list %}
            {% block tabs %}
                <ul class="nav nav-tabs">
                    <li{% if 'recently_edited' not in request.GET %} class="active"{% endif %}> <a href=".">{% trans "All" %}</a> </li>
                    <li{% if 'recently_edited' in request.GET %} class="active"{% endif %}> <a href="?recently_edited=1">{% trans "Recently edited" %}</a> </li>
                </ul>
            {% endblock %}
            <div class="table-header">
                <h2><i class="icon-sitemap icon-large"></i>{{ queryset_description }}</h2>
            </div>
            <form action="." method="post">
                {% csrf_token %}
                <table class="table table-striped table-bordered">
                    {% block product_list_header %}
                        <tr>
                            <th>{% if 'recently_edited' in request.GET %}{% trans "Title" %}
                                {% else %}{% anchor 'title' _("Title") %}{% endif %}</th>
                            <th>{% trans "UPC" %}</th>
                            <th>{% trans "Image" %}</th>
                            <th>{% trans "Product Type" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Parent" %}</th>
                            <th>{% trans "Children" %}</th>
                            <th>{% trans "Stock records" %}</th>
                            <th></th>
                        </tr>
                    {% endblock %}
                    {% for product in products %}
                        {% block product %}
                            <tr>
                                <td><a href="{% url 'dashboard:catalogue-product' pk=product.id %}">{{ product.get_title }}</a></td>
                                <td>{{ product.upc|default:"-" }}</td>
                                <td>
                                    {% if product.primary_image.original.url %}
                                        {% with image=product.primary_image %}
                                            {% thumbnail image.original "70x70" upscale=False as thumb %}
                                            <a href="{{ image.original.url }}" rel="lightbox_{{ product.upc|default:"-" }}" class="sub-image">
                                                <img src="{{ thumb.url }}" alt="{{ product.get_title }}" data-description="{% if image.caption %}{{ image.caption }}{% endif %}">
                                            </a>
                                            {% endthumbnail %}
                                        {% endwith %}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>{{ product.get_product_class.name }}</td>
                                <td>{{ product.status|default:"-" }}</td>
                                <td>
                                    {% if product.parent.id %}
                                        <a href="{% url 'dashboard:catalogue-product' pk=product.parent.id %}">{{ product.parent.title }}</a>
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>{{ product.variants.count }}</td>
                                <td>{{ product.stockrecords.count }}</td>
                                <td>
                                    <div class="btn-toolbar">
                                        <div class="btn-group">
                                            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                                                {% trans "Actions" %}
                                                <span class="caret"></span>
                                            </a>
                                            <ul class="dropdown-menu pull-right">
                                                <li>
                                                    <a href="{% url 'dashboard:catalogue-product' pk=product.id %}{% if request.GET.urlencode %}?{{ request.GET.urlencode }}{% endif %}">{% trans "Edit" %}</a>
                                                </li>
                                                <li> 
                                                    <a href="{{ product.get_absolute_url }}"> {% trans "View on site" %}</a>
                                                </li>
                                                <li>
                                                    <a href="{% url 'dashboard:catalogue-product-delete' pk=product.id %}">{% trans "Delete" %}</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        {% endblock product %}
                    {% endfor %}
                </table>
                {% include "partials/pagination.html" %}
            </form>
        {% endblock product_list %}
    {% else %}
        <p>{% trans "No products found." %}</p>
    {% endif %}

{% endblock dashboard_content %}
