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

{% block title %}{% trans "PX Sources" %}{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-lightning-fill text-warning me-2"></i>
                {% trans "PX Sources" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Manage patient experience source channels" %}</p>
        </div>
        <div>
            {% comment %} {% if request.user.is_px_admin %} {% endcomment %}
            <a href="{% url 'px_sources:source_create' %}" class="btn btn-primary">
                {% action_icon 'create' %} {% trans "Add Source" %}
            </a>
            {% comment %} {% endif %} {% endcomment %}
        </div>
    </div>

    <!-- Sources Card -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                {% action_icon 'filter' %} {% trans "Sources" %}
            </h5>
        </div>
        <div class="card-body">
            <!-- Filters -->
            <div class="row mb-3">
                <div class="col-md-4">
                    <input type="text" id="search-input" class="form-control" 
                           placeholder="{% trans 'Search...' %}" value="{{ search }}">
                </div>
                <div class="col-md-3">
                    <select id="status-filter" class="form-select">
                        <option value="">{% trans "All Status" %}</option>
                        <option value="true" {% if is_active == 'true' %}selected{% endif %}>
                            {% trans "Active" %}
                        </option>
                        <option value="false" {% if is_active == 'false' %}selected{% endif %}>
                            {% trans "Inactive" %}
                        </option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button id="apply-filters" class="btn btn-secondary w-100">
                        {% action_icon 'filter' %} {% trans "Filter" %}
                    </button>
                </div>
            </div>

            <!-- Sources Table -->
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Name (EN)" %}</th>
                            <th>{% trans "Name (AR)" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for source in sources %}
                        <tr>
                            <td><strong>{{ source.name_en }}</strong></td>
                            <td dir="rtl">{{ source.name_ar|default:"-" }}</td>
                            <td class="text-muted">{{ source.description|default:"-"|truncatewords:10 }}</td>
                            <td>
                                {% if source.is_active %}
                                    <span class="badge bg-success">{% trans "Active" %}</span>
                                {% else %}
                                    <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'px_sources:source_detail' source.pk %}" 
                                   class="btn btn-sm btn-info" title="{% trans 'View' %}">
                                    {% action_icon 'view' %}
                                </a>
                                {% if request.user.is_px_admin %}
                                <a href="{% url 'px_sources:source_edit' source.pk %}" 
                                   class="btn btn-sm btn-warning" title="{% trans 'Edit' %}">
                                    {% action_icon 'edit' %}
                                </a>
                                <a href="{% url 'px_sources:source_delete' source.pk %}" 
                                   class="btn btn-sm btn-danger" title="{% trans 'Delete' %}">
                                    {% action_icon 'delete' %}
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="5" class="text-center py-4">
                                <p class="text-muted mb-2">
                                    <i class="bi bi-inbox fs-1"></i>
                                </p>
                                <p>{% trans "No sources found. Click 'Add Source' to create one." %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Apply filters button
    document.getElementById('apply-filters').addEventListener('click', function() {
        const search = document.getElementById('search-input').value;
        const isActive = document.getElementById('status-filter').value;
        
        let url = new URL(window.location.href);
        if (search) url.searchParams.set('search', search);
        else url.searchParams.delete('search');
        
        if (isActive) url.searchParams.set('is_active', isActive);
        else url.searchParams.delete('is_active');
        
        window.location.href = url.toString();
    });
    
    // Enter key on search input
    document.getElementById('search-input').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            document.getElementById('apply-filters').click();
        }
    });
});
</script>
{% endblock %}
