{% extends "backend/template.html" %}

{% block content %}
{% with messages = get_flashed_messages() %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endwith%}

<div class="container mt-3">
    <!-- Botón para regresar al listado -->
    {% if backlink %}
        <a href="{{ backlink }}" class="btn btn-secondary">Regresar</a>
    {% else %}
        <button onclick="window.history.back();" class="btn btn-secondary">Regresar</button>
    {% endif %}
    <!-- Botón para editar -->
    <a href="{{ url_for('.edit_record', record_id=institution.id, classid=classid, backlink=backlink) }}" class="btn btn-primary">Editar</a>
    <!-- Botón para borrar -->
    <form action="{{ url_for('.delete_record', record_id=institution.id, classid=classid,backlink=backlink) }}" method="post"
        style="display: inline;"><input type="hidden" id="csrf_token" name="csrf_token" value="{{ csrf_token() }}">
        <button type="submit" class="btn btn-danger"
            onclick="return confirm('¿Estás seguro de que quieres eliminar esta institución?');">Borrar</button>
    </form>

    <h2>{{ classname }}: {{institution.id}}</h2>
    <div class="card">
        <div class="card-body">
            <div class="row">
                {% for container_name, container_data in containers.items() %}
                <div class="{{ container_data.class }}">
                    {% if container_data.title %}<h3>{{ container_data.title }}</h3>{% endif %}
                    {% for key, value in container_data.fields.items() %}
                    {% set field_value = institution|safe_getattr(value.id) %}
                    <div class="{{ value.class }}">
                        <p>
                            <b>{{ value.label }}:</b>
                            {% if value.input == "checkbox" and value.type == "boolean" %}
                                {% if field_value %} Sí {% else %} No {% endif %}
                            {% elif value.input == "checkbox" and value.type != "boolean" %}
                                {% for option in value.options %}
                                    {% if option.value in field_value %} <br>{{ option.label }} {% endif %}
                                {% endfor %}
                            {% elif value.input == "integer" or value.input == "float" %}
                                {{ field_value }}
                            {% elif value.input == "textarea" %}
                                {{ field_value }}
                            {% elif value.input == "select" %}
                                {% for option in value.options %}
                                    {% if option.value == field_value %} {{ option.label }} {% endif %}
                                {% endfor %}
                            {% elif value.input == "image" %}
                            <img src="{{ url_for('static', filename='uploads/' ~ field_value) }}" width="200" />
                            {% else %}
                            {{ field_value }}
                            {% endif %}
                        </p>
                    </div>
                    {% endfor %}
                </div>
                {% endfor %}
            </div>
        </div>
    </div>

    <div class="card my-3">
        <div class="card-body">
            <h5 class="card-title">All Fields</h5>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Nombre</th>
                        <th>Etiqueta</th>
                        <th>Tipo DB</th>
                        <th>Input</th>
                        <th>Archivo</th>
                        <th>Función</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    {% for relevant in clazz_fields %}
                    <tr>
                        <td>{{ relevant.name }}</td>
                        <td>{{ relevant.label }}</td>
                        <td>{{ relevant.type }}</td>
                        <td>{{ relevant.input }}</td>
                        <td>{{ relevant.calculate_file }}</td>
                        <td>{{ relevant.calculate_function }}</td>
                        <td>
                            <a href="{{ url_for('field.view_record', record_id=relevant.id,backlink=url_for('.view_record', record_id=institution.id, classid=classid)) }}" class="btn btn-primary">Editar</a>
                        </td>
                    </tr>
                    {% endfor %}
                    <tfoot>
                        <tr>
                            <td><a href="{{ url_for('field.create_record', parent=institution.id, backlink=url_for('.view_record', record_id=institution.id, classid=classid)) }}" class="btn btn-success mb-3">Crear Field</a></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </tbody>
            </table>
        </div>
    </div>
    <div class="card my-3">
        <div class="card-body">
            <h5 class="card-title">Relevancias</h5>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Nombre</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    {% for relevant in clazz_relevants %}
                    <tr>
                        <td>{{ relevant.name }}</td>
                        <td>
                            <a href="{{ url_for('relevant.view_record', record_id=relevant.id, backlink=url_for('.view_record', record_id=institution.id, classid=classid)) }}" class="btn btn-primary">Editar</a>
                        </td>
                    </tr>
                    {% endfor %}
                    <tfoot>
                        <tr>
                            <td><a href="{{ url_for('relevant.create_record',parent=institution.id, backlink=url_for('.view_record', record_id=institution.id, classid=classid)) }}" class="btn btn-success mb-3">Crear Relevancia</a></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </tbody>
            </table>
        </div>
    </div>

</div>
{% endblock %}