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

{% block extra_controls %}
  {% if not object.is_active %}
    {% if object.ready %}
      <a href="?_branch={{ object.schema_id }}" class="btn btn-green" role="button">
        <i class="mdi mdi-check-bold"></i> {% trans "Activate" %}
      </a>
    {% else %}
      <a href="?_branch={{ object.schema_id }}" class="btn btn-green disabled" role="button" aria-disabled="true">
        <i class="mdi mdi-check-bold"></i> {% trans "Activate" %}
      </a>
    {% endif %}
  {% elif object.is_active %}
    <a href="?_branch=" class="btn btn-danger">
      <i class="mdi mdi-close-thick"></i> {% trans "Deactivate" %}
    </a>
  {% endif %}
  {% if object.ready %}
    {% branch_sync_button object %}
    {% branch_merge_button object %}
  {% endif %}
  {% if object.merged %}
    {% branch_revert_button object %}
    {% branch_archive_button object %}
  {% endif %}
{% endblock %}

{% block content %}
  {% if last_job.status == 'errored' %}
    <div class="alert alert-danger" role="alert">
      <h4 class="alert-title">
        <i class="mdi mdi-alert"></i>
        The <a href="{{ last_job.get_absolute_url }}">last job</a> ({{ last_job.name }}) failed with an error:
      </h4>
      <div class="text-secondary">{{ last_job.error }}</div>
    </div>
  {% endif %}
  {% if object.pending_migrations %}
    <div class="alert alert-warning d-flex justify-content-between align-items-center" role="alert">
      <i class="mdi mdi-alert fs-1 text-warning"></i>
      <div class="flex-grow-1">
        <h4 class="alert-title text-warning mb-0">
          {% with count=object.pending_migrations|length %}
            {% blocktrans %}{{ count }} pending migrations detected{% endblocktrans %}
          {% endwith %}
        </h4>
        {% blocktrans %}The branch cannot be used until the migrations have been applied.{% endblocktrans %}
      </div>
      {% branch_migrate_button object %}
    </div>
  {% endif %}
  <div class="row">
	  <div class="col col-md-6">
      <div class="card">
        <h5 class="card-header">{% trans "Branch" %}</h5>
        <table class="table table-hover attr-table">
          <tr>
            <th scope="row">{% trans "Name" %}</th>
            <td>{{ object.name }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Schema ID" %}</th>
            <td>{{ object.schema_id }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Status" %}</th>
            <td class="d-flex justify-content-between">
              {% if object.is_active %}
                {% trans "Active" as status %}
                {% badge status "blue" %}
              {% else %}
                {% badge object.get_status_display object.get_status_color %}
              {% endif %}
              {% if last_job.status == 'failed' %}
                <span class="text-red">
                  <i class="mdi mdi-alert"></i>
                  <a href="{{ last_job.get_absolute_url }}" class="text-red">{% trans "Job failed" %}</a>
                </span>
              {% endif %}
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Owner" %}</th>
            <td>{{ object.owner }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Description" %}</th>
            <td>{{ object.description|placeholder }}</td>
          </tr>
          <tr>
            <th scope="row">{% trans "Last synced" %}</th>
            <td>
              {% if object.last_sync %}
                {{ object.last_sync|isodatetime }}
                {% if object.is_stale %}
                  <span class="text-danger" title="{% trans "Branch is stale and can no longer be synced" %}">
                    <i class="mdi mdi-alert-circle"></i>
                  </span>
                {% endif %}
                <div class="small text-muted">{{ object.last_sync|timesince }} {% trans "ago" %}</div>
              {% else %}
                {{ ''|placeholder }}
              {% endif %}
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Last activity" %}</th>
            <td>
              {{ latest_change.time|isodatetime|placeholder }}
              {% if latest_change %}
                <div class="small text-muted">{{ latest_change.time|timesince }} {% trans "ago" %}</div>
              {% endif %}
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Conflicts" %}</th>
            <td>
              {% if not object.ready %}
                {{ ''|placeholder }}
              {% elif conflicts_count %}
                <span class="text-red"><i class="mdi mdi-alert"></i></span>
                <a href="{% url 'plugins:netbox_branching:branch_diff' pk=object.pk %}?has_conflicts=True" class="text-red">{{ conflicts_count }} {% trans "conflicts found" %}</a>
              {% else %}
                <span class="text-green">
                  <i class="mdi mdi-check-bold"></i>
                  {% trans "None found" %}
                </span>
              {% endif %}
            </td>
          </tr>
          <tr>
            <th scope="row">{% trans "Database schema" %}</th>
            <td class="font-monospace">{{ object.schema_name }}</td>
          </tr>
        </table>
      </div>
      {% include 'inc/panels/tags.html' %}
      {% include 'inc/panels/custom_fields.html' %}
      {% plugin_left_page object %}
    </div>
    <div class="col col-md-6">
      {% include 'inc/panels/comments.html' %}
      <div class="card">
        <h5 class="card-header">{% trans "Event History" %}</h5>
        <div class="list-group list-group-flush">
          {% for event in object.get_event_history %}
            <div class="list-group-item">
              <div class="row align-items-center">
                {% if event.pk %}
                  {# Event object #}
                  <div class="col-3 text-end">
                    {% badge event.get_type_display bg_color=event.get_type_color %}
                  </div>
                  <div class="col text-truncate">
                    <span>{{ event.get_type_display }}{% if event.user %} by <strong>{{ event.user }}</strong>{% endif %}</span>
                    <small class="d-block text-muted">{{ event.time|isodatetime }}</small>
                  </div>
                {% else %}
                  {# Change summary #}
                  <div class="col offset-3 text-truncate">
                    <a href="{% url 'plugins:netbox_branching:branch_changes-ahead' pk=object.pk %}?time_after={{ event.start.isoformat }}&time_before={{ event.end.isoformat }}" class="text-muted">
                      {{ event.count }} {% trans "changes" %}
                    </a>
                  </div>
                {% endif %}
              </div>
            </div>
          {% empty %}
            <div class="list-group-item text-muted">{% trans "None" %}</div>
          {% endfor %}
        </div>
      </div>
      {% plugin_right_page object %}
    </div>
  </div>
  <div class="row">
    <div class="col col-4">
      <div class="card border-green">
        <h5 class="card-header text-green">{% trans "Created" %}</h5>
        <ul class="list-group list-group-flush">
          {% for ct, count in stats.created.items %}
            <a href="{% url 'plugins:netbox_branching:branch_changes-ahead' pk=object.pk %}?action=create&changed_object_type_id={{ ct.pk }}" class="list-group-item list-group-item-action d-flex justify-content-between">
              {{ ct.model_class|meta:'verbose_name'|bettertitle }}
              <span class="badge">{{ count }}</span>
            </a>
          {% empty %}
            <li class="list-group-item text-muted">{% trans "No changes" %}</li>
          {% endfor %}
        </ul>
      </div>
    </div>
    <div class="col col-4">
      <div class="card border-yellow">
        <h5 class="card-header text-yellow">{% trans "Updated" %}</h5>
        <ul class="list-group list-group-flush">
          {% for ct, count in stats.updated.items %}
            <a href="{% url 'plugins:netbox_branching:branch_changes-ahead' pk=object.pk %}?action=update&changed_object_type_id={{ ct.pk }}" class="list-group-item list-group-item-action d-flex justify-content-between">
              {{ ct.model_class|meta:"verbose_name_plural"|bettertitle }}
              <span class="badge">{{ count }}</span>
            </a>
          {% empty %}
            <li class="list-group-item text-muted">{% trans "No changes" %}</li>
          {% endfor %}
        </ul>
      </div>
    </div>
    <div class="col col-4">
      <div class="card border-red">
        <h5 class="card-header text-red">{% trans "Deleted" %}</h5>
        <ul class="list-group list-group-flush">
          {% for ct, count in stats.deleted.items %}
            <a href="{% url 'plugins:netbox_branching:branch_changes-ahead' pk=object.pk %}?action=delete&changed_object_type_id={{ ct.pk }}" class="list-group-item list-group-item-action d-flex justify-content-between">
              {{ ct.model_class|meta:"verbose_name_plural"|bettertitle }}
              <span class="badge">{{ count }}</span>
            </a>
          {% empty %}
            <li class="list-group-item text-muted">{% trans "No changes" %}</li>
          {% endfor %}
        </ul>
      </div>
    </div>
  </div>
  <div class="row mb-3">
    <div class="col col-md-12">
      {% plugin_full_width_page object %}
    </div>
  </div>
{% endblock %}
