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

{% block title %}Nos Réalisations - ECOLUX BTP{% endblock %}

{% block content %}
<!-- Projects Header -->
<section class="projects-header">
    <div class="container">
        <div class="projects-header-content">
            <h1>Nos Réalisations</h1>
            <p>Découvrez nos projets de construction réalisés avec excellence au Sénégal et en Afrique</p>
        </div>
    </div>
</section>

<!-- Projects Content -->
<section class="projects-content">
    <div class="container">
        <!-- Filters -->
        <div class="projects-filters">
            <div class="filter-group">
                <a href="{% url 'app:projects_list' %}" 
                   class="filter-btn {% if not selected_category and not status_filter %}active{% endif %}">
                    Tous les projets
                </a>
                {% for category in categories %}
                <a href="{% url 'app:projects_list' %}?category={{ category.slug }}" 
                   class="filter-btn {% if selected_category.slug == category.slug %}active{% endif %}">
                    {% if category.icon %}
                    <span class="filter-icon">{{ category.icon }}</span>
                    {% endif %}
                    {{ category.name }}
                </a>
                {% endfor %}
            </div>
            
            <!-- Status Filter -->
            <div class="status-filters">
                <select id="statusFilter" onchange="filterByStatus(this.value)">
                    <option value="">Tous les statuts</option>
                    {% for status_value, status_label in status_choices %}
                    <option value="{{ status_value }}" {% if status_filter == status_value %}selected{% endif %}>
                        {{ status_label }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            
            <!-- Search -->
            <div class="search-box">
                <form method="GET" class="search-form">
                    {% if selected_category %}
                    <input type="hidden" name="category" value="{{ selected_category.slug }}">
                    {% endif %}
                    {% if status_filter %}
                    <input type="hidden" name="status" value="{{ status_filter }}">
                    {% endif %}
                    <input type="text" 
                           name="search" 
                           placeholder="Rechercher un projet..."
                           value="{{ search_query|default:'' }}">
                    <button type="submit">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="11" cy="11" r="8"></circle>
                            <path d="m21 21-4.35-4.35"></path>
                        </svg>
                    </button>
                </form>
            </div>
        </div>

        <!-- Projects Grid -->
        <div class="projects-grid" id="projectsGrid">
            {% for project in projects %}
            <article class="project-card">
                <div class="project-image">
                    <img src="{{ project.main_image.url }}" alt="{{ project.title }}">
                    <div class="project-overlay">
                        <div class="project-status {{ project.status|lower }}">
                            {{ project.get_status_display }}
                        </div>
                        {% if project.category %}
                        <div class="project-category">
                            {{ project.category.name }}
                        </div>
                        {% endif %}
                    </div>
                    <a href="{{ project.get_absolute_url }}" class="project-link">
                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                            <circle cx="12" cy="12" r="3"></circle>
                        </svg>
                    </a>
                </div>
                
                <div class="project-content">
                    <div class="project-header">
                        <h3 class="project-title">
                            <a href="{{ project.get_absolute_url }}">{{ project.title }}</a>
                        </h3>
                        <div class="project-location">
                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path>
                                <circle cx="12" cy="10" r="3"></circle>
                            </svg>
                            {{ project.location }}
                        </div>
                    </div>
                    
                    <p class="project-description">{{ project.short_description }}</p>
                    
                    <div class="project-details">
                        <div class="project-stats">
                            {% if project.surface_built %}
                            <div class="stat">
                                <span class="stat-label">Surface</span>
                                <span class="stat-value">{{ project.surface_built }} m²</span>
                            </div>
                            {% endif %}
                            {% if project.completed_date %}
                            <div class="stat">
                                <span class="stat-label">Année</span>
                                <span class="stat-value">{{ project.completed_date|date:"Y" }}</span>
                            </div>
                            {% endif %}
                            {% if project.floors_count %}
                            <div class="stat">
                                <span class="stat-label">Niveaux</span>
                                <span class="stat-value">{{ project.floors_count }}</span>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="project-footer">
                        <a href="{{ project.get_absolute_url }}" class="view-project">
                            Voir le projet →
                        </a>
                        <div class="project-views">
                            {{ project.views_count }} vue{{ project.views_count|pluralize }}
                        </div>
                    </div>
                </div>
            </article>
            {% empty %}
            <div class="no-results">
                <div class="no-results-icon">
                    <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1">
                        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
                    </svg>
                </div>
                <h3>Aucun projet trouvé</h3>
                <p>Essayez de modifier vos critères de recherche ou de filtre.</p>
                <a href="{% url 'app:projects_list' %}" class="btn-primary">Voir tous les projets</a>
            </div>
            {% endfor %}
        </div>

        <!-- Pagination -->
        {% if projects.has_other_pages %}
        <div class="pagination-wrapper">
            <div class="pagination">
                {% if projects.has_previous %}
                <a href="?page=1{% if selected_category %}&category={{ selected_category.slug }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}" 
                   class="page-link">Premier</a>
                <a href="?page={{ projects.previous_page_number }}{% if selected_category %}&category={{ selected_category.slug }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}" 
                   class="page-link">Précédent</a>
                {% endif %}

                <span class="page-info">
                    Page {{ projects.number }} sur {{ projects.paginator.num_pages }}
                </span>

                {% if projects.has_next %}
                <a href="?page={{ projects.next_page_number }}{% if selected_category %}&category={{ selected_category.slug }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}" 
                   class="page-link">Suivant</a>
                <a href="?page={{ projects.paginator.num_pages }}{% if selected_category %}&category={{ selected_category.slug }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}" 
                   class="page-link">Dernier</a>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</section>

<!-- CTA Section -->
<section class="projects-cta">
    <div class="container">
        <div class="cta-content">
            <h2>Prêt à démarrer votre projet ?</h2>
            <p>Contactez-nous pour discuter de votre projet de construction et obtenir un devis personnalisé.</p>
            <div class="cta-buttons">
                <a href="#contact" class="btn-primary btn-lg">Demander un devis</a>
                <a href="{% url 'app:simulation' %}" class="btn-outline btn-lg">Faire une estimation</a>
            </div>
        </div>
    </div>
</section>

<style>
/* Projects Styles */
.projects-header {
    background: linear-gradient(135deg, hsl(var(--primary)), hsl(var(--secondary)));
    color: white;
    padding: 6rem 0 4rem;
    text-align: center;
}

.projects-header h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 1rem;
}

.projects-header p {
    font-size: 1.25rem;
    opacity: 0.9;
    max-width: 600px;
    margin: 0 auto;
}

.projects-content {
    padding: 4rem 0;
}

.projects-filters {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 3rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.filter-group {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.filter-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border: 2px solid hsl(var(--border));
    border-radius: 50px;
    text-decoration: none;
    color: hsl(var(--foreground));
    transition: var(--transition);
    font-size: 0.9rem;
}

.filter-btn:hover,
.filter-btn.active {
    background: hsl(var(--primary));
    border-color: hsl(var(--primary));
    color: white;
}

.filter-icon {
    font-size: 1rem;
}

.status-filters select {
    padding: 0.5rem 1rem;
    border: 2px solid hsl(var(--border));
    border-radius: 0.5rem;
    background: hsl(var(--card));
    color: hsl(var(--foreground));
    outline: none;
}

.search-box {
    position: relative;
}

.search-form {
    display: flex;
    align-items: center;
}

.search-form input {
    padding: 0.5rem 3rem 0.5rem 1rem;
    border: 2px solid hsl(var(--border));
    border-radius: 50px;
    width: 250px;
    outline: none;
}

.search-form button {
    position: absolute;
    right: 0.5rem;
    background: none;
    border: none;
    cursor: pointer;
    color: hsl(var(--foreground) / 0.6);
}

.projects-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 2rem;
}

.project-card {
    background: hsl(var(--card));
    border-radius: 1rem;
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: var(--transition);
}

.project-card:hover {
    transform: translateY(-10px);
    box-shadow: 0 25px 50px -10px rgba(0, 0, 0, 0.2);
}

.project-image {
    position: relative;
    height: 250px;
    overflow: hidden;
}

.project-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: var(--transition);
}

.project-card:hover .project-image img {
    transform: scale(1.05);
}

.project-overlay {
    position: absolute;
    top: 1rem;
    left: 1rem;
    right: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.project-status {
    padding: 0.25rem 0.75rem;
    border-radius: 50px;
    font-size: 0.8rem;
    font-weight: 500;
    color: white;
}

.project-status.completed {
    background: #10B981;
}

.project-status.in_progress {
    background: #F59E0B;
}

.project-status.planning {
    background: #6366F1;
}

.project-status.on_hold {
    background: #EF4444;
}

.project-category {
    padding: 0.25rem 0.75rem;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border-radius: 50px;
    font-size: 0.8rem;
}

.project-link {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: var(--transition);
    color: hsl(var(--primary));
    text-decoration: none;
}

.project-card:hover .project-link {
    opacity: 1;
}

.project-content {
    padding: 1.5rem;
}

.project-header {
    margin-bottom: 1rem;
}

.project-title {
    margin-bottom: 0.5rem;
}

.project-title a {
    text-decoration: none;
    color: hsl(var(--foreground));
    font-weight: 600;
    font-size: 1.25rem;
    transition: var(--transition);
}

.project-title a:hover {
    color: hsl(var(--primary));
}

.project-location {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: hsl(var(--foreground) / 0.6);
    font-size: 0.9rem;
}

.project-description {
    color: hsl(var(--foreground) / 0.8);
    margin-bottom: 1.5rem;
    line-height: 1.6;
}

.project-stats {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.stat {
    display: flex;
    flex-direction: column;
    text-align: center;
    min-width: 60px;
}

.stat-label {
    font-size: 0.8rem;
    color: hsl(var(--foreground) / 0.6);
    margin-bottom: 0.25rem;
}

.stat-value {
    font-weight: 600;
    color: hsl(var(--primary));
    font-size: 0.9rem;
}

.project-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-top: 1px solid hsl(var(--border));
    padding-top: 1rem;
}

.view-project {
    color: hsl(var(--primary));
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
    transition: var(--transition);
}

.view-project:hover {
    text-decoration: underline;
}

.project-views {
    font-size: 0.8rem;
    color: hsl(var(--foreground) / 0.6);
}

.no-results {
    grid-column: 1 / -1;
    text-align: center;
    padding: 4rem 2rem;
}

.no-results-icon {
    margin-bottom: 1.5rem;
    color: hsl(var(--foreground) / 0.3);
}

.no-results h3 {
    margin-bottom: 1rem;
    color: hsl(var(--foreground));
}

.no-results p {
    color: hsl(var(--foreground) / 0.6);
    margin-bottom: 2rem;
}

.pagination-wrapper {
    margin-top: 3rem;
    display: flex;
    justify-content: center;
}

.pagination {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.page-link {
    padding: 0.5rem 1rem;
    border: 2px solid hsl(var(--border));
    border-radius: 0.5rem;
    text-decoration: none;
    color: hsl(var(--foreground));
    transition: var(--transition);
}

.page-link:hover {
    background: hsl(var(--primary));
    border-color: hsl(var(--primary));
    color: white;
}

.page-info {
    padding: 0.5rem 1rem;
    color: hsl(var(--foreground) / 0.6);
}

/* CTA Section */
.projects-cta {
    background: hsl(var(--muted));
    padding: 4rem 0;
    text-align: center;
}

.cta-content h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    color: hsl(var(--foreground));
}

.cta-content p {
    font-size: 1.25rem;
    color: hsl(var(--foreground) / 0.8);
    margin-bottom: 2rem;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}

.cta-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
}

/* Responsive */
@media (max-width: 768px) {
    .projects-filters {
        flex-direction: column;
        align-items: stretch;
    }
    
    .search-form input {
        width: 100%;
    }
    
    .projects-grid {
        grid-template-columns: 1fr;
    }
    
    .projects-header h1 {
        font-size: 2rem;
    }
    
    .projects-header p {
        font-size: 1rem;
    }
    
    .project-stats {
        justify-content: center;
    }
    
    .cta-content h2 {
        font-size: 2rem;
    }
    
    .cta-buttons {
        flex-direction: column;
        align-items: center;
    }
}
</style>

<script>
function filterByStatus(status) {
    const url = new URL(window.location.href);
    if (status) {
        url.searchParams.set('status', status);
    } else {
        url.searchParams.delete('status');
    }
    url.searchParams.delete('page'); // Reset pagination
    window.location.href = url.toString();
}
</script>
{% endblock %}
