{% extends 'layouts/base.html' %}
{% load i18n %}

{% block title %}{% trans "Upload Evidence" %}{% endblock %}

{% block content %}
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">{% trans "Upload Evidence Attachment" %}</h1>
            <p class="text-muted mb-0">{{ compliance.standard.code }} - {{ compliance.standard.title }}</p>
        </div>
        <a href="{% url 'standards:standard_compliance_update' compliance_id=compliance.id %}"
           class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>{% trans "Back to Assessment" %}
        </a>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Upload New Attachment" %}</h5>
                </div>
                <div class="card-body">
                    <form method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label for="{{ form.file.id_for_label }}" class="form-label">
                                {% trans "File" %}
                            </label>
                            {{ form.file }}
                            {% if form.file.errors %}
                            <div class="text-danger">{{ form.file.errors }}</div>
                            {% endif %}
                            <small class="form-text text-muted">
                                {% trans "Accepted formats: PDF, DOC, DOCX, XLS, XLSX, JPG, PNG" %}
                            </small>
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.description.id_for_label }}" class="form-label">
                                {% trans "Description" %}
                            </label>
                            {{ form.description }}
                            {% if form.description.errors %}
                            <div class="text-danger">{{ form.description.errors }}</div>
                            {% endif %}
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-upload me-2"></i>{% trans "Upload" %}
                        </button>
                        <a href="{% url 'standards:standard_compliance_update' compliance_id=compliance.id %}"
                           class="btn btn-outline-secondary ms-2">
                            {% trans "Cancel" %}
                        </a>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Compliance Details" %}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm">
                        <tr>
                            <th width="40%">{% trans "Standard" %}</th>
                            <td>{{ compliance.standard.code }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Title" %}</th>
                            <td>{{ compliance.standard.title }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Department" %}</th>
                            <td>{{ compliance.department.name }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Status" %}</th>
                            <td>
                                <span class="badge {% if compliance.status == 'met' %}bg-success{% elif compliance.status == 'partially_met' %}bg-warning{% elif compliance.status == 'not_met' %}bg-danger{% else %}bg-secondary{% endif %}">
                                    {{ compliance.get_status_display }}
                                </span>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Existing Attachments -->
    {% if compliance.attachments.all %}
    <div class="card mt-4">
        <div class="card-header">
            <h5 class="card-title mb-0">{% trans "Existing Attachments" %}</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% trans "File Name" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "Uploaded By" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for attachment in compliance.attachments.all %}
                        <tr>
                            <td>
                                <i class="fas fa-file me-2"></i>{{ attachment.filename }}
                            </td>
                            <td>{{ attachment.description|default:"-" }}</td>
                            <td>{{ attachment.uploaded_by.get_full_name }}</td>
                            <td>{{ attachment.created_at|date:"Y-m-d H:i" }}</td>
                            <td>
                                <a href="{{ attachment.file.url }}" target="_blank"
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-download"></i>
                                </a>
                                <a href="{% url 'standards:attachment_delete' pk=attachment.id %}"
                                   class="btn btn-sm btn-outline-danger">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
