{% extends 'base/layout.html' %}
{% load buttons %}
{% load helpers %}
{% load perms %}

{% block title %}Ingestion{% endblock %}

{% block tabs %}
  <ul class="nav nav-tabs px-3">
    <li class="nav-item" role="presentation">
      <a class="nav-link active" role="tab">Ingestion</a>
    </li>
  </ul>
{% endblock tabs %}

{% block controls %}
  <div class="controls">
    <div class="control-group">
      {% block extra_controls %}{% endblock %}
      {% add_button model %}
    </div>
  </div>
{% endblock controls %}

{% block content-wrapper %}
  <div class="tab-content">
    {% for sync in syncs %}
      <div class="card">
        <h5 class="card-header" id="module{{ module.pk }}">
          <!-- TODO: Cannot delete without serializer https://github.com/netbox-community/netbox/issues/13640-->
          <!-- {% if perms.ipfabric_netbox.delete_ipfabricsync %}
            <div class="float-end">
              <a href="{% url 'plugins:ipfabric_netbox:ipfabricsync_delete' pk=sync.pk %}" class="btn btn-danger btn-sm">
                <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> Delete
              </a>
            </div>
          {% endif %} -->
          <i class="mdi mdi-cloud-sync"></i><i class="mdi mdi-sync"></i> <a href="{% url 'plugins:ipfabric_netbox:ipfabricsync' pk=sync.pk %}">{{ sync.name}}</a>

        </h5>
        <div class="card-body">
          {% include 'inc/sync_warning.html' with object=module %}
          <table class="table table-hover table-headings reports">
            <thead>
              <tr>
                <th width="200">Source</th>
                <th width="400">Snapshot</th>
                <th>Status</th>
                <th>Last Run</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td><a href="{% url 'plugins:ipfabric_netbox:ipfabricsource' pk=sync.snapshot_data.source.pk %}">{{ sync.snapshot_data.source.name }}</a></td>
                <td><a href="{% url 'plugins:ipfabric_netbox:ipfabricsnapshot' pk=sync.snapshot_data.pk %}">{{ sync.snapshot_data.name}}</a></td>
                <td>{% badge sync.get_status_display last_job.get_status_color %}</td>
                <td>{{sync.last_synced}}</td>
              </tr>
            </tbody>
            <!-- <tbody>
              {% with jobs=module.get_latest_jobs %}
                {% for report_name, report in module.reports.items %}
                  {% with last_job=jobs|get_key:report.name %}
                    <tr>
                      <td>
                        <a href="{% url 'extras:report' module=module.python_name name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>
                      </td>
                      <td>{{ report.description|markdown|placeholder }}</td>
                      {% if last_job %}
                        <td>
                          <a href="{% url 'extras:report_result' job_pk=last_job.pk %}">{{ last_job.created|annotated_date }}</a>
                        </td>
                        <td>
                          {% badge last_job.get_status_display last_job.get_status_color %}
                        </td>
                      {% else %}
                        <td class="text-muted">Never</td>
                        <td>{{ ''|placeholder }}</td>
                      {% endif %}
                      <td>
                        {% if perms.extras.run_report %}
                          <div class="float-end noprint">
                            <form action="{% url 'extras:report' module=report.module name=report.class_name %}" method="post">
                              {% csrf_token %}
                              <button type="submit" name="_run" class="btn btn-primary btn-sm" style="width: 110px">
                                {% if last_job %}
                                  <i class="mdi mdi-replay"></i> Run Again
                                {% else %}
                                  <i class="mdi mdi-play"></i> Run Report
                                {% endif %}
                              </button>
                            </form>
                          </div>
                        {% endif %}
                      </td>
                    </tr>
                    {% for method, stats in last_job.data.items %}
                      <tr>
                        <td colspan="4" class="method">
                          <span class="ps-3">{{ method }}</span>
                        </td>
                        <td class="text-end text-nowrap report-stats">
                          <span class="badge bg-success">{{ stats.success }}</span>
                          <span class="badge bg-info">{{ stats.info }}</span>
                          <span class="badge bg-warning">{{ stats.warning }}</span>
                          <span class="badge bg-danger">{{ stats.failure }}</span>
                        </td>
                      </tr>
                    {% endfor %}
                  {% endwith %}
                {% endfor %}
              {% endwith %}
            </tbody> -->
          </table>
        </div>
      </div>
    {% empty %}
      <div class="alert alert-info" role="alert">
        <h4 class="alert-heading">Injest Jobs Settings Found</h4>
        {% if perms.extras.add_reportmodule %}
          Get started by <a href="{% url 'plugins:ipfabric_netbox:ipfabricsync_add' %}">creating an injestion</a> from an IP Fabric source.
        {% endif %}
      </div>
    {% endfor %}
  </div>
{% endblock content-wrapper %}
