{% extends "base.html" %}
{% load static %}
{% load formatstring %}

{% block title %}{{ item }}{% endblock %}

{% block header_bar %}
    <a href="{{ item.container_url }}"><img class="icon" src="{% static "inventory/img/back.svg" %}"></a>
    {{ item.name }}
    <span class="small">{{ item.description }}{% if item.form_factor %}, {{ item.form_factor }}{% endif %}</span></h2>
{% endblock %}

{% block header_icons %}
    {% if user.is_staff %}
        <li>
            <a href="{% url "admin:inventory_item_change" object_id=item.pk %}"><img class="icon" src="{% static "inventory/img/edit.svg" %}"></a>
        </li>
    {% endif %}
{% endblock %}

{% block content %}
    <table class="attribute-list">
        <tbody>
            <tr>
                <th>Name</th>
                <td>{{ item.name }}</td>
            </tr>
            <tr>
                <th>Description</th>
                <td>{{ item.description }}</td>
            </tr>
            <tr>
                <th>Tags</th>
                <td>
                    <ul class="tag-list">
                        {% for tag in item.all_tags %}
                            <li><a href="{% url 'tag-detail' tag.id %}" title="{{ tag.name }}">{{ tag.name }}</a></li>
                        {% empty %}
                            No tags
                        {% endfor %}
                    </ul>
                </td>
            </tr>

        {% if item.form_factor %}
            <tr>
                <th>Form factor</th>
                <td>
                    {% if item.form_factor.datasheet %}
                        <a href="{% url 'distributor-detail' item.distributor.id %}">
                            {% if item.form_factor.icon %}<img src="{{ item.form_factor.icon.url }}" class="icon">{% endif %}
                            {{ item.form_factor.name }}
                        </a>
                    {% else %}
                        {% if item.form_factor.icon %}<img src="{{ item.form_factor.icon.url }}" class="icon">{% endif %}
                        {{ item.form_factor.name }}
                    {% endif %}
                </td>
            </tr>
        {% endif %}

        {% if item.manufacturer %}
            <tr>
                <th>Manufacturer</th>
                <td>
                    <a href="{% url 'manufacturer-detail' item.manufacturer.id %}">
                        {% if item.manufacturer.icon %}<img src="{{ item.manufacturer.icon.url }}" class="icon">{% endif %}{{ item.manufacturer.name }}
                    </a>
                </td>
            </tr>
        {% endif %}

        {% if item.distributor %}
            <tr>
                <th>Distributor</th>
                <td>
                    <a href="{% url 'distributor-detail' item.distributor.id %}">
                        {% if item.distributor.icon %}<img src="{{ item.distributor.icon.url }}" class="icon">{% endif %}{{ item.distributor.name }}
                    </a>
                </td>
            </tr>
        {% endif %}

        {% if item.price %}
            <tr>
                <th>Price</th>
                <td>{{ item.price }} Euro</td>
            </tr>
        {% endif %}

        {% if item.last_ordered_on %}
            <tr>
                <th>Last ordered</th>
                <td>{{ item.last_ordered_on }}</td>
            </tr>
        {% endif %}

        {% if item.distributor_item_no %}
            <tr>
                <th>Link</th>
                <td>
                    <a href="{% formatstring item.distributor.search_link item.distributor_item_no %}">{% formatstring item.distributor.search_link item.distributor_item_no %}</a>
                </td>
            </tr>
        {% endif %}

            <tr><th>Created at</th><td>{{ item.created_at }}</td></tr>
        {% if item.created_at != item.changed_at %}
            <tr><th>Last change</th><td>{{ item.changed_at }}</td></tr>
        {% endif %}

        {% if item.documentation.exists %}
            <tr>
                <th>Datasheets</th>
                <td>
                    <ul>
                    {% for doc in item.documentation.all %}
                        <li><a href="{{ doc.file.url }}">{{ doc.file.name }}</a></li>
                    {% endfor %}
                    </ul>
                </td>
            </tr>
        {% endif %}
        </tbody>
    </table>
{% endblock %}