{% extends "base.html" %}

{% block title %}Proyectos - ARCH{% endblock %}

{% block styles %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/projects.css') }}">
{% endblock %}

{% block page_title %}Proyectos{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Filtrar proyectos</h5>
            </div>
            <div class="card-body">
                <form id="filter-form" method="GET" action="{{ url_for('projects.list') }}" class="row g-3">
                    <div class="col-md-6">
                        {{ filter_form.cliente.label(class="form-label") }}
                        {{ filter_form.cliente(class="form-control") }}
                    </div>
                    <div class="col-md-6">
                        {{ filter_form.estado.label(class="form-label") }}
                        {{ filter_form.estado(class="form-select") }}
                    </div>
                    <div class="col-md-3">
                        {{ filter_form.ano_inicio.label(class="form-label") }}
                        {{ filter_form.ano_inicio(class="form-control", type="number") }}
                    </div>
                    <div class="col-md-3">
                        {{ filter_form.ano_fin.label(class="form-label") }}
                        {{ filter_form.ano_fin(class="form-control", type="number") }}
                    </div>
                    <div class="col-md-6">
                        {{ filter_form.descripcion.label(class="form-label") }}
                        {{ filter_form.descripcion(class="form-control") }}
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search"></i> Filtrar
                        </button>
                        <a href="{{ url_for('projects.list') }}" class="btn btn-outline-secondary">
                            Limpiar filtros
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-4 d-flex align-items-center">
        {% if current_user.has_permission(1000) %}
        <div class="text-center w-100">
            <a href="{{ url_for('projects.create') }}" class="btn btn-success btn-lg">
                <i class="bi bi-plus-circle"></i> Nuevo Proyecto
            </a>
        </div>
        {% endif %}
    </div>
</div>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">Lista de Proyectos</h5>
                    <span class="badge bg-primary">{{ projects|length }} proyectos</span>
                </div>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover table-striped mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Código</th>
                                <th>Descripción</th>
                                <th>Cliente</th>
                                <th>Año</th>
                                <th>Creado por</th>
                                <th>Estado</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% if projects %}
                                {% for project in projects %}
                                <tr>
                                    <td>{{ project.codigo }}</td>
                                    <td>
                                        {% if project.proyecto_padre %}
                                        <span class="badge bg-secondary">Subproyecto</span>
                                        {% endif %}
                                        {{ project.descripcion }}
                                    </td>
                                    <td>{{ project.cliente }}</td>
                                    <td>{{ project.ano_creacion }}</td>
                                    <td>{{ project.creado_por }}</td>
                                    <td>
                                        {% if project.estado == 'activo' %}
                                        <span class="badge bg-success">Activo</span>
                                        {% else %}
                                        <span class="badge bg-danger">Inactivo</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{{ url_for('projects.view', project_id=project.codigo|replace('PROJ', '')|int) }}" 
                                               class="btn btn-primary" title="Ver">
                                                <i class="bi bi-eye"></i>
                                            </a>
                                            
                                            {% if current_user.has_permission(5000) %}
                                            <a href="{{ url_for('projects.edit', project_id=project.codigo|replace('PROJ', '')|int) }}" 
                                               class="btn btn-warning" title="Editar">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            {% endif %}
                                            
                                            {% if current_user.has_permission(9000) and project.estado == 'activo' %}
                                            <button type="button" class="btn btn-danger" title="Eliminar"
                                                   data-bs-toggle="modal" data-bs-target="#deleteModal{{ project.codigo|replace('PROJ', '')|int }}">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                            {% endif %}
                                        </div>
                                        
                                        <!-- Modal de confirmación para eliminar -->
                                        {% if current_user.has_permission(9000) and project.estado == 'activo' %}
                                        <div class="modal fade" id="deleteModal{{ project.codigo|replace('PROJ', '')|int }}" tabindex="-1">
                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">Confirmar eliminación</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <p>¿Está seguro de que desea eliminar el proyecto <strong>{{ project.descripcion }}</strong>?</p>
                                                        <p class="text-danger">Esta acción marcará el proyecto como inactivo.</p>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                                                        <form action="{{ url_for('projects.delete', project_id=project.codigo|replace('PROJ', '')|int) }}" method="POST">
                                                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                                            <button type="submit" class="btn btn-danger">Eliminar</button>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            {% else %}
                                <tr>
                                    <td colspan="7" class="text-center py-4">
                                        No se encontraron proyectos.
                                        {% if request.args %}
                                            <a href="{{ url_for('projects.list') }}">Quitar filtros</a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/projects.js') }}"></script>
{% endblock %}