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

{% block title %}{% trans "My Performance" %} - 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;
    }
    .kpi-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        padding: 1.5rem;
        text-align: center;
        transition: all 0.3s ease;
    }
    .kpi-card:hover {
        border-color: #005696;
        box-shadow: 0 8px 20px -5px rgba(0, 86, 150, 0.15);
        transform: translateY(-2px);
    }
    .progress-ring {
        transform: rotate(-90deg);
    }
</style>
{% endblock %}

{% block content %}
<div class="mb-6">
    <a href="{% url 'dashboard:my_dashboard' %}" class="inline-flex items-center gap-2 text-slate hover:text-navy transition text-sm font-semibold">
        <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Back to My Dashboard" %}
    </a>
</div>

<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="bar-chart-3" class="w-7 h-7"></i>
                {% trans "My Performance" %}
            </h1>
            <p class="text-sm opacity-90 mt-1">
                {{ staff_member.name }} &mdash;
                {% if staff_member.department %}{{ staff_member.department.name }}{% endif %}
            </p>
        </div>
        <div class="flex items-center gap-3">
            <select id="dateRange" class="px-4 py-2 bg-white/20 text-white border border-white/30 rounded-xl text-sm font-semibold focus:ring-2 focus:ring-white/30"
                    onchange="updateDateRange(this.value)">
                <option value="7d" {% if date_range == '7d' %}selected{% endif %}>{% trans "Last 7 Days" %}</option>
                <option value="30d" {% if date_range == '30d' %}selected{% endif %}>{% trans "Last 30 Days" %}</option>
                <option value="90d" {% if date_range == '90d' %}selected{% endif %}>{% trans "Last 90 Days" %}</option>
                <option value="1y" {% if date_range == '1y' %}selected{% endif %}>{% trans "Last Year" %}</option>
            </select>
        </div>
    </div>
</div>

{% if performance %}
<!-- Performance Score -->
<div class="kpi-card mb-6">
    <div class="flex items-center justify-center gap-8">
        <div class="relative">
            <svg class="progress-ring" width="120" height="120">
                <circle cx="60" cy="60" r="50" stroke="#e2e8f0" stroke-width="10" fill="none"/>
                <circle cx="60" cy="60" r="50" stroke="{% if performance.performance_score.overall >= 80 %}#22c55e{% elif performance.performance_score.overall >= 60 %}#f59e0b{% else %}#ef4444{% endif %}" stroke-width="10" fill="none"
                    stroke-dasharray="{{ performance.performance_score.overalloverall }} 314" stroke-linecap="round"/>
            </svg>
            <div class="absolute inset-0 flex items-center justify-center">
                <span class="text-3xl font-black text-navy">{{ performance.performance_score.overall|floatformat:0 }}</span>
            </div>
        </div>
        <div>
            <h3 class="text-lg font-bold text-navy">{% trans "Overall Performance Score" %}</h3>
            <p class="text-slate text-sm">
                <span class="font-bold
                    {% if performance.performance_score.rating == 'Excellent' %}text-green-600
                    {% elif performance.performance_score.rating == 'Good' %}text-blue-600
                    {% elif performance.performance_score.rating == 'Average' %}text-amber-600
                    {% else %}text-red-600{% endif %}">
                    {{ performance.performance_score.rating }}
                </span>
            </p>
        </div>
    </div>
</div>

<!-- KPI Cards -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
    <div class="kpi-card">
        <div class="w-12 h-12 bg-orange-100 rounded-xl flex items-center justify-center mx-auto mb-3">
            <i data-lucide="file-text" class="w-6 h-6 text-orange-600"></i>
        </div>
        <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Total Complaints" %}</p>
        <p class="text-2xl font-black text-navy">{{ performance.complaint_metrics.total|default:0 }}</p>
        <p class="text-xs text-slate mt-1">
            {% trans "Resolved:" %} {{ performance.summary.complaint_resolution_rate|floatformat:0 }}%
        </p>
    </div>

    <div class="kpi-card">
        <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center mx-auto mb-3">
            <i data-lucide="clock" class="w-6 h-6 text-blue-600"></i>
        </div>
        <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Response Time" %}</p>
        <p class="text-2xl font-black text-navy">
            {{ performance.complaint_metrics.response_time.within_24h|default:0 }}/{% trans "24h" %}
        </p>
        <p class="text-xs text-slate mt-1">
            {{ performance.complaint_metrics.response_time.within_48h|default:0 }} {% trans "within 48h" %}
        </p>
    </div>

    <div class="kpi-card">
        <div class="w-12 h-12 bg-green-100 rounded-xl flex items-center justify-center mx-auto mb-3">
            <i data-lucide="check-circle" class="w-6 h-6 text-green-600"></i>
        </div>
        <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Resolution Rate" %}</p>
        <p class="text-2xl font-black text-navy">{{ performance.summary.complaint_resolution_rate|floatformat:0 }}%</p>
    </div>

    <div class="kpi-card">
        <div class="w-12 h-12 bg-purple-100 rounded-xl flex items-center justify-center mx-auto mb-3">
            <i data-lucide="message-circle" class="w-6 h-6 text-purple-600"></i>
        </div>
        <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">{% trans "Total Inquiries" %}</p>
        <p class="text-2xl font-black text-navy">{{ performance.inquiry_metrics.total|default:0 }}</p>
        <p class="text-xs text-slate mt-1">
            {% trans "Resolved:" %} {{ performance.summary.inquiry_resolution_rate|floatformat:0 }}%
        </p>
    </div>
</div>

<!-- Score Breakdown -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-indigo-100">
            <i data-lucide="pie-chart" class="w-5 h-5 text-indigo-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Score Breakdown" %}</h3>
    </div>
    <div class="p-5 space-y-3">
        {% for key, value in performance.performance_score.breakdown.items %}
        <div>
            <div class="flex justify-between text-sm mb-1">
                <span class="font-semibold text-navy">{{ key|title }}</span>
                <span class="text-slate">{{ value|floatformat:0 }}/100</span>
            </div>
            <div class="w-full bg-slate-200 rounded-full h-2.5">
                <div class="bg-navy h-2.5 rounded-full transition-all" style="width: {{ value|floatformat:0 }}%"></div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

{% else %}
<div class="section-card p-12 text-center mb-6">
    <div class="section-icon bg-slate-100 mx-auto mb-4" style="width:64px;height:64px;border-radius:1rem;">
        <i data-lucide="bar-chart-3" class="w-8 h-8 text-slate-400"></i>
    </div>
    <h3 class="text-lg font-bold text-navy mb-2">{% trans "No performance data available" %}</h3>
    <p class="text-slate text-sm">{% trans "Performance metrics will appear once you have assigned complaints or inquiries." %}</p>
</div>
{% endif %}

<!-- Department Benchmarks -->
{% if benchmarks %}
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-blue-100">
            <i data-lucide="git-compare" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Department Comparison" %}</h3>
    </div>
    <div class="p-5">
        <p class="text-slate text-sm mb-4">{% trans "How your performance compares to your department" %}</p>
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
            <div class="text-center p-4 bg-blue-50 rounded-xl">
                <p class="text-xs font-bold text-slate uppercase mb-1">{% trans "Department Avg Score" %}</p>
                <p class="text-2xl font-black text-navy">{{ benchmarks.average_score|floatformat:0 }}</p>
            </div>
            <div class="text-center p-4 bg-green-50 rounded-xl">
                <p class="text-xs font-bold text-slate uppercase mb-1">{% trans "Staff in Dept" %}</p>
                <p class="text-2xl font-black text-navy">{{ benchmarks.staff_count }}</p>
            </div>
            <div class="text-center p-4 bg-purple-50 rounded-xl">
                <p class="text-xs font-bold text-slate uppercase mb-1">{% trans "Your Rank" %}</p>
                <p class="text-2xl font-black text-navy">
                    {% for s in benchmarks.rankings %}
                        {% if s.id == staff_member.user_id|stringformat:"s" %}
                            #{{ forloop.counter }}
                        {% endif %}
                    {% endfor %}
                </p>
            </div>
        </div>
        {% if benchmarks.rankings %}
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-slate-100">
                <thead class="bg-slate-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">{% trans "Rank" %}</th>
                        <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">{% trans "Staff" %}</th>
                        <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">{% trans "Score" %}</th>
                        <th class="px-4 py-3 text-left text-[10px] font-bold text-slate uppercase">{% trans "Items" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    {% for s in benchmarks.rankings %}
                    <tr class="{% if s.id == staff_member.user_id|stringformat:"s" %}bg-blue-50 font-semibold{% else %}hover:bg-slate-50{% endif %}">
                        <td class="px-4 py-3 text-sm">#{{ forloop.counter }}</td>
                        <td class="px-4 py-3 text-sm">{{ s.name }}</td>
                        <td class="px-4 py-3 text-sm">
                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-bold
                                {% if s.score >= 80 %}bg-green-100 text-green-700
                                {% elif s.score >= 60 %}bg-amber-100 text-amber-700
                                {% else %}bg-red-100 text-red-700{% endif %}">
                                {{ s.score|floatformat:0 }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-slate">{{ s.total_items }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% endif %}
    </div>
</div>
{% endif %}

{% endblock %}

{% block extra_js %}
<script>
function updateDateRange(value) {
    const url = new URL(window.location.href);
    url.searchParams.set('date_range', value);
    window.location.href = url.toString();
}
</script>
{% endblock %}
