{% extends "main_card.html.twig" %}

{% import "info_providers/providers.macro.html.twig" as providers_macro %}
{% import "helper.twig" as helper %}

{% block title %}
    {% if update_target %}
        {% trans %}info_providers.update_part.title{% endtrans %}
    {% else %}
        {% trans %}info_providers.search.title{% endtrans %}
    {% endif %}
{% endblock %}

{% block card_title %}
    {% if update_target %} {# If update_target is set, we update an existing part #}
        <i class="fas fa-cloud-arrow-down"></i> {% trans %}info_providers.update_part.title{% endtrans %}:
        <b><a href="{{ entity_url(update_target) }}" target="_blank" class="text-bg-primary">{{ update_target.name }}</a></b>
    {% else %} {# Create a fresh part #}
        <i class="fas fa-cloud-arrow-down"></i> {% trans %}info_providers.search.title{% endtrans %}
    {% endif %}
{% endblock %}

{% block card_content %}

    {{ form_start(form) }}

    {{ form_row(form.keyword) }}
    {{ form_row(form.providers) }}

    <div class="row mb-2">
        <div class="col-sm-9 offset-sm-3">
            <a href="{{ path('info_providers_list') }}">{% trans %}info_providers.search.info_providers_list{% endtrans %}</a>
        </div>
    </div>

    {{ form_row(form.submit) }}

    {{ form_end(form) }}

    {% if results is not null %}
        <table class="table table-striped table-hover">
            <thead>
            <tr>
                <th></th>
                <th>{% trans %}name.label{% endtrans %} / {% trans %}part.table.mpn{% endtrans %}</th>
                <th>{% trans %}description.label{% endtrans %} / {% trans %}category.label{% endtrans %}</th>
                <th>{% trans %}manufacturer.label{% endtrans %} / {% trans %}footprint.label{% endtrans %}</th>
                <th>{% trans %}part.table.manufacturingStatus{% endtrans %}</th>
                <th>{% trans %}info_providers.table.provider.label{% endtrans %}</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            {% for result in results  %}
                <tr>
                    <td>
                        <img src="{{ result.preview_image_url }}" data-thumbnail="{{ result.preview_image_url }}"
                             class="hoverpic" style="max-width: 45px;" {{ stimulus_controller('elements/hoverpic') }}>
                    </td>
                    <td>
                        {% if result.provider_url is not null %}
                            <a href="{{ result.provider_url }}" target="_blank" rel="noopener">{{ result.name }}</a>
                        {% else %}
                            {{ result.name }}
                        {% endif %}

                        {% if result.mpn is not null %}
                            <br>
                            <small class="text-muted" title="{% trans %}part.table.mpn{% endtrans %}">{{ result.mpn }}</small>
                        {% endif %}
                    </td>
                    <td>
                        {{ result.description }}
                        {% if result.category is not null %}
                            <br>
                            <small class="text-muted">{{ result.category }}</small>
                        {% endif %}
                    </td>
                    <td>
                        {{ result.manufacturer ?? '' }}
                        {% if result.footprint is not null %}
                            <br>
                            <small class="text-muted">{{ result.footprint }}</small>
                        {% endif %}
                    </td>
                    <td>{{ helper.m_status_to_badge(result.manufacturing_status) }}</td>
                    <td>
                        {% if result.provider_url %}
                            <a href="{{ result.provider_url }}" target="_blank" rel="noopener">
                                {{ info_provider_label(result.provider_key)|default(result.provider_key) }}
                            </a>
                        {% else %}
                            {{ info_provider_label(result.provider_key)|default(result.provider_key) }}
                        {% endif %}
                        <br>
                        <small class="text-muted">{{ result.provider_id }}</small>
                    <td>
                        {% if update_target %} {# We update an existing part #}
                            {% set href = path('info_providers_update_part',
                                {'providerKey': result.provider_key, 'providerId': result.provider_id, 'id': update_target.iD}) %}
                        {% else %} {# Create a fresh part #}
                            {% set href = path('info_providers_create_part',
                                {'providerKey': result.provider_key, 'providerId': result.provider_id}) %}
                        {% endif %}

                        <a class="btn btn-primary" href="{{ href }}"
                           target="_blank" title="{% trans %}part.create.btn{% endtrans %}">
                            <i class="fa-solid fa-plus-square"></i>
                        </a>
                    </td>
                </tr>
            {% endfor %}

            </tbody>
        </table>
    {% endif %}

{% endblock %}
