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

{% block title %}{% translate "SLA Configurations" %} - PX360{% endblock %}

{% block content %}
<div class="page-header">
    <div class="page-header-content">
        <div>
            <h1 class="page-title">
                <i class="fas fa-clock"></i>
                {% translate "SLA Configurations" %}
            </h1>
            <p class="page-description">
                {% translate "Manage Service Level Agreement settings for complaint deadlines" %}
            </p>
        </div>
        <a href="{% url 'complaints:sla_config_create' %}" class="btn btn-primary">
            <i class="fas fa-plus"></i>
            {% translate "Create SLA Config" %}
        </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-3">
                    <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-3">
                    <label class="form-label">{% translate "Severity" %}</label>
                    <select name="severity" class="form-select">
                        <option value="">{% translate "All Severities" %}</option>
                        {% for value, label in severity_choices %}
                        <option value="{{ value }}" {% if filters.severity == value %}selected{% endif %}>
                            {{ label }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                
                <div class="col-md-3">
                    <label class="form-label">{% translate "Priority" %}</label>
                    <select name="priority" class="form-select">
                        <option value="">{% translate "All Priorities" %}</option>
                        {% for value, label in priority_choices %}
                        <option value="{{ value }}" {% if filters.priority == value %}selected{% endif %}>
                            {{ label }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                
                <div class="col-md-3">
                    <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:sla_config_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i>
                        {% translate "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- SLA Configurations Table -->
    <div class="card">
        <div class="card-body">
            {% if sla_configs %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% translate "Hospital" %}</th>
                            <th>{% translate "Severity" %}</th>
                            <th>{% translate "Priority" %}</th>
                            <th>{% translate "SLA Hours" %}</th>
                            <th>{% translate "Warning Hours" %}</th>
                            <th>{% translate "Status" %}</th>
                            <th>{% translate "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for config in sla_configs %}
                        <tr>
                            <td>
                                <strong>{{ config.hospital.name }}</strong>
                            </td>
                            <td>
                                <span class="badge severity-{{ config.severity }}">
                                    {{ config.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <span class="badge priority-{{ config.priority }}">
                                    {{ config.get_priority_display }}
                                </span>
                            </td>
                            <td>{{ config.sla_hours }}h</td>
                            <td>{{ config.warning_hours }}h</td>
                            <td>
                                {% if config.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:sla_config_edit' config.id %}" 
                                       class="btn btn-sm btn-outline-primary"
                                       title="{% translate 'Edit' %}">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form method="post" 
                                          action="{% url 'complaints:sla_config_delete' config.id %}" 
                                          class="d-inline"
                                          onsubmit="return confirm('{% translate "Are you sure you want to delete this SLA configuration?" %}')">
                                        {% 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-clock fa-3x text-muted mb-3"></i>
                <p class="text-muted">
                    {% translate "No SLA configurations found. Create your first configuration to get started." %}
                </p>
                <a href="{% url 'complaints:sla_config_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i>
                    {% translate "Create SLA Config" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
