{% extends 'base.html' %}

{% block title %}Mon Profil - Contrôle Technique{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="bi bi-person-circle"></i> Mon Profil</h5>
                </div>
                <div class="card-body text-center">
                    <img src="https://www.gravatar.com/avatar/{{ user.email|lower|md5 }}?s=200&d=identicon" 
                         class="rounded-circle img-fluid mb-3" style="width: 150px; height: 150px; object-fit: cover;">
                    <h4>{{ user.get_full_name|default:user.username }}</h4>
                    <p class="text-muted">
                        <i class="bi bi-envelope"></i> {{ user.email|default:"Email non renseigné" }}
                    </p>
                    <p>
                        <span class="badge bg-success">
                            <i class="bi bi-check-circle"></i> Compte actif
                        </span>
                    </p>
                    <p class="text-muted small">
                        <i class="bi bi-calendar"></i> Membre depuis {{ user.date_joined|date:"d/m/Y" }}
                    </p>
                </div>
                <div class="card-footer">
                    <div class="d-grid gap-2">
                        <a href="{% url 'core:change_password' %}" class="btn btn-warning">
                            <i class="bi bi-key"></i> Changer mon mot de passe
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-8">
            <div class="card mb-4">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="bi bi-pencil-square"></i> Modifier mes informations</h5>
                </div>
                <div class="card-body">
                    <form method="post" action="{% url 'core:profile' %}">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label for="username" class="form-label">Nom d'utilisateur</label>
                            <input type="text" class="form-control" id="username" value="{{ user.username }}" readonly disabled>
                            <small class="text-muted">Le nom d'utilisateur ne peut pas être modifié</small>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="first_name" class="form-label">Prénom</label>
                                <input type="text" class="form-control" id="first_name" name="first_name" 
                                       value="{{ user.first_name }}" placeholder="Votre prénom">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="last_name" class="form-label">Nom</label>
                                <input type="text" class="form-control" id="last_name" name="last_name" 
                                       value="{{ user.last_name }}" placeholder="Votre nom">
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="email" class="form-label">Email</label>
                            <input type="email" class="form-control" id="email" name="email" 
                                   value="{{ user.email }}" placeholder="votre@email.com">
                        </div>
                        
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Enregistrer les modifications
                        </button>
                    </form>
                </div>
            </div>
            
            <div class="card">
                <div class="card-header bg-secondary text-white">
                    <h5 class="mb-0"><i class="bi bi-clock-history"></i> Historique des connexions</h5>
                </div>
                <div class="card-body">
                    {% if login_history %}
                        <div class="list-group">
                            {% for login in login_history %}
                                <div class="list-group-item">
                                    <div class="d-flex w-100 justify-content-between">
                                        <h6 class="mb-1">
                                            <i class="bi bi-laptop"></i> {{ login.user_agent|truncatechars:50 }}
                                        </h6>
                                        <small>{{ login.timestamp|date:"d/m/Y H:i:s" }}</small>
                                    </div>
                                    <p class="mb-1">IP: {{ login.ip }}</p>
                                </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="text-muted text-center mb-0">Aucun historique de connexion disponible</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // Template filter pour md5 (Gravatar)
    document.addEventListener('DOMContentLoaded', function() {
        // Calculer le hash MD5 pour Gravatar
        const email = "{{ user.email|lower }}";
        if (email) {
            // Note: Pour une vraie implémentation, utilisez un filtre personnalisé Django
            // ou calculez le hash côté serveur
        }
    });
</script>
{% endblock %}