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

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

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}

.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.source-type-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.source-type-badge.internal {
    background: linear-gradient(135deg, #dbeafe, #bfdbfe);
    color: #1e40af;
}

.source-type-badge.external {
    background: linear-gradient(135deg, #dcfce7, #bbf7d0);
    color: #166534;
}

.source-type-badge.partner {
    background: linear-gradient(135deg, #fef3c7, #fde68a);
    color: #92400e;
}

.source-type-badge.government {
    background: linear-gradient(135deg, #e0e7ff, #c7d2fe);
    color: #3730a3;
}

.usage-stat {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.75rem;
    color: var(--hh-slate);
}

.data-table th {
    background: linear-gradient(135deg, #f8fafc, #e0f2fe);
    padding: 0.875rem 1rem;
    text-align: left;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #005696;
    border-bottom: 2px solid #bae6fd;
}

.data-table td {
    padding: 1rem;
    border-bottom: 1px solid #f1f5f9;
    color: #475569;
    font-size: 0.875rem;
}

.data-table tbody tr {
    transition: background-color 0.2s ease;
}

.data-table tbody tr:hover {
    background-color: #f8fafc;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.animate-in {
    animation: fadeIn 0.5s ease-out forwards;
}
</style>
{% endblock %}

{% block content %}
<div class="px-4 py-6">
    <!-- Page Header -->
    <div class="page-header-gradient animate-in">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="radio" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "PX Sources" %}</h1>
                    <p class="text-white/80">{% trans "Manage patient experience source channels" %}</p>
                </div>
            </div>
            <a href="{% url 'px_sources:source_create' %}"
               class="inline-flex items-center gap-2 bg-white text-navy px-5 py-2.5 rounded-xl font-bold hover:bg-gray-100 transition shadow-lg">
                <i data-lucide="plus" class="w-4 h-4"></i>
                {% trans "Add Source" %}
            </a>
        </div>
    </div>

    <!-- Sources Card -->
    <div class="section-card animate-in">
        <div class="section-header">
            <div class="section-icon bg-blue-100">
                <i data-lucide="database" class="w-5 h-5 text-blue-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "All Sources" %}</h2>
        </div>

        <div class="p-6">
            <!-- Filters -->
            <div class="flex flex-wrap gap-3 mb-6">
                <div class="flex-1 min-w-[250px]">
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Search" %}</label>
                    <div class="relative">
                        <i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate"></i>
                        <input type="text" id="search-input"
                               class="w-full pl-10 pr-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue focus:ring-2 focus:ring-blue/20 transition"
                               placeholder="{% trans 'Search by name, code, or description...' %}" value="{{ search }}">
                    </div>
                </div>
                <div class="w-48">
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% trans "Status" %}</label>
                    <select id="status-filter"
                            class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white transition">
                        <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="flex items-end">
                    <button id="apply-filters"
                            class="inline-flex items-center gap-2 px-6 py-2.5 bg-navy text-white rounded-xl font-bold hover:bg-blue transition shadow-lg">
                        <i data-lucide="filter" class="w-4 h-4"></i>
                        {% trans "Filter" %}
                    </button>
                </div>
            </div>

            <!-- Sources Table -->
            <div class="overflow-x-auto">
                <table class="w-full data-table">
                    <thead>
                        <tr>
                            <th>{% trans "Code" %}</th>
                            <th>{% trans "Name (EN)" %}</th>
                            <th>{% trans "Name (AR)" %}</th>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Usage" %}</th>
                            <th class="text-center">{% trans "Status" %}</th>
                            <th class="text-center">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for source in sources %}
                        <tr>
                            <td>
                                <span class="font-mono text-xs bg-slate-100 px-2 py-1 rounded">{{ source.code }}</span>
                            </td>
                            <td>
                                <a href="{% url 'px_sources:source_detail' source.pk %}"
                                   class="font-semibold text-navy hover:text-blue transition">
                                    {{ source.get_localized_name }}
                                </a>
                            </td>
                            <td dir="rtl">
                                <span class="text-slate">{{ source.name_ar|default:"-" }}</span>
                            </td>
                            <td>
                                <span class="source-type-badge {{ source.source_type }}">
                                    {% if source.source_type == 'internal' %}
                                        <i data-lucide="building" class="w-3 h-3"></i>
                                    {% elif source.source_type == 'external' %}
                                        <i data-lucide="globe" class="w-3 h-3"></i>
                                    {% elif source.source_type == 'partner' %}
                                        <i data-lucide="handshake" class="w-3 h-3"></i>
                                    {% elif source.source_type == 'government' %}
                                        <i data-lucide="landmark" class="w-3 h-3"></i>
                                    {% else %}
                                        <i data-lucide="circle" class="w-3 h-3"></i>
                                    {% endif %}
                                    {{ source.get_source_type_display }}
                                </span>
                            </td>
                            <td>
                                <div class="flex flex-col gap-1">
                                    <span class="usage-stat">
                                        <i data-lucide="file-text" class="w-3 h-3"></i>
                                        {{ source.total_complaints }} {% trans "complaints" %}
                                    </span>
                                    <span class="usage-stat">
                                        <i data-lucide="help-circle" class="w-3 h-3"></i>
                                        {{ source.total_inquiries }} {% trans "inquiries" %}
                                    </span>
                                </div>
                            </td>
                            <td class="text-center">
                                {% if source.is_active %}
                                    <span class="inline-flex items-center gap-1.5 px-3 py-1.5 bg-green-100 text-green-700 rounded-full text-xs font-bold">
                                        <i data-lucide="check-circle" class="w-3.5 h-3.5"></i>
                                        {% trans "Active" %}
                                    </span>
                                {% else %}
                                    <span class="inline-flex items-center gap-1.5 px-3 py-1.5 bg-slate-100 text-slate-600 rounded-full text-xs font-bold">
                                        <i data-lucide="x-circle" class="w-3.5 h-3.5"></i>
                                        {% trans "Inactive" %}
                                    </span>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                <div class="flex items-center justify-center gap-1.5">
                                    <a href="{% url 'px_sources:source_detail' source.pk %}"
                                       class="p-2 text-blue hover:bg-blue-50 rounded-lg transition" title="{% trans 'View' %}">
                                        <i data-lucide="eye" class="w-4 h-4"></i>
                                    </a>
                                    {% if user.is_px_admin or user.is_hospital_admin %}
                                    <a href="{% url 'px_sources:source_edit' source.pk %}"
                                       class="p-2 text-navy hover:bg-navy/10 rounded-lg transition" title="{% trans 'Edit' %}">
                                        <i data-lucide="edit" class="w-4 h-4"></i>
                                    </a>
                                    <form action="{% url 'px_sources:source_toggle_status' source.pk %}" method="post" style="display:inline">
                                        {% csrf_token %}
                                        <button type="submit" class="p-2 hover:bg-slate-100 rounded-lg transition" 
                                                title="{% if source.is_active %}{% trans 'Deactivate' %}{% else %}{% trans 'Activate' %}{% endif %}">
                                            {% if source.is_active %}
                                                <i data-lucide="pause" class="w-4 h-4 text-yellow-600"></i>
                                            {% else %}
                                                <i data-lucide="play" class="w-4 h-4 text-green-600"></i>
                                            {% endif %}
                                        </button>
                                    </form>
                                    {% if user.is_px_admin %}
                                    <a href="{% url 'px_sources:source_delete' source.pk %}"
                                       class="p-2 text-red-500 hover:bg-red-50 rounded-lg transition" title="{% trans 'Delete' %}">
                                        <i data-lucide="trash-2" class="w-4 h-4"></i>
                                    </a>
                                    {% endif %}
                                    {% endif %}
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="py-12 text-center">
                                <div class="flex flex-col items-center">
                                    <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                                        <i data-lucide="database" class="w-8 h-8 text-slate-400"></i>
                                    </div>
                                    <p class="text-slate font-medium">{% trans "No sources found" %}</p>
                                    <p class="text-slate text-sm mt-1">{% trans "Add your first source to get started" %}</p>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();

    // Filter functionality
    document.getElementById('apply-filters').addEventListener('click', function() {
        const search = document.getElementById('search-input').value;
        const status = document.getElementById('status-filter').value;

        let url = new URL(window.location.href);
        url.searchParams.set('search', search);
        if (status) {
            url.searchParams.set('is_active', status);
        } else {
            url.searchParams.delete('is_active');
        }
        window.location.href = url.toString();
    });

    // Enter key to search
    document.getElementById('search-input').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            document.getElementById('apply-filters').click();
        }
    });
});
</script>
{% endblock %}
