{% import "backend/macros.html" as macros %}
{% extends "backend/custom/surround.html" %}

{% block content %}
    {% if not origin %}
        {% set origin = False %}
    {% endif %}
            {% if record %}
                {% set stopUrl = url_for('.view_record', record_id=record.id, cmd="stop") %}
            {% else %}
                {% set stopUrl = url_for('.list_record') %}
            {% endif %}
            <div class="mb-4">
                <h1 class="fs-3 fw-semibold text-secondary">Usuario: {% if record %}{{ record.__repr__() }}{% endif %}</h1>
                <div class="d-flex align-items-center gap-2 mt-3">
                    {% if backlink %}
                        <a href="{{ backlink }}" class="btn btn-sm btn-secondary">
                            <span class="material-icons align-middle">arrow_back</span>
                        </a>
                    {% endif %}
                    {% if record %}
                        {% if not editable %}
                        <!-- Botón para editar -->
                        <a href="{{ url_for('.edit_record', record_id=record.id) }}" class="btn btn-sm btn-secondary"><span class="material-icons align-middle">edit</span>Editar</a>
                        {%else%}
                        <a href="{{ stopUrl }}" class="btn btn-sm btn-secondary"><span class="material-icons align-middle">stop</span>Detener</a>
                        <button type="submit" class="btn btn-sm btn-secondary" form="form-record"><span class="material-icons align-middle">save</span>Guardar</button>
                        {%endif%}
                        {% if current_user.usergroup.id == 1 or current_user.usergroup.id == 2%}
                        <!-- Botón para borrar -->
                        <form action="{{ url_for('.delete_record', record_id=record.id) }}" id="delete-form" method="post"
                            style="display: inline;"><input type="hidden" id="csrf_token" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-sm btn-secondary"
                                onclick="return confirm('¿Estás seguro de que quieres eliminar este registro?');"><span class="material-icons align-middle">delete</span>Borrar</button>
                        </form>
                        {%endif%}
                    {%else%}
                        {% if editable %}
                        <button type="submit" class="btn btn-sm btn-secondary" form="form-record"><span class="material-icons align-middle">save</span>Guardar</button>

                        <a href="{{ stopUrl }}" class="btn btn-sm btn-secondary">
                            <span class="material-icons align-middle">stop</span>Detener
                        </a>
                        {% endif %}
                    {% endif %}

                </div>
            </div>
            {% if editable %}
            <form method="post" enctype="multipart/form-data" id="form-record">
            <!-- Inputs ocultos -->
            <input type="hidden" id="csrf_token" name="csrf_token" value="{{ csrf_token() }}">
            {% if record %}
                <input type="hidden" id="modifiedby_id" name="modifiedby_id" value="{{ current_user.id }}" >
            {%else%}
                <input type="hidden" id="createdby_id" name="createdby_id" value="{{ current_user.id }}" >
            {% endif %}
            {% endif %}
            <div class="bg-white shadow-sm rounded-3 p-4">
                <!-- Aquí va el contenido -->
                <div class="card mb-4">
                    <div class="card-header bg-light fw-semibold">
                        Datos de Usuario
                    </div>
                    <div class="card-body">
                        {{ macros.fieldForm(fields["email"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["image"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["name"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["lastname"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["description"],record,editable,origin)}}
                    </div>
                </div>
                <!-- Aquí va el contenido -->
                <div class="card mb-4">
                    <div class="card-header bg-light fw-semibold">
                        Información de Contacto
                    </div>
                    <div class="card-body">
                        {{ macros.fieldForm(fields["birthdate"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["phone"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["mobile"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["address"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["zipcode"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["city"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["state"],record,editable,origin)}}
                        {{ macros.fieldForm(fields["country"],record,editable,origin)}}
                    </div>
                </div>
                <!-- Aquí va el contenido -->
                <div class="card mb-4">
                    <div class="card-header bg-light fw-semibold">
                        Modificar contraseña
                    </div>
                    <div class="card-body">
                        <label for="password" class="form-label">Contraseña</label>
                        <input class="form-control" placeholder="(Dejar en blanco si no quiere cambiarla)" type="password" id="password" name="password">
                    </div>
                </div>
            </div>
            {%if editable %}
            </form>
            {% endif %}
{% endblock %}