{% extends 'base.html' %}

{% load static %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md">
            <h2>Packages</h2>
            <form action="" method="get">
                <div class="input-group mb-3">
                    <input class="form-control" name="search" type="search" placeholder="Search Package..." {% if search_string %}value="{{search_string}}"{% endif %}>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search"></i>
                    </button>
                    <button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#add-pkg-modal"><i class="bi bi-plus-circle"></i> Add Package</button>
                </div>
            </form>
            <div class="mb-3">
            {% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' %}
            </div>
            <div class="list-group mb-3">
                {% for pkg in packages %}
                <a href="{% url 'parts-packages-detail' uuid=pkg.id %}" class="text-decoration-none">
                    <li class="list-group-item list-group-item-action d-flex align-items-center">
                        <div class="flex-shrink-0">
                            {% if pkg.image %}
                                <img src="{{pkg.image.url}}" class="component-img-small" alt="{{ pkg.name }}" class="mr-3">
                            {% else %}
                                <img src="{% static 'css/icons/card-image.svg' %}" class="component-img-def-small" alt="{{ pkg.name }}" class="mr-3">
                            {% endif %}
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="mt-0 text-primary">{{ pkg.name }}</h6>
                            {% if pkg.smd %}
                            SMD Package<br>
                            {% endif %}
                            Pin Count: {{pkg.pin_count}}
                        </div>
                    </li>
                </a>
                {% endfor %}
            </div>
            {% include 'paginator.html' with paginator=packages get_param='page' aria_label='Package Page Navigation' %}
        </div>
    </div>
</div>

<div class="modal fade" id="add-pkg-modal" tabindex="-1" aria-labelledby="Add Package Modal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Add New Package</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form method="post" enctype="multipart/form-data">
      {% csrf_token %}
        <div class="modal-body">
            {{new_pkg_form|crispy}}
        </div>
        <div class="modal-footer">
            <input type="submit" class="btn btn-primary" name="submit-pkg-add-new" value="Add Package">
        </div>
      </form>
    </div>
  </div>
</div>

{% endblock content %}

{% block custom_scripts %}
<script type="text/javascript">
{% if new_pkg_form.errors %}
bootstrap.Modal.getOrCreateInstance(document.getElementById('add-pkg-modal')).show()
{% endif %}
</script>
{% endblock custom_scripts %}