{% load static %} 
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - Groupe-alpages |Contrôle Technique Bâtiments</title>
    
    <!-- Fonts & Icons  -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet">
    <link href="{% static 'css/style.css' %}" rel="stylesheet"> 
    
  
</head>
<body>
    <!-- Animated background circles -->
    <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="login-wrapper">
        <div class="glass-container">
            <!-- Header -->
            <div class="login-header">
                <i class="bi bi-building"></i>
                <h1>Contrôle Technique</h1>
                <p>Bâtiments et Infrastructure</p>
            </div>

            <!-- Body -->
            <div class="login-body">
                {% if form.errors %}
                    <div class="custom-alert alert-danger animate__animated animate__shakeX">
                        <i class="bi bi-exclamation-triangle-fill"></i>
                        <span>Nom d'utilisateur ou mot de passe incorrect</span>
                    </div>
                {% endif %}
                
                {% if next and user.is_authenticated %}
                    <div class="custom-alert alert-warning">
                        <i class="bi bi-shield-lock-fill"></i>
                        <span>Accès non autorisé à cette page</span>
                    </div>
                {% endif %}
                
                {% if messages %}
                    {% for message in messages %}
                        <div class="custom-alert alert-{{ message.tags }}">
                            <i class="bi bi-{% if message.tags == 'success' %}check-circle{% else %}exclamation-triangle{% endif %}-fill"></i>
                            <span>{{ message }}</span>
                        </div>
                    {% endfor %}
                {% endif %}
                
                <form method="post" action="{% url 'controle:login' %}" id="login-form">
                    {% csrf_token %}
                    
                    <div class="form-group" id="username-group">
                        <i class="bi bi-person"></i>
                        <input type="text" 
                               name="username" 
                               class="form-control" 
                               placeholder="Nom d'utilisateur" 
                               required 
                               autofocus
                               id="username-input">
                    </div>
                    
                    <div class="form-group" id="password-group">
                        <i class="bi bi-lock"></i>
                        <input type="password" 
                               name="password" 
                               class="form-control" 
                               placeholder="Mot de passe" 
                               required
                               id="password-input">
                    </div>
                    
                    <div class="form-options">
                        <label class="checkbox-wrapper">
                            <input type="checkbox" name="remember" id="remember-checkbox">
                            <span>Se souvenir de moi</span>
                        </label>
                        <a href="{% url 'controle:password_reset' %}" class="forgot-password">
                            Mot de passe oublié?
                        </a>
                    </div>
                    
                    <button type="submit" class="btn-login" id="submit-btn">
                        <span class="btn-content">
                            <i class="bi bi-box-arrow-in-right"></i>
                            <span>Se connecter</span>
                            <span class="loader" id="loader"></span>
                        </span>
                    </button>
                    
                    <input type="hidden" name="next" value="{{ next }}">
                </form>
                
                
                <div class="version">
                    <i class="bi bi-c-circle"></i> 2024 Contrôle Technique Bâtiments v1.0.0
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Elements
            const form = document.getElementById('login-form');
            const submitBtn = document.getElementById('submit-btn');
            const loader = document.getElementById('loader');
            const usernameInput = document.getElementById('username-input');
            const passwordInput = document.getElementById('password-input');
            const rememberCheckbox = document.getElementById('remember-checkbox');
            const demoCredentials = document.getElementById('demo-credentials');
            const usernameGroup = document.getElementById('username-group');
            const passwordGroup = document.getElementById('password-group');

            // Form submission
            form.addEventListener('submit', function(e) {
                const btnText = submitBtn.querySelector('.btn-content span:not(.loader):not(i)');
                btnText.style.opacity = '0.7';
                loader.style.display = 'inline-block';
                submitBtn.disabled = true;
            });

            // Input focus effects
            [usernameInput, passwordInput].forEach(input => {
                const group = input.closest('.form-group');
                
                input.addEventListener('focus', function() {
                    group.classList.add('focused');
                    group.style.transform = 'scale(1.02)';
                });
                
                input.addEventListener('blur', function() {
                    group.classList.remove('focused');
                    group.style.transform = 'scale(1)';
                });
                
                // Real-time validation
                input.addEventListener('input', function() {
                    this.classList.remove('is-invalid');
                    const errorDiv = this.parentElement.querySelector('.invalid-feedback');
                    if (errorDiv) {
                        errorDiv.remove();
                    }
                });
            });

            // Remember me functionality
            if (rememberCheckbox) {
                const rememberedUser = localStorage.getItem('rememberedUser');
                if (rememberedUser) {
                    usernameInput.value = rememberedUser;
                    rememberCheckbox.checked = true;
                }
                
                rememberCheckbox.addEventListener('change', function() {
                    if (this.checked && usernameInput.value) {
                        localStorage.setItem('rememberedUser', usernameInput.value);
                    } else {
                        localStorage.removeItem('rememberedUser');
                    }
                });
            }

            // Demo credentials with animation
            if (demoCredentials) {
                demoCredentials.addEventListener('click', function() {
                    // Fill credentials
                    usernameInput.value = 'demo';
                    passwordInput.value = 'demo123';
                    
                    // Animate the click
                    this.style.transform = 'scale(0.98)';
                    setTimeout(() => {
                        this.style.transform = 'scale(1)';
                    }, 200);
                    
                    // Highlight the filled inputs
                    [usernameGroup, passwordGroup].forEach(group => {
                        group.style.backgroundColor = '#e8f5e9';
                        group.style.transition = 'background-color 0.3s ease';
                        setTimeout(() => {
                            group.style.backgroundColor = 'transparent';
                        }, 500);
                    });
                    
                    // Optional: auto-check remember me
                    if (rememberCheckbox) {
                        rememberCheckbox.checked = true;
                        localStorage.setItem('rememberedUser', 'demo');
                    }
                });
            }

            // Typing animation for placeholder (fun effect)
            let typingTimer;
            const typingDelay = 1000;
            
            usernameInput.addEventListener('keyup', function() {
                clearTimeout(typingTimer);
                if (this.value.length > 0) {
                    typingTimer = setTimeout(() => {
                        // Optional: Add typing animation
                    }, typingDelay);
                }
            });

            // Password strength indicator (optional)
            passwordInput.addEventListener('input', function() {
                const strength = checkPasswordStrength(this.value);
                // You can add a visual indicator here if desired
            });

            function checkPasswordStrength(password) {
                let strength = 0;
                if (password.length >= 8) strength++;
                if (password.match(/[a-z]+/)) strength++;
                if (password.match(/[A-Z]+/)) strength++;
                if (password.match(/[0-9]+/)) strength++;
                if (password.match(/[$@#&!]+/)) strength++;
                return strength;
            }

            // Network status
            window.addEventListener('offline', function() {
                showNotification('Mode hors ligne', 'warning');
            });
            
            window.addEventListener('online', function() {
                showNotification('Connexion rétablie', 'success');
                setTimeout(() => {
                    location.reload();
                }, 1500);
            });

            function showNotification(message, type) {
                const alert = document.createElement('div');
                alert.className = `custom-alert alert-${type} animate__animated animate__fadeInDown`;
                alert.innerHTML = `
                    <i class="bi bi-${type === 'warning' ? 'wifi-off' : 'wifi'}"></i>
                    <span>${message}</span>
                `;
                
                const loginBody = document.querySelector('.login-body');
                loginBody.insertBefore(alert, loginBody.firstChild);
                
                setTimeout(() => {
                    alert.classList.add('animate__fadeOutUp');
                    setTimeout(() => alert.remove(), 500);
                }, 3000);
            }

            // Smooth hover effects for options
            const forgotLink = document.querySelector('.forgot-password');
            forgotLink.addEventListener('mouseenter', function() {
                this.style.letterSpacing = '0.5px';
            });
            
            forgotLink.addEventListener('mouseleave', function() {
                this.style.letterSpacing = 'normal';
            });

            // Add floating labels effect
            function updateFloatLabels() {
                [usernameInput, passwordInput].forEach(input => {
                    if (input.value.length > 0) {
                        input.closest('.form-group').classList.add('has-value');
                    } else {
                        input.closest('.form-group').classList.remove('has-value');
                    }
                });
            }
            
            [usernameInput, passwordInput].forEach(input => {
                input.addEventListener('input', updateFloatLabels);
            });
            
            // Initial check
            updateFloatLabels();

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

            // Add smooth scroll to top if needed
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });

            // Easter egg: Konami code (optional fun feature)
            let konamiCode = [];
            const konamiSequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
            
            document.addEventListener('keydown', function(e) {
                konamiCode.push(e.keyCode);
                if (konamiCode.length > konamiSequence.length) {
                    konamiCode.shift();
                }
                
                if (konamiCode.toString() === konamiSequence.toString()) {
                    document.body.style.background = 'linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 100%)';
                    showNotification('Mode développeur activé!', 'success');
                }
            });
        });

        // Add CSS class for when inputs have value
        const style = document.createElement('style');
        style.textContent = `
            .form-group.has-value i {
                color: #667eea;
            }
            .form-group.focused i {
                transform: translateY(-50%) scale(1.1);
            }
        `;
        document.head.appendChild(style);
    </script>
</body>
</html>