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

{% block title %}{% trans "Search Standards" %}{% 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 "Search Standards" %}</h1>
            <p class="text-muted mb-0">{{ hospital.name }}</p>
        </div>
        <a href="{% url 'standards:dashboard' %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>{% trans "Back to Dashboard" %}
        </a>
    </div>

    <!-- Search Form -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-6">
                    <label for="q" class="form-label">{% trans "Search" %}</label>
                    <input type="text" class="form-control" name="q" id="q"
                           value="{{ query }}" placeholder="{% trans 'Search by code, title, or description...' %}">
                </div>
                <div class="col-md-3">
                    <label for="source" class="form-label">{% trans "Source" %}</label>
                    <select class="form-select" name="source" id="source">
                        <option value="">{% trans "All Sources" %}</option>
                        {% for source in sources %}
                        <option value="{{ source.id }}" {% if source_filter == source.id|stringformat:"s" %}selected{% endif %}>
                            {{ source.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="category" class="form-label">{% trans "Category" %}</label>
                    <select class="form-select" name="category" id="category">
                        <option value="">{% trans "All Categories" %}</option>
                        {% for category in categories %}
                        <option value="{{ category.id }}" {% if category_filter == category.id|stringformat:"s" %}selected{% endif %}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search me-2"></i>{% trans "Search" %}
                    </button>
                    <a href="{% url 'standards:search' %}" class="btn btn-outline-secondary ms-2">
                        {% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Results -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                {% if query or source_filter or category_filter %}
                {% trans "Search Results" %} ({{ standards.count }})
                {% else %}
                {% trans "All Standards" %} ({{ standards.count }})
                {% endif %}
            </h5>
        </div>
        <div class="card-body">
            {% if standards %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% trans "Code" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Source" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for standard in standards %}
                        <tr>
                            <td><strong>{{ standard.code }}</strong></td>
                            <td>
                                <a href="{% url 'standards:standard_detail' pk=standard.id %}">
                                    {{ standard.title }}
                                </a>
                            </td>
                            <td>{{ standard.source.name }}</td>
                            <td>{{ standard.category.name }}</td>
                            <td>
                                {% if standard.department %}
                                {{ standard.department.name }}
                                {% else %}
                                <em>{% trans "All Departments" %}</em>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'standards:standard_detail' pk=standard.id %}"
                                   class="btn btn-sm btn-primary">
                                    <i class="fas fa-eye me-1"></i>{% trans "View" %}
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-search fa-3x text-muted mb-3"></i>
                <p class="text-muted">{% trans "No standards found" %}</p>
                {% if query or source_filter or category_filter %}
                <a href="{% url 'standards:search' %}" class="btn btn-outline-primary">
                    {% trans "Clear Filters" %}
                </a>
                {% endif %}
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
