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

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

{% block content %}
<div class="page-header">
    <div class="page-header-content">
        <div>
            <h1 class="page-title">
                <i class="fas fa-arrow-up"></i>
                {% translate "Escalation Rules" %}
            </h1>
            <p class="page-description">
                {% translate "Configure automatic complaint escalation based on time thresholds" %}
            </p>
        </div>
        <a href="{% url 'complaints:escalation_rule_create' %}" class="btn btn-primary">
            <i class="fas fa-plus"></i>
            {% translate "Create Escalation Rule" %}
        </a>
    </div>
</div>

<div class="page-content">
    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title">
                <i class="fas fa-filter"></i>
                {% translate "Filters" %}
            </h5>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                {% if request.user.is_px_admin %}
                <div class="col-md-4">
                    <label class="form-label">{% translate "Hospital" %}</label>
                    <select name="hospital" class="form-select">
                        <option value="">{% translate "All Hospitals" %}</option>
                        {% for hospital in hospitals %}
                        <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                            {{ hospital.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                {% endif %}
                
                <div class="col-md-4">
                    <label class="form-label">{% translate "Escalation Level" %}</label>
                    <select name="escalation_level" class="form-select">
                        <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 class="col-md-4">
                    <label class="form-label">{% translate "Status" %}</label>
                    <select name="is_active" class="form-select">
                        <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="col-12 text-end">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search"></i>
                        {% translate "Apply Filters" %}
                    </button>
                    <a href="{% url 'complaints:escalation_rule_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i>
                        {% translate "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Escalation Rules Table -->
    <div class="card">
        <div class="card-body">
            {% if escalation_rules %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <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>{% 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 bg-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 bg-secondary">{{ rule.get_escalate_to_role_display }}</span>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.severity %}
                                <span class="badge severity-{{ rule.severity }}">
                                    {{ rule.get_severity_display }}
                                </span>
                                {% else %}
                                <span class="text-muted">{% translate "All" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.priority %}
                                <span class="badge priority-{{ rule.priority }}">
                                    {{ rule.get_priority_display }}
                                </span>
                                {% else %}
                                <span class="text-muted">{% translate "All" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if rule.is_active %}
                                <span class="badge bg-success">{% translate "Active" %}</span>
                                {% else %}
                                <span class="badge bg-secondary">{% translate "Inactive" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group">
                                    <a href="{% url 'complaints:escalation_rule_edit' rule.id %}" 
                                       class="btn btn-sm btn-outline-primary"
                                       title="{% translate 'Edit' %}">
                                        <i class="fas fa-edit"></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="btn btn-sm btn-outline-danger" 
                                                title="{% translate 'Delete' %}">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <nav class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                            <i class="fas fa-angle-double-left"></i>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                            <i class="fas fa-angle-left"></i>
                        </a>
                    </li>
                    {% endif %}
                    
                    {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                            {{ num }}
                        </a>
                    </li>
                    {% endif %}
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                            <i class="fas fa-angle-right"></i>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                            <i class="fas fa-angle-double-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
            
            {% else %}
            <div class="text-center py-5">
                <i class="fas fa-arrow-up fa-3x text-muted mb-3"></i>
                <p class="text-muted">
                    {% translate "No escalation rules found. Create your first rule to get started." %}
                </p>
                <a href="{% url 'complaints:escalation_rule_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i>
                    {% translate "Create Escalation Rule" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
