{% extends "base.html" %}
{% load core %}
{% load generic %}

{% block content %}
  {% if datamodel.graph.svg %}
    <div class="d-flex justify-content-center">{{ datamodel.graph.svg|safe }}</div>
    <hr>
  {% endif %}
  <div class="container">
    {% if datamodel.graph.error %}
      <div class="alert alert-warning" role="alert">{{ datamodel.graph.error }}</div>
    {% endif %}
    <h1>Entities</h1>
    {% for entity in datamodel.entities %}
      <div class="border p-3 mb-3">
        <a id="entity-{{ entity.name }}">
          <h2>{{ entity.name|title }}</h2>
        </a>
        <h3>related via</h3>
        {% for relation in datamodel.relations %}
          {% with relation.model_class.subj_list|add:relation.model_class.obj_list as model_list %}
            <ul>
              {% if entity.model_class in model_list %}
                <li>
                  <a href="#relation-{{ relation.model }}">{{ relation.name }}</a>
                </li>
              {% endif %}
            </ul>
          {% endwith %}
        {% endfor %}
        <h3>attributes</h3>
        <table class="table table-hover">
          <thead>
            <tr>
              <th>Verbose name</th>
              <th>Help text</th>
              <th>Primary key</th>
              <th>Can be blank</th>
              <th>Can be null</th>
              <th>Choices</th>
              <th>Type</th>
            </tr>
          </thead>
          <tbody>
            {% get_model_fields entity.model_class False as entity_fields %}
            {% for field in entity_fields %}
              <tr>
                <td>{{ field.verbose_name }}</td>
                <td>
                  <small>{{ field.help_text }}</small>
                </td>
                <td>
                  <input type="checkbox" disabled {% if field.primary_key %}checked{% endif %} />
                </td>
                <td>
                  <input type="checkbox" disabled {% if field.blank %}checked{% endif %} />
                </td>
                <td>
                  <input type="checkbox" disabled {% if field.null %}checked{% endif %} />
                </td>
                <td>
                  {% for val, key in field.choices %}
                    {{ key }}
                    {% if not foloop.last %},{% endif %}
                  {% endfor %}
                </td>
                <td>{{ field.get_internal_type }}</td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    {% endfor %}
    <h1>Relations</h1>
    {% for relation in datamodel.relations %}
      <a id="relation-{{ relation.model }}">
        <h2>
          {{ relation.model_class.name }} <span class="material-symbols-outlined">width</span> {{ relation.model_class.reverse_name }}
        </h2>
        <table class="table">
          <thead>
            <tr>
              <th>Subjects</th>
              <th>Objects</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                {% for cls in relation.model_class.subj_list %}
                  {% with cls|contenttype as entity %}<a href="#entity-{{ entity.model }}">{{ entity.name }}</a>{% endwith %}
                {% endfor %}
              </td>
              <td>
                {% for cls in relation.model_class.obj_list %}
                  {% with cls|contenttype as entity %}<a href="#entity-{{ entity.model }}">{{ entity.name }}</a>{% endwith %}
                {% endfor %}
              </td>
            </tr>
          </tbody>
        </table>
      </a>
    {% endfor %}
  </div>
{% endblock %}
