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

{% block title %}Blog - ECOLUX BTP{% endblock %}

{% block content %}
<!-- Blog Header -->
<section class="blog-header">
    <div class="container">
        <div class="blog-header-content">
            <h1>Notre Blog</h1>
            <p>Découvrez nos derniers articles, conseils et actualités sur la construction au Sénégal</p>
        </div>
    </div>
</section>

<!-- Blog Content -->
<section class="blog-content">
    <div class="container">
        <div class="blog-layout">
            <!-- Main Content -->
            <div class="blog-main">
                <!-- Filters -->
                <div class="blog-filters">
                    <div class="filter-group">
                        <a href="{% url 'app:blog_list' %}" 
                           class="filter-btn {% if not selected_category %}active{% endif %}">
                            Tous les articles
                        </a>
                        {% for category in categories %}
                        <a href="{% url 'app:blog_list' %}?category={{ category.slug }}" 
                           class="filter-btn {% if selected_category.slug == category.slug %}active{% endif %}"
                           style="--category-color: {{ category.color }};">
                            {{ category.name }}
                        </a>
                        {% endfor %}
                    </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 %}
                            <input type="text" 
                                   name="search" 
                                   placeholder="Rechercher un article..."
                                   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>

                <!-- Articles Grid -->
                <div class="blog-grid" id="blogGrid">
                    {% for post in posts %}
                    <article class="blog-card">
                        {% if post.featured_image %}
                        <div class="blog-card-image">
                            <img src="{{ post.featured_image.url }}" alt="{{ post.title }}">
                            {% if post.category %}
                            <span class="blog-category" style="background-color: {{ post.category.color }};">
                                {{ post.category.name }}
                            </span>
                            {% endif %}
                        </div>
                        {% endif %}
                        
                        <div class="blog-card-content">
                            <div class="blog-meta">
                                <span class="blog-date">{{ post.published_at|date:"d F Y" }}</span>
                                <span class="blog-reading-time">{{ post.reading_time }} min de lecture</span>
                            </div>
                            
                            <h3 class="blog-title">
                                <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
                            </h3>
                            
                            <p class="blog-excerpt">{{ post.excerpt }}</p>
                            
                            <div class="blog-footer">
                                <div class="blog-author">
                                    <span>Par {{ post.author.first_name }} {{ post.author.last_name }}</span>
                                </div>
                                <a href="{{ post.get_absolute_url }}" class="read-more">
                                    Lire la suite →
                                </a>
                            </div>
                        </div>
                    </article>
                    {% empty %}
                    <div class="no-results">
                        <h3>Aucun article trouvé</h3>
                        <p>Essayez de modifier vos critères de recherche.</p>
                    </div>
                    {% endfor %}
                </div>

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

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

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

            <!-- Sidebar -->
            <aside class="blog-sidebar">
                <!-- Featured Posts -->
                {% if featured_posts %}
                <div class="sidebar-widget">
                    <h3>Articles en vedette</h3>
                    <div class="featured-posts">
                        {% for post in featured_posts %}
                        <article class="featured-post">
                            {% if post.featured_image %}
                            <div class="featured-post-image">
                                <img src="{{ post.featured_image.url }}" alt="{{ post.title }}">
                            </div>
                            {% endif %}
                            <div class="featured-post-content">
                                <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
                                <span class="featured-post-date">{{ post.published_at|date:"d M Y" }}</span>
                            </div>
                        </article>
                        {% endfor %}
                    </div>
                </div>
                {% endif %}

                <!-- Categories -->
                <div class="sidebar-widget">
                    <h3>Catégories</h3>
                    <ul class="category-list">
                        {% for category in categories %}
                        <li>
                            <a href="{% url 'app:blog_category' category.slug %}">
                                <span class="category-color" style="background-color: {{ category.color }};"></span>
                                {{ category.name }}
                                <span class="post-count">({{ category.posts.count }})</span>
                            </a>
                        </li>
                        {% endfor %}
                    </ul>
                </div>

                <!-- Contact CTA -->
                <div class="sidebar-widget contact-cta">
                    <h3>Besoin d'aide ?</h3>
                    <p>Contactez nos experts pour votre projet de construction.</p>
                    <a href="#contact" class="btn-primary">Nous contacter</a>
                </div>
            </aside>
        </div>
    </div>
</section>

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

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

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

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

.blog-layout {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 3rem;
}

.blog-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 {
    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: var(--category-color, hsl(var(--primary)));
    border-color: var(--category-color, hsl(var(--primary)));
    color: white;
}

.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);
}

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

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

.blog-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.15);
}

.blog-card-image {
    position: relative;
    height: 200px;
    overflow: hidden;
}

.blog-card-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.blog-category {
    position: absolute;
    top: 1rem;
    left: 1rem;
    padding: 0.25rem 0.75rem;
    border-radius: 50px;
    color: white;
    font-size: 0.8rem;
    font-weight: 500;
}

.blog-card-content {
    padding: 1.5rem;
}

.blog-meta {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    font-size: 0.85rem;
    color: hsl(var(--foreground) / 0.6);
}

.blog-title {
    margin-bottom: 1rem;
}

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

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

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

.blog-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.blog-author {
    font-size: 0.9rem;
    color: hsl(var(--foreground) / 0.6);
}

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

.read-more:hover {
    text-decoration: underline;
}

/* Sidebar */
.blog-sidebar {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.sidebar-widget {
    background: hsl(var(--card));
    padding: 1.5rem;
    border-radius: 1rem;
    box-shadow: var(--shadow);
}

.sidebar-widget h3 {
    margin-bottom: 1rem;
    color: hsl(var(--foreground));
    font-size: 1.25rem;
}

.featured-posts {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.featured-post {
    display: flex;
    gap: 1rem;
}

.featured-post-image {
    width: 60px;
    height: 60px;
    border-radius: 0.5rem;
    overflow: hidden;
    flex-shrink: 0;
}

.featured-post-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.featured-post-content h4 {
    font-size: 0.9rem;
    margin-bottom: 0.25rem;
}

.featured-post-content h4 a {
    text-decoration: none;
    color: hsl(var(--foreground));
}

.featured-post-content h4 a:hover {
    color: hsl(var(--primary));
}

.featured-post-date {
    font-size: 0.8rem;
    color: hsl(var(--foreground) / 0.6);
}

.category-list {
    list-style: none;
}

.category-list li {
    margin-bottom: 0.5rem;
}

.category-list a {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    color: hsl(var(--foreground));
    padding: 0.5rem;
    border-radius: 0.5rem;
    transition: var(--transition);
}

.category-list a:hover {
    background: hsl(var(--accent));
}

.category-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.post-count {
    margin-left: auto;
    font-size: 0.8rem;
    color: hsl(var(--foreground) / 0.6);
}

.contact-cta {
    text-align: center;
    background: linear-gradient(135deg, hsl(var(--primary)), hsl(var(--secondary)));
    color: white;
}

.contact-cta h3 {
    color: white;
}

.contact-cta p {
    margin-bottom: 1.5rem;
    opacity: 0.9;
}

.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);
}

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

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

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

/* Responsive */
@media (max-width: 768px) {
    .blog-layout {
        grid-template-columns: 1fr;
        gap: 2rem;
    }
    
    .blog-filters {
        flex-direction: column;
        align-items: stretch;
    }
    
    .search-form input {
        width: 100%;
    }
    
    .blog-grid {
        grid-template-columns: 1fr;
    }
    
    .blog-header h1 {
        font-size: 2rem;
    }
    
    .blog-header p {
        font-size: 1rem;
    }
}
</style>
{% endblock %}
