{% extends 'generic/object.html' %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
{% load ipfabric_netbox_helpers %}
{% load perms %}
{% load plugins %}
{% load tabs %}
{% load i18n %}

{% block title %}{{ object }}{% endblock %}

{% block control-buttons %}
{# Clone/Edit/Delete Buttons #}
<div class="controls">
  <div class="control-group">
    {% custom_links object %}
  </div>
  <div id="ingestion_merge_button">
    {% include 'ipfabric_netbox/partials/ingestion_merge_button.html' with object=object %}
  </div>
</div>
{% endblock control-buttons %}

{% block tabs %}
  <div id="object_tabs">
    {% include 'ipfabric_netbox/partials/object_tabs.html' with object=object %}
  </div>
{% endblock tabs %}

{% block content %}
<div class="row mb-3">
  <div class="col col-md-6">
    <div class="card">
      <h5 class="card-header">{% trans "Ingestion Information" %}</h5>
      <div class="card-body">
        <table class="table table-hover attr-table">
          <tr>
            <th scope="row">{% trans "Name" %}</th>
            <td>{{ object.name }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Description" %}</th>
            <td>{{ object.description|placeholder }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Sync Object" %}</th>
            <td><a href="{% url 'plugins:ipfabric_netbox:ipfabricsync' pk=object.sync.pk %}">{{ object.sync.name}}</a>
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Branch Object" %}</th>
            <td>{% if object.branch %}<a href="{% url 'plugins:netbox_branching:branch' pk=object.branch.pk %}">{{ object.branch.name}}</a>{% else %}{{ ""|placeholder }}{% endif %}
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Job Object" %}</th>
            <td>{% if object.job %}<a href="{% url 'core:job' pk=object.job.pk %}">{{ object.job }}</a>{% else %}{{ ""|placeholder }}{% endif %}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Merge Job Object" %}</th>
            <td>{% if object.merge_job %}<a href="{% url 'core:job' pk=object.merge_job.pk %}">{{ object.merge_job }}</a>{% else %}{{ ""|placeholder }}{% endif %}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Ingestion Status" %}</th>
              <td><div id="ingestion_status">{% include 'ipfabric_netbox/partials/ingestion_status.html' with object=object %}</div></td>
              <!-- <td >{% badge object.sync.get_status_display bg_color=object.sync.get_status_color %}</td> -->
          </tr>
          <tr>
            <th scope="row">{% trans "Snapshot" %}</th>
            <td><a
                href="{% url 'plugins:ipfabric_netbox:ipfabricsnapshot' pk=object.sync.snapshot_data.pk %}">{{object.sync.snapshot_data.name }}</a></td>
          </tr>
          <tr>
            <th scope="row">{% trans "Source" %}</th>
            <td><a
                href="{% url 'plugins:ipfabric_netbox:ipfabricsource' pk=object.sync.snapshot_data.source.pk %}">{{object.sync.snapshot_data.source.name }}</a></td>
          </tr>
        </table>
      </div>
    </div>
    {% plugin_left_page object %}
    <div id="ingestion_statistics">
      {% include 'ipfabric_netbox/partials/ingestion_statistics.html' with object=object %}
    </div>
  </div>
  <div class="col col-md-6">
    <div id="ingestion_progress">
      {% render_progress_card object.job statistics active_stage merge_job=merge_job merge_disabled=merge_disabled %}
    </div>
    <!-- {% include 'inc/panels/related_objects.html' %} -->
    {% include 'inc/panels/custom_fields.html' %}
    {% plugin_right_page object %}
  </div>
  <div class="row mb-3">
    <div id="ingestion_poll" class="col col-md-12"
      hx-get="{% url 'plugins:ipfabric_netbox:ipfabricingestion_logs' pk=object.pk %}?stage={{ active_stage|default:'sync' }}"
      hx-trigger="{% if job_running %}every 5s{% else %}none{% endif %}"
      hx-target="#ingestion_logs"
      hx-select="#ingestion_logs"
      hx-select-oob="#ingestion_status:innerHTML,#ingestion_progress:innerHTML,#ingestion_statistics:innerHTML,#object_tabs:innerHTML,#ingestion_merge_button:innerHTML"
      hx-swap="innerHTML">
    </div>
    <div id="ingestion_logs" class="col col-md-12">
      {% include 'ipfabric_netbox/partials/job_logs.html' with job=object.job merge_job=merge_job job_results=job_results merge_job_results=merge_job_results active_stage=active_stage %}
    </div>
  </div>
</div>
{% endblock content %}

{% block modals %}
{% include 'inc/htmx_modal.html' with size='lg' %}
{% endblock modals %}
