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

{% block breadcrumbs %}
  {{ block.super }}
  {% for group in object.get_ancestors %}
    <li class="breadcrumb-item"><a href="{% url 'plugins:netbox_inventory:inventoryitemgroup' group.pk %}">{{ group }}</a></li>
  {% endfor %}
{% endblock %}

{% block extra_controls %}
  {% if perms.netbox_inventory.add_inventoryitemtype %}
    <a href="{% url 'plugins:netbox_inventory:inventoryitemtype_add' %}?inventoryitem_group={{ object.pk }}" class="btn btn-sm btn-primary">
      <i class="mdi mdi-plus-thick" aria-hidden="true"></i> Add inventory item type
    </a>
  {% endif %}
{% endblock extra_controls %}

{% block content %}
  <div class="row mb-3">
    <div class="col col-md-6">
      <div class="card">
        <h5 class="card-header">Inventory Item Group</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">Parent</th>
              <td>{{ object.parent|linkify|placeholder }}</td>
            </tr>
            <tr>
              <th scope="row">Assets</th>
              <td>
                <a href="{% url 'plugins:netbox_inventory:asset_list' %}?inventoryitem_group_id={{ object.pk }}">{{ asset_table.rows|length }}</a>
              </td>
            </tr>
          </table>
        </div>
      </div>
      {% include 'inc/panels/custom_fields.html' %}


        <div class="card">
        <h5 class="card-header">Asset count by status</h5>
        <div class="card-body">
          <table class="table table-hover object-list">
          <thead>
            <tr>
              <th>Status</th>
              <th>Count</th>
            </tr>
          </thead>
          <tbody>
            {% for sc in status_counts.values %}
            <tr>
              <td>{% badge value=sc.label bg_color=sc.color %}</td>
              <td>
                <a href="{% url 'plugins:netbox_inventory:asset_list' %}?inventoryitem_group_id={{ object.pk }}&status={{ sc.value }}">
                  {{ sc.count }}
                </a>
              </td>
            </tr>
            {% empty %}
            <tr><td class="text-center text-muted" colspan="2">— No assets found —</td></tr>
            {% endfor %}
          </tbody>
          </table>
        </div>
      </div>

      <div class="card">
        <h5 class="card-header">Asset count by type & status</h5>
        <div class="card-body">
          <table class="table table-hover object-list table-striped">
          <thead>
            <tr>
              <th>Inventory Item Type</th>
              <th>Status</th>
              <th>Count</th>
            </tr>
          </thead>
          <tbody>
            {% for tsc in type_status_counts %}
            <tr>
            {% ifchanged tsc.inventoryitem_type %}
              <td>
                <a href="{% url 'plugins:netbox_inventory:inventoryitemtype' tsc.inventoryitem_type %}">
                  {{ tsc.inventoryitem_type__manufacturer__name }} {{ tsc.inventoryitem_type__model }}
                </a>
              </td>
            {% else %}
              <td>&nbsp;</td>
            {% endifchanged %}
              <td>
                {% badge value=tsc.label bg_color=tsc.color %}
              </td>
              <td>
                <a href="{% url 'plugins:netbox_inventory:asset_list' %}?inventoryitem_type_id={{ tsc.inventoryitem_type }}&status={{ tsc.status }}">
                  {{ tsc.count }}
                </a>
              </td>
            </tr>
            {% empty %}
            <tr><td class="text-center text-muted" colspan="3">— No assets found —</td></tr>
            {% endfor %}
          </tbody>
          </table>
        </div>
      </div>
  
    </div>
    <div class="col col-md-6">
      <div class="card">
        <h5 class="card-header">
          Child Groups
        </h5>
        <div class="card-body table-responsive">
          {% render_table child_groups_table 'inc/table.html' %}
        </div>
        {% if perms.netbox_inventory.add_inventoryitemgroup %}
          <div class="card-footer text-end noprint">
            <a href="{% url 'plugins:netbox_inventory:inventoryitemgroup_add' %}?parent={{ object.pk }}" class="btn btn-sm btn-primary">
              <span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add Group
            </a>
          </div>
        {% endif %}
      </div>
      {% include 'inc/panels/tags.html' %}
      {% include 'inc/panels/comments.html' %}
    </div>
  </div>
  <div class="row mb-3">
    <div class="col col-md-12">
      <div class="card">
        <h5 class="card-header">Assets</h5>
        <div class="card-body table-responsive">
          {% render_table asset_table 'inc/table.html' %}
          {% include 'inc/paginator.html' with paginator=asset_table.paginator page=asset_table.page %}
        </div>
      </div>
    </div>
  </div>
{% endblock content %}
