{% extends "base.html" %}

{% block title %}Subir documento - ARCH{% endblock %}

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

{% block page_title %}Subir nuevo documento{% endblock %}

{% block content %}
<div class="row">
    <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">Subir documento</li>
            </ol>
        </nav>
        
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Subir documento a proyecto: {{ project.descripcion }}</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ url_for('documents.upload', project_id=project.codigo|replace('PROJ', '')|int) }}" enctype="multipart/form-data">
                    {{ form.hidden_tag() }}
                    
                    <div class="mb-3">
                        {{ form.nombre.label(class="form-label") }}
                        {{ form.nombre(class="form-control") }}
                        {% if form.nombre.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.nombre.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">
                            Nombre descriptivo para identificar el documento en el sistema.
                        </div>
                    </div>
                    
                    <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", accept=".pdf,.doc,.docx,.txt,.zip,.rar,.dwg,.xlsx,.pptx") }}
                        {% if form.file.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.file.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>
                    
                    <div class="d-flex justify-content-between">
                        <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>
                        
                        {{ form.submit(class="btn btn-primary") }}
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Tipos de Archivo Permitidos</h5>
            </div>
            <div class="card-body">
                <div class="list-group">
                    <div class="list-group-item">
                        <div class="d-flex align-items-center">
                            <i class="bi bi-file-earmark-pdf text-danger me-2 fs-4"></i>
                            <div>
                                <h6 class="mb-0">PDF</h6>
                                <small class="text-muted">Documentos, manuales, informes</small>
                            </div>
                        </div>
                    </div>
                    <div class="list-group-item">
                        <div class="d-flex align-items-center">
                            <i class="bi bi-file-earmark-word text-primary me-2 fs-4"></i>
                            <div>
                                <h6 class="mb-0">Word</h6>
                                <small class="text-muted">Documentos editables (.doc, .docx)</small>
                            </div>
                        </div>
                    </div>
                    <div class="list-group-item">
                        <div class="d-flex align-items-center">
                            <i class="bi bi-file-earmark-excel text-success me-2 fs-4"></i>
                            <div>
                                <h6 class="mb-0">Excel</h6>
                                <small class="text-muted">Hojas de cálculo (.xlsx)</small>
                            </div>
                        </div>
                    </div>
                    <div class="list-group-item">
                        <div class="d-flex align-items-center">
                            <i class="bi bi-file-earmark-zip text-warning me-2 fs-4"></i>
                            <div>
                                <h6 class="mb-0">Archivos comprimidos</h6>
                                <small class="text-muted">ZIP, RAR</small>
                            </div>
                        </div>
                    </div>
                    <div class="list-group-item">
                        <div class="d-flex align-items-center">
                            <i class="bi bi-file-earmark-text text-secondary me-2 fs-4"></i>
                            <div>
                                <h6 class="mb-0">Otros formatos</h6>
                                <small class="text-muted">DWG, TXT, etc.</small>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="alert alert-info mt-3">
                    <i class="bi bi-info-circle"></i> El tamaño máximo de archivo permitido es de 100MB.
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/documents.js') }}"></script>
<script>
    // Validación adicional del formulario
    $(document).ready(function() {
        $('form').on('submit', function(e) {
            var fileInput = $('#file')[0];
            
            if (fileInput.files.length > 0) {
                var fileSize = fileInput.files[0].size; // bytes
                var maxSize = 100 * 1024 * 1024; // 100MB
                
                if (fileSize > maxSize) {
                    e.preventDefault();
                    alert('El archivo es demasiado grande. El tamaño máximo permitido es 100MB.');
                    return false;
                }
            }
            
            return true;
        });
    });
</script>
{% endblock %}