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

{% block title %}{% translate "Escalation Rules" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    .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;
    }

    .section-icon.primary {
        background: linear-gradient(135deg, #005696, #007bbd);
        color: white;
    }

    .section-icon.secondary {
        background: linear-gradient(135deg, #f1f5f9, #e2e8f0);
        color: #005696;
    }

    .data-table th {
        background: linear-gradient(135deg, var(--hh-light), #e0f2fe);
        padding: 0.875rem 1rem;
        text-align: left;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--hh-navy);
        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: var(--hh-light);
    }

    .hh-btn-primary {
        background: linear-gradient(135deg, var(--hh-navy) 0%, var(--hh-blue) 100%);
        color: white;
        padding: 0.625rem 1.25rem;
        border-radius: 0.75rem;
        font-weight: 600;
        border: none;
        cursor: pointer;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none;
        font-size: 0.875rem;
    }

    .hh-btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 16px rgba(0, 86, 150, 0.3);
    }

    .hh-btn-secondary {
        background: white;
        color: #475569;
        padding: 0.625rem 1.25rem;
        border-radius: 0.75rem;
        font-weight: 600;
        border: 2px solid #e2e8f0;
        cursor: pointer;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none;
        font-size: 0.875rem;
    }

    .hh-btn-secondary:hover {
        background: #f1f5f9;
        border-color: #cbd5e1;
        transform: translateY(-1px);
    }

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

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

    .badge-secondary {
        background: linear-gradient(135deg, #f1f5f9, #e2e8f0);
        color: #475569;
    }

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

    .severity-low {
        background: linear-gradient(135deg, #dcfce7, #bbf7d0);
        color: #166534;
    }

    .severity-medium {
        background: linear-gradient(135deg, #fef3c7, #fde68a);
        color: #92400e;
    }

    .severity-high {
        background: linear-gradient(135deg, #fee2e2, #fecaca);
        color: #991b1b;
    }

    .severity-critical {
        background: linear-gradient(135deg, #7f1d1d, #991b1b);
        color: white;
    }

    .priority-low {
        background: linear-gradient(135deg, #f1f5f9, #e2e8f0);
        color: #475569;
    }

    .priority-medium {
        background: linear-gradient(135deg, #dbeafe, #bfdbfe);
        color: #1e40af;
    }

    .priority-high {
        background: linear-gradient(135deg, #fef3c7, #fde68a);
        color: #92400e;
    }

    .priority-urgent {
        background: linear-gradient(135deg, #fee2e2, #fecaca);
        color: #991b1b;
    }

    @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 items-center justify-between">
            <div>
                <h1 class="text-2xl font-bold mb-2">
                    <i data-lucide="arrow-up-circle" class="w-7 h-7 inline-block me-2"></i>
                    {% translate "Escalation Rules" %}
                </h1>
                <p class="text-white/90">{% translate "Configure automatic complaint escalation based on time thresholds" %}</p>
            </div>
            <a href="{% url 'complaints:escalation_rule_create' %}" class="hh-btn hh-btn-secondary">
                <i data-lucide="plus" class="w-4 h-4"></i>
                {% translate "Create Rule" %}
            </a>
        </div>
    </div>

    <!-- Filters -->
    <div class="section-card mb-6 animate-in">
        <div class="section-header">
            <div class="section-icon secondary">
                <i data-lucide="filter" class="w-5 h-5"></i>
            </div>
            <h2 class="text-lg font-bold text-navy m-0">{% translate "Filters" %}</h2>
        </div>
        <div class="p-6">
            <form method="get" class="flex flex-wrap gap-4">
                <div>
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% translate "Escalation Level" %}</label>
                    <select name="escalation_level" class="px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white">
                        <option value="">{% translate "All Levels" %}</option>
                        <option value="1" {% if filters.escalation_level == "1" %}selected{% endif %}>1</option>
                        <option value="2" {% if filters.escalation_level == "2" %}selected{% endif %}>2</option>
                        <option value="3" {% if filters.escalation_level == "3" %}selected{% endif %}>3</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-semibold text-slate mb-1.5">{% translate "Status" %}</label>
                    <select name="is_active" class="px-4 py-2.5 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-blue bg-white">
                        <option value="">{% translate "All" %}</option>
                        <option value="true" {% if filters.is_active == "true" %}selected{% endif %}>{% translate "Active" %}</option>
                        <option value="false" {% if filters.is_active == "false" %}selected{% endif %}>{% translate "Inactive" %}</option>
                    </select>
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="hh-btn hh-btn-primary h-[46px]">
                        <i data-lucide="search" class="w-4 h-4"></i>
                        {% translate "Apply Filters" %}
                    </button>
                    <a href="{% url 'complaints:escalation_rule_list' %}" class="hh-btn hh-btn-secondary h-[46px]">
                        <i data-lucide="x" class="w-4 h-4"></i>
                        {% translate "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Escalation Rules Table -->
    <div class="section-card animate-in">
        <div class="section-header">
            <div class="section-icon primary">
                <i data-lucide="arrow-up-circle" class="w-5 h-5"></i>
            </div>
            <h2 class="text-lg font-bold text-navy m-0">{% translate "All Escalation Rules" %}</h2>
        </div>
        <div class="p-0">
            {% if escalation_rules %}
            <div class="overflow-x-auto">
                <table class="w-full data-table">
                    <thead>
                        <tr>
                            <th>{% translate "Hospital" %}</th>
                            <th>{% translate "Rule Name" %}</th>
                            <th>{% translate "Level" %}</th>
                            <th>{% translate "Trigger Hours" %}</th>
                            <th>{% translate "Escalate To" %}</th>
                            <th>{% translate "Severity" %}</th>
                            <th>{% translate "Priority" %}</th>
                            <th>{% translate "Status" %}</th>
                            <th class="text-right">{% translate "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rule in escalation_rules %}
                        <tr>
                            <td>
                                <strong>{{ rule.hospital.name }}</strong>
                            </td>
                            <td>{{ rule.name }}</td>
                            <td>
                                <span class="badge badge-info">
                                    {% translate "Level" %} {{ rule.escalation_level }}
                                </span>
                            </td>
                            <td>{{ rule.trigger_hours }}h</td>
                            <td>
                                {% if rule.escalate_to_user %}
                                {{ rule.escalate_to_user.get_full_name }}
                                {% elif rule.escalate_to_role %}
                                <span class="badge badge-secondary">{{ rule.get_escalate_to_role_display }}</span>
                                {% else %}
                                <span class="text-slate-400">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.severity %}
                                <span class="badge severity-{{ rule.severity }}">
                                    {{ rule.get_severity_display }}
                                </span>
                                {% else %}
                                <span class="text-slate-400">{% translate "All" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.priority %}
                                <span class="badge priority-{{ rule.priority }}">
                                    {{ rule.get_priority_display }}
                                </span>
                                {% else %}
                                <span class="text-slate-400">{% translate "All" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.is_active %}
                                <span class="badge badge-success">
                                    <i data-lucide="check-circle" class="w-3 h-3"></i>
                                    {% translate "Active" %}
                                </span>
                                {% else %}
                                <span class="badge badge-secondary">
                                    <i data-lucide="x-circle" class="w-3 h-3"></i>
                                    {% translate "Inactive" %}
                                </span>
                                {% endif %}
                            </td>
                            <td class="text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{% url 'complaints:escalation_rule_edit' rule.id %}" 
                                       class="p-2 text-blue hover:bg-blue-50 rounded-lg transition" 
                                       title="{% translate 'Edit' %}">
                                        <i data-lucide="edit" class="w-4 h-4"></i>
                                    </a>
                                    <form method="post" 
                                          action="{% url 'complaints:escalation_rule_delete' rule.id %}" 
                                          class="d-inline"
                                          onsubmit="return confirm('{% translate "Are you sure you want to delete this escalation rule?" %}')">
                                        {% csrf_token %}
                                        <button type="submit" 
                                                class="p-2 text-red-500 hover:bg-red-50 rounded-lg transition" 
                                                title="{% translate 'Delete' %}">
                                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <div class="p-4 border-t border-slate-200">
                <div class="flex items-center justify-between">
                    <p class="text-sm text-slate">
                        {% blocktrans with start=page_obj.start_index end=page_obj.end_index total=page_obj.paginator.count %}
                            Showing {{ start }} to {{ end }} of {{ total }} rules
                        {% endblocktrans %}
                    </p>
                    <div class="flex gap-2">
                        {% if page_obj.has_previous %}
                        <a href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
                           class="px-4 py-2 border border-slate-200 rounded-lg hover:bg-slate-50 transition text-sm font-medium">
                            <i data-lucide="chevron-left" class="w-4 h-4 inline"></i>
                            {% translate "Previous" %}
                        </a>
                        {% endif %}
                        {% if page_obj.has_next %}
                        <a href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
                           class="px-4 py-2 border border-slate-200 rounded-lg hover:bg-slate-50 transition text-sm font-medium">
                            {% translate "Next" %}
                            <i data-lucide="chevron-right" class="w-4 h-4 inline"></i>
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
            {% endif %}
            
            {% else %}
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="arrow-up-circle" class="w-8 h-8 text-slate-400"></i>
                </div>
                <p class="text-slate font-medium">{% translate "No escalation rules found" %}</p>
                <p class="text-slate text-sm mt-1">{% translate "Create your first rule to get started" %}</p>
                <a href="{% url 'complaints:escalation_rule_create' %}" class="hh-btn hh-btn-primary mt-4">
                    <i data-lucide="plus" class="w-4 h-4"></i>
                    {% translate "Create Escalation Rule" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>

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