{% extends 'base.html' %}

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

{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md">
            <h2>Distributors</h2>
            <form action="" method="get">
                <div class="input-group mb-3">
                    <input class="form-control" name="search" type="search" placeholder="Search Distributor..." {% 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-distri-modal"><i class="bi bi-plus-circle"></i> Add Distributor</button>                </div>
            </form>
            <div class="mb-3">
            {% include 'paginator.html' with paginator=distributors get_param='page' aria_label='Distributor Page Navigation' %}
            </div>
            <div class="list-group mb-3">
                {% for distri in distributors %}
                <a href="{% url 'parts-distributors-detail' uuid=distri.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 distri.image %}
                                <img src="{{distri.image.url}}" class="component-img-small" alt="{{ distri.name }}" class="mr-3">
                            {% else %}
                                <img src="{% static 'css/icons/card-image.svg' %}" class="component-img-def-small" alt="{{ distri.name }}" class="mr-3">
                            {% endif %}
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <h6 class="mt-0 text-primary">{{ distri.name }}</h6>
                            {{distri.website|default_if_none:"No website specified"}}
                        </div>
                    </li>
                </a>
                {% endfor %}
            </div>
            {% include 'paginator.html' with paginator=distributors get_param='page' aria_label='Distributor Page Navigation' %}
        </div>
    </div>
</div>

<div class="modal fade" id="add-distri-modal" tabindex="-1" aria-labelledby="Add Distributor Modal" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Add New Distributor</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_distri_form|crispy}}
        </div>
        <div class="modal-footer">
            <input type="submit" class="btn btn-primary" name="submit-distri-add-new" value="Add Distributor">
        </div>
      </form>
    </div>
  </div>
</div>

{% endblock content %}

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