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

{% block title %}KPI Definitions - PX360{% endblock %}

{% block extra_css %}
<style>
    .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;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h2 class="mb-1">
                    <i data-lucide="gauge" class="me-2" style="width: 28px; height: 28px; vertical-align: text-bottom;"></i>
                    KPI Definitions
                </h2>
                <p class="mb-0 opacity-75">Manage key performance indicators</p>
            </div>
        </div>
    </div>

    <!-- Table Section -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon" style="background: linear-gradient(135deg, #005696, #007bbd);">
                <i data-lucide="list" style="width: 20px; height: 20px; color: white;"></i>
            </div>
            <h5 class="mb-0 fw-bold">{% trans "KPI List" %}</h5>
        </div>
        <div class="p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Name" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Unit" %}</th>
                            <th>{% trans "Target" %}</th>
                            <th>{% trans "Thresholds" %}</th>
                            <th>{% trans "Status" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for kpi in kpis %}
                        <tr>
                            <td>
                                <strong>{{ kpi.name }}</strong><br>
                                {% if kpi.description %}
                                <small class="text-muted">{{ kpi.description|truncatewords:15 }}</small>
                                {% endif %}
                            </td>
                            <td><span class="badge bg-secondary">{{ kpi.get_category_display }}</span></td>
                            <td><small>{{ kpi.unit }}</small></td>
                            <td>
                                {% if kpi.target_value %}
                                <strong>{{ kpi.target_value }}</strong>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if kpi.warning_threshold or kpi.critical_threshold %}
                                <small>
                                    {% if kpi.warning_threshold %}
                                    <span class="badge bg-warning">Warning: {{ kpi.warning_threshold }}</span>
                                    {% endif %}
                                    {% if kpi.critical_threshold %}
                                    <span class="badge bg-danger">Critical: {{ kpi.critical_threshold }}</span>
                                    {% endif %}
                                </small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if kpi.is_active %}
                                <span class="badge bg-success">Active</span>
                                {% else %}
                                <span class="badge bg-secondary">Inactive</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <i data-lucide="gauge" style="width: 48px; height: 48px; color: #ccc;"></i>
                                <p class="text-muted mt-3">No KPIs defined</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {% if page_obj.has_other_pages %}
    <nav aria-label="KPIs pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                    <i data-lucide="chevron-left" style="width: 16px; height: 16px;"></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 }}">{{ 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 }}">
                    <i data-lucide="chevron-right" style="width: 16px; height: 16px;"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
