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

{% block title %}Routing Rules - PX360{% endblock %}

{% block content %}
<!-- Header -->
<header class="mb-6">
    <div class="flex justify-between items-start">
        <div>
            <h1 class="text-2xl font-bold text-navy">
                <i data-lucide="git-branch" class="w-6 h-6 inline-block mr-2"></i>
                {% trans "Routing Rules" %}
            </h1>
            <p class="text-sm text-slate mt-1">{% trans "Manage action routing and assignment rules" %}</p>
        </div>
        <a href="{% url 'config:dashboard' %}" class="inline-flex items-center gap-2 px-4 py-2 border border-slate-200 rounded-xl text-sm font-semibold text-slate hover:bg-light transition">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            {% trans "Back to Config" %}
        </a>
    </div>
</header>

<!-- Routing Rules Table -->
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
    <div class="flex justify-between items-center p-5 border-b border-slate-100">
        <h3 class="font-bold text-navy flex items-center gap-2 text-sm">
            <i data-lucide="list" class="w-4 h-4"></i>
            {% trans "Routing Rules List" %}
        </h3>
        <div class="text-xs text-slate font-medium">
            {% trans "Total" %}: <span class="font-bold text-navy">{{ routing_rules.count }}</span> {% trans "rules" %}
        </div>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-slate-50 border-b border-slate-100">
                <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                    <th class="px-5 py-3 text-left">{% trans "Priority" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Name" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Source Type" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Severity" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Hospital" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Assign To" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Status" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for rule in routing_rules %}
                <tr class="hover:bg-light/30 transition">
                    <td class="px-5 py-4">
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-navy text-white">{{ rule.priority }}</span>
                    </td>
                    <td class="px-5 py-4">
                        <div class="font-bold text-navy">{{ rule.name }}</div>
                        {% if rule.description %}
                        <div class="text-xs text-slate mt-0.5">{{ rule.description|truncatewords:10 }}</div>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if rule.source_type %}
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-600">{{ rule.get_source_type_display }}</span>
                        {% else %}
                        <span class="text-slate text-xs">{% trans "Any" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if rule.severity %}
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-yellow-100 text-yellow-700">{{ rule.get_severity_display }}</span>
                        {% else %}
                        <span class="text-slate text-xs">{% trans "Any" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if rule.hospital %}
                        <span class="text-slate-800 text-xs">{{ rule.hospital.get_localized_name }}</span>
                        {% else %}
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{% trans "All" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if rule.assign_to_user %}
                        <span class="text-slate-800 text-xs">{{ rule.assign_to_user.get_full_name }}</span>
                        {% elif rule.assign_to_department %}
                        <span class="text-slate-800 text-xs">{{ rule.assign_to_department.get_localized_name }}</span>
                        {% elif rule.assign_to_role %}
                        <span class="text-slate-800 text-xs">{{ rule.assign_to_role }}</span>
                        {% else %}
                        <span class="text-slate text-xs">-</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if rule.is_active %}
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600">{% trans "Active" %}</span>
                        {% else %}
                        <span class="px-2 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-500">{% trans "Inactive" %}</span>
                        {% endif %}
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="px-5 py-12 text-center">
                        <div class="w-16 h-16 mx-auto mb-3 bg-slate-100 rounded-2xl flex items-center justify-center">
                            <i data-lucide="git-branch" class="w-8 h-8 text-slate-300"></i>
                        </div>
                        <p class="text-slate font-medium">{% trans "No routing rules found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}