{% extends 'generic/object.html' %}
{% load static %}
{% load helpers %}
{% load plugins %}
{% load render_table from django_tables2 %}
{% load perms %}

{% block extra_controls %}
{% if perms.ipfabric_netbox.sync_source %}
{% if object.ready_for_sync and object.type == 'local' %}
<form action="{% url 'plugins:ipfabric_netbox:ipfabricsource_sync' pk=object.pk %}" method="post">
  {% csrf_token %}
  <button type="submit" class="btn btn-sm btn-primary">
    <i class="mdi mdi-sync" aria-hidden="true"></i> Sync
  </button>
</form>
{% else %}
<span class="inline-block" tabindex="0" data-bs-toggle="tooltip" data-bs-delay="100" data-bs-placement="bottom">
  <button class="btn btn-sm btn-primary" disabled>
    <i class="mdi mdi-sync" aria-hidden="true"></i> Sync
  </button>
</span>
{% endif %}
{% endif %}
{% endblock %}

{% block content %}
<div class="row mb-3">
  <div class="col col-md-6">
    <div class="card">
      <h5 class="card-header">Data Source</h5>
      <div class="card-body">
        <table class="table table-hover attr-table">
          <tr>
            <th scope="row">Name</th>
            <td>{{ object.name }}</td>
          </tr>
          <tr>
            <th scope="row">Type</th>
            <td>{{ object.get_type_display }}</td>
          </tr>
          <tr>
            <th scope="row">Status</th>
            <td>{% badge object.get_status_display bg_color=object.get_status_color %}</td>
          </tr>
          <tr>
            <th scope="row">Last synced</th>
            <td>{{ object.last_synced|placeholder }}</td>
          </tr>
          <tr>
            <th scope="row">Description</th>
            <td>{{ object.description|placeholder }}</td>
          </tr>
          <tr>
            <th scope="row">URL</th>
            <td>
              <a href="{{ object.url }}">{{ object.url }}</a>
            </td>
          </tr>
        </table>
      </div>
    </div>
    {% include 'inc/panels/tags.html' %}
    {% include 'inc/panels/comments.html' %}
    {% plugin_left_page object %}
  </div>
  <div class="col col-md-6">
    <div class="card">
      <h5 class="card-header">Parameters</h5>
      <div class="card-body">
        <table class="table table-hover attr-table">
          {% for name, value in object.parameters.items %}
          <tr>
            <th scope="row">{{ name | capfirst}}</th>
            {% if name == "auth" and object.parameters|get_key:name %}
            <td>
              {% if request.user|can_add:object or request.user|can_change:object or request.user|can_delete:object %}
              <span id="secret" class="font-monospace" data-secret="{{ object.parameters|get_key:name }}">{{
                object.parameters|get_key:name|placeholder }}</span>
              <button type="button" class="btn btn-sm btn-primary toggle-secret float-end" data-bs-toggle="button">Show
                Secret</button>
              {% else %}
              <i>Password Hidden</i>
              {% endif %}
            </td>
            {% else %}
            <td>{{ object.parameters|get_key:name|placeholder }}</td>
            {% endif %}
          </tr>
          {% empty %}
          <tr>
            <td colspan="2" class="text-muted">
              No parameters defined
            </td>
          </tr>
          {% endfor %}
        </table>
      </div>
    </div>
    {% include 'inc/panels/related_objects.html' %}
    {% include 'inc/panels/custom_fields.html' %}
    {% plugin_right_page object %}
  </div>
  {% if job %}
  <div class="col col-md-12" {% if not object.job.completed %} hx-trigger="every 5s" hx-target="#sync_logs"
    hx-select="#sync_logs" hx-select-oob="#sync_status:innerHTML" hx-swap="innerHTML" {% endif %}>
    <div id="sync_logs">{% include 'ipfabric_netbox/partials/job_logs.html' with job=job %}</div>
  </div>
  {% endif %}
</div>
{% endblock %}
