{% extends "base.html" %}

{% block title %}Versiones de documento - ARCH{% endblock %}

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

{% block page_title %}Versiones de documento{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col-md-8">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ url_for('projects.list') }}">Proyectos</a></li>
                <li class="breadcrumb-item"><a href="{{ url_for('projects.view', project_id=project.codigo|replace('PROJ', '')|int) }}">{{ project.descripcion }}</a></li>
                <li class="breadcrumb-item active">{{ document.original_filename }}</li>
            </ol>
        </nav>
        
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Información del Documento</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <dl class="row">
                            <dt class="col-sm-4">Archivo original:</dt>
                            <dd class="col-sm-8">{{ document.original_filename }}</dd>
                            
                            <dt class="col-sm-4">ID:</dt>
                            <dd class="col-sm-8">{{ document.document_id }}</dd>
                        </dl>
                    </div>
                    <div class="col-md-6">
                        <dl class="row">
                            <dt class="col-sm-4">Versiones:</dt>
                            <dd class="col-sm-8">{{ document.versions|length }}</dd>
                            
                            <dt class="col-sm-4">Última versión:</dt>
                            <dd class="col-sm-8">
                                v{{ document.versions|map(attribute='version')|list|max }}
                                ({{ document.versions|selectattr('version', 'eq', document.versions|map(attribute='version')|list|max)|map(attribute='created_at')|first|replace('T', ' ')|replace('Z', '') }})
                            </dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Acciones</h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="{{ url_for('documents.download_latest', project_id=project.codigo|replace('PROJ', '')|int, document_id=document.document_id|replace(document.document_id.split('_')[0] + '_', '')|int) }}" 
                       class="btn btn-success">
                        <i class="bi bi-download"></i> Descargar última versión
                    </a>
                    
                    <a href="{{ url_for('projects.view', project_id=project.codigo|replace('PROJ', '')|int) }}" 
                       class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-left"></i> Volver al proyecto
                    </a>
                    
                    {% if current_user.has_permission(9000) and project.estado == 'activo' %}
                    <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
                        <i class="bi bi-trash"></i> Eliminar documento
                    </button>
                    {% endif %}
                </div>
            </div>
        </div>
        
        {% if project.estado == 'activo' and current_user.has_permission(1000) %}
        <div class="card mt-3">
            <div class="card-header">
                <h5 class="card-title mb-0">Nueva Versión</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ url_for('documents.upload_version', project_id=project.codigo|replace('PROJ', '')|int, document_id=document.document_id|replace(document.document_id.split('_')[0] + '_', '')|int) }}" enctype="multipart/form-data">
                    {{ form.hidden_tag() }}
                    {{ form.document_id }}
                    
                    <div class="mb-3">
                        {{ form.description.label(class="form-label") }}
                        {{ form.description(class="form-control", rows=3) }}
                        {% if form.description.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.description.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        {{ form.file.label(class="form-label") }}
                        {{ form.file(class="form-control") }}
                        {% if form.file.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.file.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">
                            El archivo debe ser del mismo tipo que el original.
                        </div>
                    </div>
                    
                    <div class="d-grid">
                        {{ form.submit(class="btn btn-primary") }}
                    </div>
                </form>
            </div>
        </div>
        {% endif %}
    </div>
</div>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Historial de Versiones</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover table-striped">
                        <thead class="table-light">
                            <tr>
                                <th>Versión</th>
                                <th>Nombre de archivo</th>
                                <th>Fecha de creación</th>
                                <th>Usuario</th>
                                <th>Tamaño</th>
                                <th>Descripción</th>
                                <th>Descargas</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for version in document.versions|sort(attribute='version', reverse=true) %}
                            <tr>
                                <td>
                                    <span class="badge bg-secondary">v{{ version.version }}</span>
                                    {% if loop.first %}
                                    <span class="badge bg-success">Última</span>
                                    {% endif %}
                                </td>
                                <td>{{ version.filename }}</td>
                                <td>{{ version.created_at|replace('T', ' ')|replace('Z', '') }}</td>
                                <td>{{ version.created_by }}</td>
                                <td>{{ (version.file_size / 1024)|round(1) }} KB</td>
                                <td>{{ version.description }}</td>
                                <td>{{ version.downloads|length }}</td>
                                <td>
                                    <a href="{{ url_for('documents.download', project_id=project.codigo|replace('PROJ', '')|int, document_id=document.document_id|replace(document.document_id.split('_')[0] + '_', '')|int, version=version.version) }}" 
                                       class="btn btn-sm btn-primary">
                                        <i class="bi bi-download"></i>
                                    </a>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal para eliminar documento -->
{% if current_user.has_permission(9000) and project.estado == 'activo' %}
<div class="modal fade" id="deleteModal" 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 documento <strong>{{ document.original_filename }}</strong>?</p>
                <p class="text-danger">Esta acción no se puede deshacer y eliminará todas las versiones del documento.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                <form action="{{ url_for('documents.delete', project_id=project.codigo|replace('PROJ', '')|int, document_id=document.document_id|replace(document.document_id.split('_')[0] + '_', '')|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 %}
{% endblock %}

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