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

{% block title %}{% translate "Complaint Thresholds" %} - PX360{% endblock %}

{% block content %}
<div class="page-header">
    <div class="page-header-content">
        <div>
            <h1 class="page-title">
                <i class="fas fa-chart-line"></i>
                {% translate "Complaint Thresholds" %}
            </h1>
            <p class="page-description">
                {% translate "Configure thresholds for automatic alerts and reports" %}
            </p>
        </div>
        <a href="{% url 'complaints:complaint_threshold_create' %}" class="btn btn-primary">
            <i class="fas fa-plus"></i>
            {% translate "Create Threshold" %}
        </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 "Threshold Type" %}</label>
                    <select name="threshold_type" class="form-select">
                        <option value="">{% translate "All Types" %}</option>
                        <option value="daily" {% if filters.threshold_type == "daily" %}selected{% endif %}>{% translate "Daily" %}</option>
                        <option value="weekly" {% if filters.threshold_type == "weekly" %}selected{% endif %}>{% translate "Weekly" %}</option>
                        <option value="monthly" {% if filters.threshold_type == "monthly" %}selected{% endif %}>{% translate "Monthly" %}</option>
                        <option value="category" {% if filters.threshold_type == "category" %}selected{% endif %}>{% translate "By Category" %}</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:complaint_threshold_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i>
                        {% translate "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Thresholds Table -->
    <div class="card">
        <div class="card-body">
            {% if thresholds %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% translate "Hospital" %}</th>
                            <th>{% translate "Threshold Name" %}</th>
                            <th>{% translate "Type" %}</th>
                            <th>{% translate "Metric" %}</th>
                            <th>{% translate "Threshold Value" %}</th>
                            <th>{% translate "Category" %}</th>
                            <th>{% translate "Action" %}</th>
                            <th>{% translate "Status" %}</th>
                            <th>{% translate "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for threshold in thresholds %}
                        <tr>
                            <td>
                                <strong>{{ threshold.hospital.name }}</strong>
                            </td>
                            <td>{{ threshold.name }}</td>
                            <td>
                                <span class="badge bg-info">{{ threshold.get_threshold_type_display }}</span>
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ threshold.get_metric_type_display }}</span>
                            </td>
                            <td>
                                <strong>{{ threshold.threshold_value }}</strong>
                            </td>
                            <td>
                                {% if threshold.complaint_category %}
                                {{ threshold.complaint_category.name }}
                                {% else %}
                                <span class="text-muted">{% translate "All Categories" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="badge bg-warning text-dark">{{ threshold.get_action_display }}</span>
                            </td>
                            <td>
                                {% if threshold.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:complaint_threshold_edit' threshold.id %}" 
                                       class="btn btn-sm btn-outline-primary"
                                       title="{% translate 'Edit' %}">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form method="post" 
                                          action="{% url 'complaints:complaint_threshold_delete' threshold.id %}" 
                                          class="d-inline"
                                          onsubmit="return confirm('{% translate "Are you sure you want to delete this threshold?" %}')">
                                        {% 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-chart-line fa-3x text-muted mb-3"></i>
                <p class="text-muted">
                    {% translate "No thresholds found. Create your first threshold to get started." %}
                </p>
                <a href="{% url 'complaints:complaint_threshold_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus"></i>
                    {% translate "Create Threshold" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
