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

{% block title %}{{ page_title }} - ECOLUX Dashboard{% endblock %}

{% block content %}
<section class="section is-main-section">
    <div class="level">
        <div class="level-left">
            <div class="level-item">
                <div class="title">
                    <h1 class="title is-3 has-text-ecolux-primary">
                        <span class="icon-text">
                            <span class="icon has-text-ecolux-secondary">
                                <i class="fas fa-project-diagram"></i>
                            </span>
                            <span>{{ page_title }}</span>
                        </span>
                    </h1>
                </div>
            </div>
        </div>
        <div class="level-right">
            <div class="level-item">
                <div class="buttons">
                    <a href="{% url 'app:dashboard_home' %}" class="button is-light">
                        <span class="icon">
                            <i class="fas fa-arrow-left"></i>
                        </span>
                        <span>Retour au tableau de bord</span>
                    </a>
                    {% if user_type == 'DRH' or user_type == 'TECHNICIEN' %}
                    <button class="button is-ecolux-primary" onclick="openCreateProjectModal()">
                        <span class="icon">
                            <i class="fas fa-plus"></i>
                        </span>
                        <span>Nouveau Projet</span>
                    </button>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Filtres -->
    <div class="card ecolux-card">
        <div class="card-content">
            <form method="GET" class="field is-grouped">
                <div class="control">
                    <div class="field has-addons">
                        <div class="control">
                            <span class="button is-static ecolux-primary">
                                <i class="fas fa-filter"></i>
                            </span>
                        </div>
                        <div class="control">
                            <div class="select">
                                <select name="status" onchange="this.form.submit()">
                                    <option value="">Tous les statuts</option>
                                    {% for value, label in status_choices %}
                                        <option value="{{ value }}" {% if current_status == value %}selected{% endif %}>
                                            {{ label }}
                                        </option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="control is-expanded">
                    <input class="input" type="text" name="search" placeholder="Rechercher un projet..." 
                           value="{{ search_query }}">
                </div>
                <div class="control">
                    <button type="submit" class="button is-ecolux-primary">
                        <span class="icon">
                            <i class="fas fa-search"></i>
                        </span>
                    </button>
                </div>
                    <button type="submit" class="button is-primary">
                        <span class="icon">
                            <i class="fas fa-search"></i>
                        </span>
                        <span>Filtrer</span>
                    </button>
                </div>
                {% if current_status or search_query %}
                <div class="control">
                    <a href="{% url 'app:dashboard_projects_list' %}" class="button is-light">
                        <span class="icon">
                            <i class="fas fa-times"></i>
                        </span>
                        <span>Effacer</span>
                    </a>
                </div>
                {% endif %}
            </form>
        </div>
    </div>

    <!-- Liste des projets -->
    <div class="card ecolux-card">
        <header class="card-header ecolux-primary">
            <p class="card-header-title has-text-ecolux-secondary">
                <span class="icon">
                    <i class="fas fa-list"></i>
                </span>
                Liste des projets
            </p>
        </header>
        <div class="card-content">
            {% if page_obj %}
                {% for project in page_obj %}
                <div class="media" style="border-bottom: 1px solid #f1f3f4; padding-bottom: 1rem; margin-bottom: 1rem;">
                    <div class="media-content">
                        <div class="content">
                            <p>
                                <strong>
                                    <a href="{% url 'app:dashboard_project_detail' project.id %}" class="ecolux-link">
                                        {{ project.title }}
                                    </a>
                                </strong>
                                {% if project.status == 'PLANNING' %}
                                    <span class="tag is-ecolux-secondary is-small">{{ project.get_status_display }}</span>
                                {% elif project.status == 'IN_PROGRESS' %}
                                    <span class="tag is-ecolux-primary is-small">{{ project.get_status_display }}</span>
                                {% elif project.status == 'COMPLETED' %}
                                    <span class="tag is-success is-small">{{ project.get_status_display }}</span>
                                {% elif project.status == 'PAUSED' %}
                                    <span class="tag is-warning is-small">{{ project.get_status_display }}</span>
                                {% else %}
                                    <span class="tag is-light is-small">{{ project.get_status_display }}</span>
                                {% endif %}
                                <br>
                                {{ project.description|striptags|truncatewords:20 }}
                                <br>
                                <small>
                                    <strong class="has-text-ecolux-primary">Client:</strong> {{ project.client.user.get_full_name }}
                                    &nbsp;|&nbsp;
                                    <strong class="has-text-ecolux-primary">Activités:</strong> {{ project.activities_count }}
                                    &nbsp;|&nbsp;
                                    <strong class="has-text-ecolux-primary">Progression:</strong> {{ project.completion_percentage }}%
                                    {% if project.budget %}
                                        &nbsp;|&nbsp;
                                        <strong class="has-text-ecolux-primary">Budget:</strong> {{ project.budget|floatformat:0 }} FCFA
                                    {% endif %}
                                </small>
                                <br>
                                <small class="has-text-grey">
                                    Créé le {{ project.created_at|date:"d/m/Y à H:i" }}
                                    {% if project.start_date %}
                                        - Début: {{ project.start_date|date:"d/m/Y" }}
                                    {% endif %}
                                    {% if project.end_date %}
                                        - Fin prévue: {{ project.end_date|date:"d/m/Y" }}
                                    {% endif %}
                                </small>
                            </p>
                            
                            <!-- Barre de progression -->
                            <div class="progress is-small is-ecolux-primary">
                                <progress class="progress is-ecolux-primary" value="{{ project.completion_percentage }}" max="100">
                                    {{ project.completion_percentage }}%
                                </progress>
                            </div>
                            
                            <!-- Actions pour DRH et TECHNICIEN -->
                            {% if user_type == 'DRH' or user_type == 'TECHNICIEN' %}
                            <div class="buttons are-small">
                                <button class="button is-ecolux-secondary is-light" onclick="editProject({{ project.id }})">
                                    <span class="icon">
                                        <i class="fas fa-edit"></i>
                                    </span>
                                    <span>Modifier</span>
                                </button>
                                <button class="button is-ecolux-primary is-light" onclick="assignTechnician({{ project.id }})">
                                    <span class="icon">
                                        <i class="fas fa-user-plus"></i>
                                    </span>
                                    <span>Affecter</span>
                                </button>
                                {% if user_type == 'DRH' %}
                                <button class="button is-danger is-light" onclick="deleteProject({{ project.id }})">
                                    <span class="icon">
                                        <i class="fas fa-trash"></i>
                                    </span>
                                </button>
                                {% endif %}
                            </div>
                            {% endif %}
                            
                            <!-- Techniciens assignés -->
                            {% if project.assigned_technicians.count > 0 %}
                            <div class="tags">
                                <span class="tag is-light">Techniciens:</span>
                                {% for tech in project.assigned_technicians.all %}
                                    <span class="tag is-info is-light">
                                        {{ tech.user.get_full_name|default:tech.user.username }}
                                    </span>
                                {% endfor %}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="media-right">
                        <div class="buttons">
                            <a href="{% url 'app:dashboard_project_detail' project.id %}" class="button is-small is-primary">
                                <span class="icon">
                                    <i class="fas fa-eye"></i>
                                </span>
                                <span>Détails</span>
                            </a>
                        </div>
                    </div>
                </div>
                {% if not forloop.last %}<hr>{% endif %}
                {% endfor %}
            {% else %}
                <div class="notification is-info is-light">
                    <p>Aucun projet trouvé.</p>
                    {% if current_status or search_query %}
                        <p><a href="{% url 'app:dashboard_projects_list' %}">Effacer les filtres</a> pour voir tous les projets.</p>
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav class="pagination is-centered" role="navigation" aria-label="pagination">
        {% if page_obj.has_previous %}
            <a class="pagination-previous" href="?page={{ page_obj.previous_page_number }}{% if current_status %}&status={{ current_status }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}">
                Précédent
            </a>
        {% else %}
            <a class="pagination-previous" disabled>Précédent</a>
        {% endif %}
        
        {% if page_obj.has_next %}
            <a class="pagination-next" href="?page={{ page_obj.next_page_number }}{% if current_status %}&status={{ current_status }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}">
                Suivant
            </a>
        {% else %}
            <a class="pagination-next" disabled>Suivant</a>
        {% endif %}
        
        <ul class="pagination-list">
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                    <li><a class="pagination-link is-current" aria-label="Page {{ num }}" aria-current="page">{{ num }}</a></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li><a class="pagination-link" href="?page={{ num }}{% if current_status %}&status={{ current_status }}{% endif %}{% if search_query %}&search={{ search_query }}{% endif %}" aria-label="Page {{ num }}">{{ num }}</a></li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</section>

<style>
.tag.planning { background-color: #3273dc; color: white; }
.tag.in_progress { background-color: #ffdd57; color: #363636; }
.tag.completed { background-color: #48c774; color: white; }
.tag.on_hold { background-color: #f14668; color: white; }
</style>
{% endblock %}
