@extends('layouts.admin')

@section('content')
<header class="flex justify-between items-center mb-10">
    <div>
        <h1 class="text-3xl font-black">Pengawasan Partner</h1>
        <p class="text-slate-500 font-medium">Pantau seluruh penyelenggara di platform</p>
    </div>
</header>

{{-- Statistik Partner --}}
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-10">
    <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm">
        <div class="w-12 h-12 bg-indigo-50 text-indigo-600 rounded-2xl flex items-center justify-center mb-4">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path>
            </svg>
        </div>
        <p class="text-slate-400 text-sm font-bold uppercase mb-1">Total Partner</p>
        <h3 class="text-2xl font-black">{{ $totalPartners }}</h3>
    </div>

    <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm">
        <div class="w-12 h-12 bg-green-50 text-green-600 rounded-2xl flex items-center justify-center mb-4">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
            </svg>
        </div>
        <p class="text-slate-400 text-sm font-bold uppercase mb-1">Aktif</p>
        <h3 class="text-2xl font-black text-green-600">{{ $totalActive }}</h3>
    </div>

    <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm">
        <div class="w-12 h-12 bg-yellow-50 text-yellow-600 rounded-2xl flex items-center justify-center mb-4">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
            </svg>
        </div>
        <p class="text-slate-400 text-sm font-bold uppercase mb-1">Pending</p>
        <h3 class="text-2xl font-black text-yellow-600">{{ $totalPending }}</h3>
    </div>

    <div class="bg-white p-6 rounded-3xl border border-slate-100 shadow-sm">
        <div class="w-12 h-12 bg-red-50 text-red-600 rounded-2xl flex items-center justify-center mb-4">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636"></path>
            </svg>
        </div>
        <p class="text-slate-400 text-sm font-bold uppercase mb-1">Dinonaktifkan</p>
        <h3 class="text-2xl font-black text-red-600">{{ $totalSuspended }}</h3>
    </div>
</div>

{{-- Statistik Global --}}
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
    <div class="bg-gradient-to-br from-indigo-600 to-indigo-800 rounded-3xl p-6 text-white shadow-lg">
        <p class="text-indigo-200 text-sm font-bold uppercase mb-1">Total Pendapatan Platform</p>
        <h3 class="text-3xl font-black">Rp {{ number_format($totalRevenue, 0, ',', '.') }}</h3>
    </div>
    <div class="bg-gradient-to-br from-emerald-600 to-emerald-800 rounded-3xl p-6 text-white shadow-lg">
        <p class="text-emerald-200 text-sm font-bold uppercase mb-1">Total Tiket Terjual</p>
        <h3 class="text-3xl font-black">{{ number_format($totalTicketsSold, 0, ',', '.') }}</h3>
    </div>
    <div class="bg-gradient-to-br from-purple-600 to-purple-800 rounded-3xl p-6 text-white shadow-lg">
        <p class="text-purple-200 text-sm font-bold uppercase mb-1">Total Event</p>
        <h3 class="text-3xl font-black">{{ $totalEvents }}</h3>
    </div>
</div>

{{-- Grafik Pendapatan Global --}}
<div class="bg-white rounded-3xl border border-slate-100 shadow-sm p-8 mb-8">
    <div class="flex justify-between items-center mb-6">
        <h3 class="text-xl font-black">Pendapatan Platform Bulanan (12 Bulan)</h3>
        <span class="text-sm text-slate-400">Total: Rp {{ number_format(array_sum($globalRevenueChart), 0, ',', '.') }}</span>
    </div>

    <div class="flex items-end gap-2 h-64">
        @php $maxRevenue = max($globalRevenueChart) ?: 1; @endphp
        @foreach($globalRevenueChart as $i => $value)
            <div class="flex-1 flex flex-col items-center">
                <div class="w-full bg-indigo-500 rounded-t-lg relative group cursor-pointer transition-all hover:bg-indigo-600"
                     style="height: {{ ($value / $maxRevenue) * 100 }}%; min-height: {{ $value > 0 ? '8px' : '0' }};">
                    <div class="absolute -top-10 left-1/2 -translate-x-1/2 bg-slate-800 text-white text-xs px-2 py-1 rounded-lg opacity-0 group-hover:opacity-100 transition whitespace-nowrap">
                        Rp {{ number_format($value, 0, ',', '.') }}
                    </div>
                </div>
                <span class="text-[10px] text-slate-400 mt-2 rotate-45 origin-left whitespace-nowrap">{{ $globalRevenueLabels[$i] }}</span>
            </div>
        @endforeach
    </div>
</div>

{{-- Top Partners + Pending Partners --}}
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
    {{-- Top 10 Partners --}}
    <div class="bg-white rounded-3xl border border-slate-100 shadow-sm p-8">
        <h3 class="text-xl font-black mb-6">Top 10 Partner Berdasarkan Pendapatan</h3>

        @if($topPartners->count() > 0)
            <div class="space-y-4">
                @foreach($topPartners as $i => $partner)
                    <div class="flex items-center justify-between p-4 rounded-2xl {{ $i === 0 ? 'bg-yellow-50 border border-yellow-100' : 'bg-slate-50' }}">
                        <div class="flex items-center gap-4">
                            <div class="w-8 h-8 rounded-full flex items-center justify-center font-black text-sm {{ $i === 0 ? 'bg-yellow-400 text-white' : 'bg-slate-200 text-slate-500' }}">
                                {{ $i + 1 }}
                            </div>
                            <div>
                                <p class="font-bold text-slate-800">{{ $partner->name }}</p>
                                <p class="text-xs text-slate-400">{{ $partner->events_count }} event</p>
                            </div>
                        </div>
                        <p class="font-black {{ $i === 0 ? 'text-yellow-600' : 'text-indigo-600' }}">
                            Rp {{ number_format($partner->total_revenue ?? 0, 0, ',', '.') }}
                        </p>
                    </div>
                @endforeach
            </div>
        @else
            <p class="text-slate-400 italic text-center py-8">Belum ada data partner.</p>
        @endif
    </div>

    {{-- Partner Pending (butuh approval) --}}
    <div class="bg-white rounded-3xl border border-slate-100 shadow-sm p-8">
        <h3 class="text-xl font-black mb-6">Partner Menunggu Persetujuan</h3>

        @if($pendingPartners->count() > 0)
            <div class="space-y-4">
                @foreach($pendingPartners as $partner)
                    <div class="flex items-center justify-between p-4 rounded-2xl bg-amber-50 border border-amber-100">
                        <div class="flex items-center gap-4">
                            <div class="w-10 h-10 bg-amber-100 rounded-full flex items-center justify-center text-amber-600 font-bold text-sm">
                                {{ substr($partner->name, 0, 2) }}
                            </div>
                            <div>
                                <p class="font-bold text-slate-800">{{ $partner->name }}</p>
                                <p class="text-xs text-slate-400">{{ $partner->email ?? '-' }} • {{ $partner->created_at->diffForHumans() }}</p>
                            </div>
                        </div>
                        <div class="flex gap-2">
                            <form action="{{ route('admin.partners.approve', $partner->id) }}" method="POST">
                                @csrf
                                <button type="submit" class="px-4 py-2 bg-green-600 text-white rounded-xl text-xs font-bold hover:bg-green-700 transition">
                                    Setujui
                                </button>
                            </form>
                            <form action="{{ route('admin.partners.suspend', $partner->id) }}" method="POST">
                                @csrf
                                <button type="submit" class="px-4 py-2 bg-red-500 text-white rounded-xl text-xs font-bold hover:bg-red-600 transition">
                                    Tolak
                                </button>
                            </form>
                        </div>
                    </div>
                @endforeach
            </div>
            <div class="mt-6 text-center">
                <a href="{{ route('admin.partners.index') }}" class="text-indigo-600 font-bold hover:underline text-sm">
                    Lihat Semua Partner →
                </a>
            </div>
        @else
            <p class="text-slate-400 italic text-center py-8">Tidak ada partner yang menunggu persetujuan.</p>
        @endif
    </div>
</div>

<div class="text-center mt-10">
    <a href="{{ route('admin.dashboard') }}" class="inline-flex items-center gap-2 text-indigo-600 font-bold hover:underline">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path>
        </svg>
        Kembali ke Dashboard
    </a>
</div>
@endsection

