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

{% block title %}{{ standard.code }} - {% trans "Standard Details" %}{% 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">{{ standard.code }}</h1>
            <p class="text-muted mb-0">{{ standard.title }}</p>
        </div>
        <a href="{% url 'standards:dashboard' %}" class="btn btn-outline-secondary">
            {% action_icon "back" %} {% trans "Back to Dashboard" %}
        </a>
    </div>

    <!-- Standard Information -->
    <div class="row mb-4">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Standard Information" %}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm">
                        <tr>
                            <th width="30%">{% trans "Source" %}</th>
                            <td>{{ standard.source.name }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Category" %}</th>
                            <td>{{ standard.category.name }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Department" %}</th>
                            <td>
                                {% if standard.department %}
                                {{ standard.department.name }}
                                {% else %}
                                <em>{% trans "All Departments" %}</em>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th>{% trans "Effective Date" %}</th>
                            <td>
                                {% if standard.effective_date %}
                                {{ standard.effective_date|date:"Y-m-d" }}
                                {% else %}
                                <em>{% trans "Not specified" %}</em>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th>{% trans "Review Date" %}</th>
                            <td>
                                {% if standard.review_date %}
                                {{ standard.review_date|date:"Y-m-d" }}
                                {% else %}
                                <em>{% trans "Not specified" %}</em>
                                {% endif %}
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Compliance Summary" %}</h5>
                </div>
                <div class="card-body">
                    <div class="text-center">
                        <h2>{{ compliance_records.count }}</h2>
                        <p class="text-muted">{% trans "Assessments" %}</p>
                    </div>
                    <div class="mt-3">
                        <span class="badge bg-success me-1">
                            {{ compliance_records|count_by:"status:met" }} {% trans "Met" %}
                        </span>
                        <span class="badge bg-warning me-1">
                            {{ compliance_records|count_by:"status:partially_met" }} {% trans "Partially Met" %}
                        </span>
                        <span class="badge bg-danger me-1">
                            {{ compliance_records|count_by:"status:not_met" }} {% trans "Not Met" %}
                        </span>
                        <span class="badge bg-secondary">
                            {{ compliance_records|count_by:"status:not_assessed" }} {% trans "Not Assessed" %}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Description -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title mb-0">{% trans "Description" %}</h5>
        </div>
        <div class="card-body">
            {{ standard.description|linebreaks }}
        </div>
    </div>

    <!-- Compliance Records -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">{% trans "Compliance by Department" %}</h5>
        </div>
        <div class="card-body">
            {% if compliance_records %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Last Assessed" %}</th>
                            <th>{% trans "Assessor" %}</th>
                            <th>{% trans "Evidence" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for record in compliance_records %}
                        <tr>
                            <td>
                                <a href="{% url 'standards:department_standards' pk=record.department.id %}">
                                    {{ record.department.name }}
                                </a>
                            </td>
                            <td>
                                <span class="badge {% if record.status == 'met' %}bg-success{% elif record.status == 'partially_met' %}bg-warning{% elif record.status == 'not_met' %}bg-danger{% else %}bg-secondary{% endif %}">
                                    {{ record.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if record.last_assessed_date %}
                                {{ record.last_assessed_date|date:"Y-m-d" }}
                                {% else %}
                                <em>{% trans "Not assessed" %}</em>
                                {% endif %}
                            </td>
                            <td>
                                {% if record.assessor %}
                                {{ record.assessor.get_full_name }}
                                {% else %}
                                <em>-</em>
                                {% endif %}
                            </td>
                            <td>
                                <span class="badge bg-info">
                                    {% action_icon "attachment" %}
                                    {{ record.attachments.count }}
                                </span>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <p class="text-muted text-center">{% trans "No compliance assessments yet" %}</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
