{% extends 'base.html' %}
{% load static %}

{% block title %}Réinitialisation du mot de passe - Contrôle Technique{% endblock %}

{% block extra_css %}
<style>
    :root {
        --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        --success-gradient: linear-gradient(135deg, #48bb78 0%, #2f855a 100%);
        --info-gradient: linear-gradient(135deg, #4299e1 0%, #2b6cb0 100%);
    }

    .reset-page {
        background: linear-gradient(135deg, #cbd2f3 0%, #d9d4e0 100%);
        min-height: calc(100vh - 72px);
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 40px 20px;
        position: relative;
        overflow: hidden;
    }

    /* Animated background */
    .circles {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        margin: 0;
        padding: 0;
    }

    .circles li {
        position: absolute;
        display: block;
        list-style: none;
        width: 20px;
        height: 20px;
        background: rgba(4, 36, 9, 0.15);
        animation: animate 20s linear infinite;
        bottom: -150px;
    }

    .circles li:nth-child(1) {
        left: 25%;
        width: 80px;
        height: 80px;
        animation-delay: 0s;
    }

    .circles li:nth-child(2) {
        left: 10%;
        width: 20px;
        height: 20px;
        animation-delay: 2s;
        animation-duration: 12s;
    }

    .circles li:nth-child(3) {
        left: 70%;
        width: 20px;
        height: 20px;
        animation-delay: 4s;
    }

    .circles li:nth-child(4) {
        left: 40%;
        width: 60px;
        height: 60px;
        animation-delay: 0s;
        animation-duration: 18s;
    }

    .circles li:nth-child(5) {
        left: 65%;
        width: 20px;
        height: 20px;
        animation-delay: 0s;
    }

    .circles li:nth-child(6) {
        left: 75%;
        width: 110px;
        height: 110px;
        animation-delay: 3s;
    }

    .circles li:nth-child(7) {
        left: 35%;
        width: 150px;
        height: 150px;
        animation-delay: 7s;
    }

    .circles li:nth-child(8) {
        left: 50%;
        width: 25px;
        height: 25px;
        animation-delay: 15s;
        animation-duration: 45s;
    }

    .circles li:nth-child(9) {
        left: 20%;
        width: 15px;
        height: 15px;
        animation-delay: 2s;
        animation-duration: 35s;
    }

    .circles li:nth-child(10) {
        left: 85%;
        width: 150px;
        height: 150px;
        animation-delay: 0s;
        animation-duration: 11s;
    }

    @keyframes animate {
        0% {
            transform: translateY(0) rotate(0deg);
            opacity: 1;
            border-radius: 0;
        }
        100% {
            transform: translateY(-1000px) rotate(720deg);
            opacity: 0;
            border-radius: 50%;
        }
    }

    /* Main card */
    .reset-card {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border-radius: 30px;
        box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        overflow: hidden;
        width: 100%;
        max-width: 500px;
        position: relative;
        z-index: 10;
        animation: slideUpFade 0.8s ease-out;
    }

    @keyframes slideUpFade {
        from {
            opacity: 0;
            transform: translateY(50px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    /* Header */
    .reset-header {
        background: #2f855a;;
        padding: 40px 30px;
        text-align: center;
        color: white;
        position: relative;
        overflow: hidden;
    }

    .reset-header::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -50%;
        width: 200%;
        height: 200%;
        background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
        animation: rotate 20s linear infinite;
    }

    @keyframes rotate {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
    }

    .reset-header i {
        font-size: 4rem;
        background: rgba(255, 255, 255, 0.2);
        padding: 20px;
        border-radius: 50%;
        margin-bottom: 20px;
        box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        animation: float 3s ease-in-out infinite;
    }

    @keyframes float {
        0% { transform: translateY(0px); }
        50% { transform: translateY(-10px); }
        100% { transform: translateY(0px); }
    }

    .reset-header h2 {
        font-size: 2rem;
        font-weight: 700;
        margin-bottom: 10px;
        letter-spacing: -0.5px;
    }

    .reset-header p {
        opacity: 0.9;
        font-size: 1rem;
        margin: 0;
    }

    /* Body */
    .reset-body {
        padding: 40px;
    }

    /* Info box */
    .info-box {
        background: linear-gradient(135deg, #ebf8ff 0%, #bee3f8 100%);
        border-radius: 15px;
        padding: 20px;
        margin-bottom: 30px;
        border-left: 4px solid #4299e1;
        animation: slideInRight 0.5s ease-out;
    }

    @keyframes slideInRight {
        from {
            opacity: 0;
            transform: translateX(30px);
        }
        to {
            opacity: 1;
            transform: translateX(0);
        }
    }

    .info-box i {
        font-size: 2rem;
        color: #4299e1;
        margin-bottom: 10px;
    }

    .info-box h5 {
        color: #2c5282;
        font-weight: 600;
        margin-bottom: 10px;
    }

    .info-box p {
        color: #2d3748;
        font-size: 0.95rem;
        margin: 0;
        line-height: 1.5;
    }

    /* Form groups */
    .form-group {
        margin-bottom: 25px;
        position: relative;
    }

    .form-group i {
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        color: #a0aec0;
        font-size: 1.2rem;
        transition: all 0.3s ease;
        z-index: 10;
    }

    .form-group.focused i {
        color: #124e29;
        transform: translateY(-50%) scale(1.1);
    }

    .form-control {
        height: 55px;
        padding-left: 45px;
        padding-right: 15px;
        border: 2px solid #e2e8f0;
        border-radius: 15px;
        font-size: 1rem;
        font-weight: 500;
        transition: all 0.3s ease;
        width: 100%;
    }

    .form-control:focus {
        border-color: #667eea;
        box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
        outline: none;
    }

    .form-control::placeholder {
        color: #a0aec0;
        font-weight: 400;
    }

    /* Email validation */
    .email-validation {
        margin-top: 10px;
        font-size: 0.9rem;
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 10px 15px;
        border-radius: 10px;
        transition: all 0.3s ease;
    }

    .email-validation.valid {
        background: #c6f6d5;
        color: #2f855a;
    }

    .email-validation.invalid {
        background: #fed7d7;
        color: #c53030;
    }

    /* Buttons */
    .btn-submit {
        width: 100%;
        height: 55px;
        background: #2f855a;
        border: none;
        border-radius: 15px;
        color: white;
        font-size: 1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
        margin-bottom: 15px;
    }

    .btn-submit::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
        transition: left 0.5s ease;
    }

    .btn-submit:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
    }

    .btn-submit:hover::before {
        left: 100%;
    }

    .btn-submit:active {
        transform: translateY(0);
    }

    .btn-submit:disabled {
        opacity: 0.7;
        cursor: not-allowed;
    }

    .btn-back {
        width: 100%;
        height: 55px;
        background: white;
        border: 2px solid #e2e8f0;
        border-radius: 15px;
        color: #4a5568;
        font-size: 1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        text-decoration: none;
    }

    .btn-back:hover {
        background: #f7fafc;
        border-color: #cbd5e0;
        transform: translateY(-2px);
        color: #2d3748;
    }

    /* Alerts */
    .custom-alert {
        border-radius: 15px;
        padding: 15px 20px;
        margin-bottom: 25px;
        border: none;
        display: flex;
        align-items: center;
        gap: 10px;
        animation: slideInRight 0.5s ease-out;
    }

    .alert-success {
        background: linear-gradient(135deg, #f0fff4 0%, #c6f6d5 100%);
        color: #2f855a;
        border-left: 4px solid #2f855a;
    }

    .alert-error {
        background: linear-gradient(135deg, #fff5f5 0%, #fed7d7 100%);
        color: #c53030;
        border-left: 4px solid #c53030;
    }

    .alert-info {
        background: linear-gradient(135deg, #ebf8ff 0%, #bee3f8 100%);
        color: #2b6cb0;
        border-left: 4px solid #2b6cb0;
    }

    .alert-warning {
        background: linear-gradient(135deg, #feebc8 0%, #fbd38d 100%);
        color: #c05621;
        border-left: 4px solid #c05621;
    }

    /* Loading spinner */
    .loader {
        display: none;
        width: 20px;
        height: 20px;
        border: 3px solid rgba(255,255,255,0.3);
        border-top: 3px solid white;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
        margin-left: 10px;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }

    .btn-content {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
    }

    /* Success state */
    .success-state {
        text-align: center;
        padding: 20px;
    }

    .success-state i {
        font-size: 5rem;
        color: #48bb78;
        animation: checkmark 0.5s ease-in-out;
        margin-bottom: 20px;
    }

    @keyframes checkmark {
        0% { transform: scale(0); }
        50% { transform: scale(1.2); }
        100% { transform: scale(1); }
    }

    .success-state h3 {
        color: #2d3748;
        font-weight: 700;
        margin-bottom: 15px;
    }

    .success-state p {
        color: #718096;
        margin-bottom: 25px;
        line-height: 1.6;
    }

    .success-state .email-sent {
        background: #f7fafc;
        border-radius: 10px;
        padding: 15px;
        margin-top: 20px;
        border: 2px dashed #48bb78;
    }

    .success-state .email-sent i {
        font-size: 1.5rem;
        margin-right: 10px;
        animation: none;
    }

    /* Countdown timer */
    .countdown {
        font-size: 0.9rem;
        color: #718096;
        margin-top: 15px;
    }

    .countdown span {
        font-weight: 700;
        color: #667eea;
    }

    /* Footer */
    .reset-footer {
        text-align: center;
        margin-top: 30px;
        padding-top: 20px;
        border-top: 2px solid #edf2f7;
    }

    .reset-footer a {
        color: #667eea;
        text-decoration: none;
        font-size: 0.95rem;
        font-weight: 500;
        transition: all 0.3s ease;
    }

    .reset-footer a:hover {
        color: #764ba2;
        text-decoration: underline;
    }

    /* Responsive */
    @media (max-width: 576px) {
        .reset-body {
            padding: 30px 20px;
        }

        .reset-header {
            padding: 30px 20px;
        }

        .reset-header h2 {
            font-size: 1.5rem;
        }

        .reset-header i {
            font-size: 3rem;
            padding: 15px;
        }

        .info-box {
            padding: 15px;
        }
    }

    /* Animation for resend button */
    @keyframes pulse {
        0% { transform: scale(1); }
        50% { transform: scale(1.05); }
        100% { transform: scale(1); }
    }

    .resend-active {
        animation: pulse 2s infinite;
    }
</style>
{% endblock %}

{% block content %}
<div class="reset-page">
    <!-- Animated background -->
    <ul class="circles">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>

    <div class="reset-card">
        <!-- Header -->
        <div class="reset-header">
            <i class="bi bi-key"></i>
            <h2>Mot de passe oublié ?</h2>
            <p>Réinitialisez votre mot de passe en toute sécurité</p>
        </div>

        <!-- Body -->
        <div class="reset-body">
            {% if messages %}
                {% for message in messages %}
                    <div class="custom-alert alert-{{ message.tags }} animate__animated animate__fadeInDown">
                        <i class="bi bi-{% if message.tags == 'success' %}check-circle{% elif message.tags == 'error' %}exclamation-triangle{% elif message.tags == 'warning' %}exclamation-circle{% else %}info-circle{% endif %}-fill"></i>
                        <span>{{ message }}</span>
                    </div>
                {% endfor %}
            {% endif %}

            {% if not email_sent %}
                <!-- Info box -->
                <div class="info-box">
                    <i class="bi bi-envelope-paper"></i>
                    <h5>Comment ça fonctionne ?</h5>
                    <p>Entrez votre adresse email ci-dessous. Si elle est associée à un compte, nous vous enverrons un lien pour réinitialiser votre mot de passe.</p>
                </div>

                <!-- Form -->
                <form method="post" action="{% url 'controle:password_reset' %}" id="reset-form">
                    {% csrf_token %}
                    
                    <div class="form-group" id="email-group">
                        <i class="bi bi-envelope"></i>
                        <input type="email" 
                               name="email" 
                               class="form-control" 
                               placeholder="Votre adresse email" 
                               required
                               autofocus
                               id="email-input">
                    </div>

                    <!-- Email validation (optional) -->
                    <div class="email-validation" id="email-validation" style="display: none;">
                        <i class="bi bi-check-circle-fill"></i>
                        <span>Email valide</span>
                    </div>

                    <button type="submit" class="btn-submit" id="submit-btn">
                        <span class="btn-content">
                            <i class="bi bi-send"></i>
                            <span>Envoyer le lien de réinitialisation</span>
                            <span class="loader" id="loader"></span>
                        </span>
                    </button>

                    <a href="{% url 'controle:login' %}" class="btn-back">
                        <i class="bi bi-arrow-left"></i>
                        Retour à la connexion
                    </a>
                </form>
            {% else %}
                <!-- Success state -->
                <div class="success-state">
                    <i class="bi bi-check-circle-fill"></i>
                    <h3>Email envoyé !</h3>
                    <p>Nous avons envoyé un lien de réinitialisation à <strong>{{ email }}</strong></p>
                    
                    <div class="email-sent">
                        <i class="bi bi-envelope-check"></i>
                        <span>Vérifiez votre boîte de réception et suivez les instructions</span>
                    </div>

                    <div class="countdown">
                        <i class="bi bi-clock"></i>
                        Vous n'avez pas reçu l'email ? <span id="countdown">60</span>s avant de pouvoir renvoyer
                    </div>

                    <button class="btn-submit resend-btn" id="resend-btn" disabled style="margin-top: 20px; opacity: 0.5;">
                        <span class="btn-content">
                            <i class="bi bi-arrow-repeat"></i>
                            <span>Renvoyer l'email</span>
                        </span>
                    </button>

                    <div class="reset-footer">
                        <a href="{% url 'controle:login' %}">
                            <i class="bi bi-box-arrow-in-right"></i>
                            Retour à la connexion
                        </a>
                    </div>
                </div>
            {% endif %}

            {% if not email_sent %}
                <!-- Additional help -->
                <div class="reset-footer">
                    <p class="mb-2">
                        <i class="bi bi-question-circle"></i>
                        <a href="#" data-bs-toggle="modal" data-bs-target="#helpModal">Besoin d'aide ?</a>
                    </p>
                    <p class="small text-muted">
                        Le lien de réinitialisation expire après 24 heures pour des raisons de sécurité.
                    </p>
                </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- Help Modal -->
<div class="modal fade" id="helpModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title">
                    <i class="bi bi-question-circle"></i> Aide - Réinitialisation du mot de passe
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="list-group list-group-flush">
                    <div class="list-group-item">
                        <i class="bi bi-1-circle-fill text-primary me-2"></i>
                        Entrez l'adresse email associée à votre compte
                    </div>
                    <div class="list-group-item">
                        <i class="bi bi-2-circle-fill text-primary me-2"></i>
                        Vérifiez votre boîte de réception (pensez aux spams)
                    </div>
                    <div class="list-group-item">
                        <i class="bi bi-3-circle-fill text-primary me-2"></i>
                        Cliquez sur le lien dans l'email
                    </div>
                    <div class="list-group-item">
                        <i class="bi bi-4-circle-fill text-primary me-2"></i>
                        Choisissez un nouveau mot de passe sécurisé
                    </div>
                </div>
                
                <div class="alert alert-warning mt-3 mb-0">
                    <i class="bi bi-exclamation-triangle"></i>
                    Si vous ne recevez pas l'email, vérifiez vos spams ou attendez quelques minutes.
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Elements
        const emailInput = document.getElementById('email-input');
        const emailGroup = document.getElementById('email-group');
        const emailValidation = document.getElementById('email-validation');
        const submitBtn = document.getElementById('submit-btn');
        const loader = document.getElementById('loader');
        const form = document.getElementById('reset-form');
        const resendBtn = document.getElementById('resend-btn');

        // Email validation
        if (emailInput) {
            emailInput.addEventListener('focus', function() {
                emailGroup.classList.add('focused');
            });

            emailInput.addEventListener('blur', function() {
                emailGroup.classList.remove('focused');
                validateEmail(this.value);
            });

            emailInput.addEventListener('input', function() {
                validateEmail(this.value);
            });
        }

        function validateEmail(email) {
            if (!emailValidation) return;

            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            
            if (email.length === 0) {
                emailValidation.style.display = 'none';
                return;
            }

            if (emailRegex.test(email)) {
                emailValidation.style.display = 'flex';
                emailValidation.className = 'email-validation valid';
                emailValidation.innerHTML = '<i class="bi bi-check-circle-fill"></i><span>Email valide</span>';
            } else {
                emailValidation.style.display = 'flex';
                emailValidation.className = 'email-validation invalid';
                emailValidation.innerHTML = '<i class="bi bi-exclamation-triangle-fill"></i><span>Format d\'email invalide</span>';
            }
        }

        // Form submission
        if (form) {
            form.addEventListener('submit', function(e) {
                const email = emailInput.value;
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

                if (!emailRegex.test(email)) {
                    e.preventDefault();
                    showNotification('Veuillez entrer une adresse email valide', 'error');
                    return;
                }

                const btnText = submitBtn.querySelector('.btn-content span:not(.loader):not(i)');
                btnText.style.opacity = '0.7';
                loader.style.display = 'inline-block';
                submitBtn.disabled = true;
            });
        }

        // Countdown timer for resend button
        if (resendBtn) {
            let countdown = 60;
            const countdownElement = document.getElementById('countdown');
            
            const timer = setInterval(() => {
                countdown--;
                if (countdownElement) {
                    countdownElement.textContent = countdown;
                }
                
                if (countdown <= 0) {
                    clearInterval(timer);
                    resendBtn.disabled = false;
                    resendBtn.style.opacity = '1';
                    resendBtn.classList.add('resend-active');
                    
                    if (countdownElement) {
                        countdownElement.textContent = '0';
                    }
                }
            }, 1000);

            // Resend click handler
            resendBtn.addEventListener('click', function() {
                // Here you would typically trigger the resend via AJAX
                // For now, just reload the page
                showNotification('Nouvel email envoyé !', 'success');
                setTimeout(() => {
                    location.reload();
                }, 2000);
            });
        }

        // Notification function
        function showNotification(message, type) {
            const alertDiv = document.createElement('div');
            alertDiv.className = `custom-alert alert-${type} animate__animated animate__fadeInDown`;
            alertDiv.innerHTML = `
                <i class="bi bi-${type === 'success' ? 'check-circle' : 'exclamation-triangle'}-fill"></i>
                <span>${message}</span>
            `;
            
            const resetBody = document.querySelector('.reset-body');
            resetBody.insertBefore(alertDiv, resetBody.firstChild);
            
            setTimeout(() => {
                alertDiv.classList.add('animate__fadeOutUp');
                setTimeout(() => alertDiv.remove(), 500);
            }, 5000);
        }

        // Prevent form resubmission on page refresh
        if (window.history.replaceState) {
            window.history.replaceState(null, null, window.location.href);
        }

        // Keyboard shortcuts
        document.addEventListener('keydown', function(e) {
            // Ctrl+Enter to submit
            if (e.ctrlKey && e.key === 'Enter' && form) {
                e.preventDefault();
                form.dispatchEvent(new Event('submit'));
            }
            
            // Escape to go back to login
            if (e.key === 'Escape') {
                window.location.href = "{% url 'controle:login' %}";
            }
        });

        // Auto-dismiss alerts after 5 seconds
        setTimeout(() => {
            document.querySelectorAll('.custom-alert').forEach(alert => {
                alert.classList.add('animate__fadeOutUp');
                setTimeout(() => alert.remove(), 500);
            });
        }, 5000);

        // Check for saved email (optional)
        const savedEmail = localStorage.getItem('resetEmail');
        if (savedEmail && emailInput) {
            emailInput.value = savedEmail;
            validateEmail(savedEmail);
        }

        // Save email on input (optional)
        if (emailInput) {
            emailInput.addEventListener('change', function() {
                if (this.value) {
                    localStorage.setItem('resetEmail', this.value);
                }
            });
        }

        // Add animation to resend button
        if (resendBtn) {
            resendBtn.addEventListener('mouseenter', function() {
                if (!this.disabled) {
                    this.style.transform = 'scale(1.02)';
                }
            });
            
            resendBtn.addEventListener('mouseleave', function() {
                this.style.transform = 'scale(1)';
            });
        }
    });
</script>
{% endblock %}