@extends('layouts.app')

@section('title', 'Laporan Keuangan - Vogue3D')

@section('content')
<div class="space-y-8">

    {{-- ── Header ─────────────────────────────────────────────── --}}
    <div class="flex flex-col md:flex-row md:items-center justify-between gap-4">
        <div class="flex items-center gap-4">
            <div class="w-14 h-14 bg-gradient-to-br from-pink-400 to-pink-600 text-white rounded-[20px] flex items-center justify-center shadow-xl shadow-pink-200 rotate-3">
                <i data-lucide="bar-chart-2" class="w-7 h-7"></i>
            </div>
            <div>
                <h1 class="text-3xl font-black text-slate-800 italic leading-tight tracking-tighter">Laporan Keuangan</h1>
                <p class="text-[11px] text-slate-400 font-bold uppercase tracking-[0.2em] mt-1">Vogue3D &bull; Rekap Pendapatan Usaha</p>
            </div>
        </div>

        {{-- Tombol Download PDF --}}
        <a href="{{ route('admin.financial.pdf', array_merge(request()->query(), ['period' => $period, 'start' => $startDate->format('Y-m-d'), 'end' => $endDate->format('Y-m-d')])) }}"
           class="inline-flex items-center gap-2 bg-pink-500 hover:bg-pink-600 text-white font-black text-xs uppercase tracking-widest px-5 py-3 rounded-2xl shadow-lg shadow-pink-200 transition-all duration-300 hover:scale-105 active:scale-95">
            <i data-lucide="download" class="w-4 h-4"></i>
            Download PDF
        </a>
    </div>

    {{-- ── Filter Periode ─────────────────────────────────────── --}}
    <div class="bg-white/70 backdrop-blur-xl rounded-[28px] border border-white shadow-lg shadow-pink-50 p-5">
        <form method="GET" action="{{ route('admin.financial.index') }}" class="flex flex-wrap items-end gap-4">

            <div class="flex gap-2 flex-wrap">
                @foreach([
                    'this_month'  => 'Bulan Ini',
                    'last_month'  => 'Bulan Lalu',
                    'this_year'   => 'Tahun Ini',
                    'custom'      => 'Custom',
                ] as $val => $label)
                    <button type="submit" name="period" value="{{ $val }}"
                        class="px-4 py-2 rounded-xl text-xs font-black uppercase tracking-widest transition-all
                        {{ $period === $val ? 'bg-pink-500 text-white shadow-md shadow-pink-200' : 'bg-slate-100 text-slate-500 hover:bg-pink-50 hover:text-pink-500' }}">
                        {{ $label }}
                    </button>
                @endforeach
            </div>

            {{-- Custom date range --}}
            @if($period === 'custom')
            <div class="flex items-center gap-3 bg-slate-50 px-4 py-2.5 rounded-2xl border border-slate-200">
                <label class="text-[10px] font-black text-slate-400 uppercase">Dari</label>
                <input type="date" name="start" value="{{ $startDate->format('Y-m-d') }}"
                       class="text-xs font-bold text-slate-700 bg-transparent border-none outline-none">
                <span class="text-slate-300">—</span>
                <label class="text-[10px] font-black text-slate-400 uppercase">Sampai</label>
                <input type="date" name="end" value="{{ $endDate->format('Y-m-d') }}"
                       class="text-xs font-bold text-slate-700 bg-transparent border-none outline-none">
                <input type="hidden" name="period" value="custom">
                <button type="submit" class="bg-pink-500 text-white text-[10px] font-black uppercase tracking-widest px-3 py-1.5 rounded-xl">
                    Terapkan
                </button>
            </div>
            @endif

        </form>

        <p class="mt-3 text-[11px] font-bold text-slate-400">
            Menampilkan data:
            <span class="text-pink-500">{{ $startDate->translatedFormat('d F Y') }} – {{ $endDate->translatedFormat('d F Y') }}</span>
        </p>
    </div>

    {{-- ── Kartu Ringkasan ────────────────────────────────────── --}}
    <div class="grid grid-cols-2 lg:grid-cols-4 gap-4">
        @php
        $summaryCards = [
            [
                'label' => 'Total Pendapatan',
                'value' => 'Rp ' . number_format($totalRevenue, 0, ',', '.'),
                'icon'  => 'trending-up',
                'color' => 'pink',
            ],
            [
                'label' => 'Penjualan Produk',
                'value' => 'Rp ' . number_format($totalProductSales, 0, ',', '.'),
                'icon'  => 'shopping-bag',
                'color' => 'slate',
            ],
            [
                'label' => 'Total Ongkir',
                'value' => 'Rp ' . number_format($totalShippingFee, 0, ',', '.'),
                'icon'  => 'truck',
                'color' => 'amber',
            ],
            [
                'label' => 'Order Selesai',
                'value' => $completedOrders->count() . ' / ' . $orders->count(),
                'icon'  => 'check-circle',
                'color' => 'emerald',
            ],
        ];
        $colorMap = [
            'pink'    => ['bg' => 'bg-pink-50',    'text' => 'text-pink-600',    'icon' => 'bg-pink-500'],
            'slate'   => ['bg' => 'bg-slate-50',   'text' => 'text-slate-700',   'icon' => 'bg-slate-800'],
            'amber'   => ['bg' => 'bg-amber-50',   'text' => 'text-amber-600',   'icon' => 'bg-amber-500'],
            'emerald' => ['bg' => 'bg-emerald-50', 'text' => 'text-emerald-600', 'icon' => 'bg-emerald-500'],
        ];
        @endphp

        @foreach($summaryCards as $card)
        @php $c = $colorMap[$card['color']]; @endphp
        <div class="{{ $c['bg'] }} rounded-[24px] p-5 border border-white shadow-sm">
            <div class="flex items-start justify-between mb-3">
                <p class="text-[10px] font-black uppercase tracking-[0.2em] text-slate-400">{{ $card['label'] }}</p>
                <div class="{{ $c['icon'] }} w-8 h-8 rounded-xl flex items-center justify-center shadow-sm">
                    <i data-lucide="{{ $card['icon'] }}" class="w-4 h-4 text-white"></i>
                </div>
            </div>
            <p class="text-xl font-black {{ $c['text'] }} italic leading-tight">{{ $card['value'] }}</p>
        </div>
        @endforeach
    </div>

    {{-- ── Metode Pembayaran + Top Produk ─────────────────────── --}}
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">

        {{-- Metode Pembayaran --}}
        <div class="bg-white/70 backdrop-blur-xl rounded-[28px] border border-white shadow-lg shadow-pink-50 p-6">
            <h2 class="text-sm font-black text-slate-800 uppercase tracking-widest mb-4 flex items-center gap-2">
                <i data-lucide="credit-card" class="w-4 h-4 text-pink-400"></i>
                Metode Pembayaran
            </h2>
            <div class="space-y-3">
                {{-- Midtrans --}}
                <div class="flex items-center justify-between bg-blue-50 rounded-2xl px-4 py-3">
                    <div class="flex items-center gap-3">
                        <div class="w-8 h-8 bg-blue-500 rounded-xl flex items-center justify-center">
                            <i data-lucide="smartphone" class="w-4 h-4 text-white"></i>
                        </div>
                        <div>
                            <p class="text-xs font-black text-slate-700">Midtrans</p>
                            <p class="text-[9px] text-slate-400 font-bold">{{ $midtransCount }} transaksi</p>
                        </div>
                    </div>
                    <p class="text-sm font-black text-blue-600 italic">Rp {{ number_format($midtransRevenue, 0, ',', '.') }}</p>
                </div>
                {{-- Total --}}
                <div class="flex items-center justify-between bg-pink-500 rounded-2xl px-4 py-3">
                    <p class="text-xs font-black text-white uppercase tracking-widest">Total</p>
                    <p class="text-sm font-black text-white italic">Rp {{ number_format($totalRevenue, 0, ',', '.') }}</p>
                </div>
            </div>
        </div>

        {{-- Top Produk --}}
        <div class="bg-white/70 backdrop-blur-xl rounded-[28px] border border-white shadow-lg shadow-pink-50 p-6">
            <h2 class="text-sm font-black text-slate-800 uppercase tracking-widest mb-4 flex items-center gap-2">
                <i data-lucide="trophy" class="w-4 h-4 text-amber-400"></i>
                Top 5 Produk Terlaris
            </h2>
            @if($topProducts->count())
            <div class="space-y-2.5">
                @foreach($topProducts as $i => $prod)
                <div class="flex items-center gap-3">
                    <span class="w-7 h-7 rounded-xl {{ $i === 0 ? 'bg-amber-400 text-white' : 'bg-slate-100 text-slate-500' }} flex items-center justify-center text-[10px] font-black shrink-0">
                        {{ $i + 1 }}
                    </span>
                    <div class="flex-1 min-w-0">
                        <p class="text-xs font-black text-slate-700 truncate">{{ $prod->name }}</p>
                        <p class="text-[9px] text-slate-400 font-bold">{{ $prod->total_qty }} terjual</p>
                    </div>
                    <p class="text-xs font-black text-pink-500 italic shrink-0">Rp {{ number_format($prod->total_sales, 0, ',', '.') }}</p>
                </div>
                @endforeach
            </div>
            @else
            <p class="text-sm text-slate-400 italic text-center py-6">Belum ada data penjualan.</p>
            @endif
        </div>
    </div>

    {{-- ── Status Order ────────────────────────────────────────── --}}
    <div class="bg-white/70 backdrop-blur-xl rounded-[28px] border border-white shadow-lg shadow-pink-50 p-6">
        <h2 class="text-sm font-black text-slate-800 uppercase tracking-widest mb-5 flex items-center gap-2">
            <i data-lucide="layers" class="w-4 h-4 text-pink-400"></i>
            Ringkasan Status Pesanan
        </h2>
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
            @php
            $statusGroups = [
                ['label' => 'Selesai',       'count' => $completedOrders->count(), 'bg' => 'bg-emerald-50',  'text' => 'text-emerald-600', 'dot' => 'bg-emerald-500'],
                ['label' => 'Diproses',      'count' => $pendingOrders->count(),   'bg' => 'bg-blue-50',     'text' => 'text-blue-600',    'dot' => 'bg-blue-500'],
                ['label' => 'Dibatalkan',    'count' => $cancelledOrders->count(), 'bg' => 'bg-red-50',      'text' => 'text-red-500',     'dot' => 'bg-red-400'],
                ['label' => 'Total Order',   'count' => $orders->count(),          'bg' => 'bg-pink-50',     'text' => 'text-pink-600',    'dot' => 'bg-pink-500'],
            ];
            @endphp
            @foreach($statusGroups as $sg)
            <div class="{{ $sg['bg'] }} rounded-2xl px-4 py-4 flex items-center gap-3">
                <span class="w-2.5 h-2.5 rounded-full {{ $sg['dot'] }} shrink-0 animate-pulse"></span>
                <div>
                    <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">{{ $sg['label'] }}</p>
                    <p class="text-xl font-black {{ $sg['text'] }} italic">{{ $sg['count'] }}</p>
                </div>
            </div>
            @endforeach
        </div>
    </div>

    {{-- ── Tabel Detail Pesanan ────────────────────────────────── --}}
    <div class="bg-white/70 backdrop-blur-xl rounded-[28px] border border-white shadow-lg shadow-pink-50 overflow-hidden">
        <div class="px-6 pt-6 pb-4 flex items-center justify-between border-b border-pink-50">
            <h2 class="text-sm font-black text-slate-800 uppercase tracking-widest flex items-center gap-2">
                <i data-lucide="list" class="w-4 h-4 text-pink-400"></i>
                Detail Pesanan
            </h2>
            <span class="text-[10px] font-bold text-slate-400">{{ $orders->count() }} pesanan</span>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead>
                    <tr class="bg-pink-50/60">
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">No. Order</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Pelanggan</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Metode</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Ongkir</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Total</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Status</th>
                        <th class="px-5 py-4 text-[10px] font-black text-pink-400 uppercase tracking-widest">Tanggal</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-pink-50/50">
                    @forelse($orders as $order)
                    <tr class="hover:bg-white/60 transition">
                        <td class="px-5 py-3.5 text-xs font-black text-slate-700 italic">{{ $order->order_number }}</td>
                        <td class="px-5 py-3.5 text-xs font-bold text-slate-700">{{ $order->user->name }}</td>
                        <td class="px-5 py-3.5">
                            @if($order->payment_method === 'midtrans')
                                <span class="inline-flex items-center gap-1 bg-blue-50 text-blue-600 text-[9px] font-black uppercase px-2.5 py-1 rounded-full border border-blue-100">
                                    <i data-lucide="smartphone" class="w-3 h-3"></i> Midtrans
                                </span>
                            @else
                                <span class="inline-flex items-center gap-1 bg-emerald-50 text-emerald-600 text-[9px] font-black uppercase px-2.5 py-1 rounded-full border border-emerald-100">
                                    <i data-lucide="banknote" class="w-3 h-3"></i> COD
                                </span>
                            @endif
                        </td>
                        <td class="px-5 py-3.5 text-xs font-bold text-amber-600">Rp {{ number_format($order->shipping_fee, 0, ',', '.') }}</td>
                        <td class="px-5 py-3.5 text-xs font-black text-slate-900 italic">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                        <td class="px-5 py-3.5">
                            @php
                            $badge = match($order->status) {
                                'completed'  => 'bg-emerald-50 text-emerald-600 border-emerald-100',
                                'cancelled'  => 'bg-red-50 text-red-500 border-red-100',
                                'shipping'   => 'bg-pink-50 text-pink-600 border-pink-100',
                                'picking_up' => 'bg-amber-50 text-amber-600 border-amber-100',
                                'packaging'  => 'bg-blue-50 text-blue-600 border-blue-100',
                                default      => 'bg-slate-50 text-slate-400 border-slate-100',
                            };
                            $statusLabel = match($order->status) {
                                'completed'  => '✅ Selesai',
                                'cancelled'  => '✗ Batal',
                                'shipping'   => '🚚 Dikirim',
                                'picking_up' => '🏪 Ambil',
                                'packaging'  => '📦 Dikemas',
                                'pending'    => '⏳ Pending',
                                default      => $order->status,
                            };
                            @endphp
                            <span class="inline-flex text-[9px] font-black {{ $badge }} px-2.5 py-1 rounded-full uppercase tracking-wide border">
                                {{ $statusLabel }}
                            </span>
                        </td>
                        <td class="px-5 py-3.5 text-[10px] text-slate-400 font-bold">
                            {{ \Carbon\Carbon::parse($order->created_at)->format('d M Y') }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center py-12 text-slate-400 italic font-bold">Tidak ada data pesanan pada periode ini.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Totals footer --}}
        @if($orders->count())
        <div class="px-6 py-4 bg-pink-50/50 border-t border-pink-100 flex flex-wrap gap-6 justify-end">
            <div class="text-right">
                <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Total Ongkir</p>
                <p class="text-sm font-black text-amber-600 italic">Rp {{ number_format($totalShippingFee, 0, ',', '.') }}</p>
            </div>
            <div class="text-right">
                <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Total Pendapatan</p>
                <p class="text-lg font-black text-pink-600 italic">Rp {{ number_format($totalRevenue, 0, ',', '.') }}</p>
            </div>
        </div>
        @endif
    </div>

</div>

<script>lucide.createIcons();</script>
@endsection