@extends('layouts.admin_sidebar')

@section('content')
<div class="space-y-6">
    {{-- ===== PAGE HEADER ===== --}}
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center bg-white p-6 md:p-8 rounded-[2rem] md:rounded-[2.5rem] shadow-sm border border-slate-100 gap-4">
        <div>
            <div class="inline-flex items-center gap-2 px-3 py-1 bg-gas3kg/10 text-gas3kg rounded-full text-[10px] font-black uppercase tracking-wider mb-3">
                <i data-lucide="file-text" class="w-3 h-3"></i> Laporan
            </div>
            <h1 class="text-2xl md:text-3xl font-black text-slate-800 tracking-tight leading-none">Laporan Penjualan</h1>
            <p class="text-slate-400 text-[10px] md:text-xs font-bold uppercase tracking-[0.2em] mt-2">
                @if($filtered && $tanggalAwal && $tanggalAkhir)
                    Periode {{ \Carbon\Carbon::parse($tanggalAwal)->translatedFormat('d M Y') }} &ndash; {{ \Carbon\Carbon::parse($tanggalAkhir)->translatedFormat('d M Y') }}
                @else
                    Pilih rentang tanggal untuk melihat laporan
                @endif
            </p>
        </div>
        
        <div class="flex flex-col md:flex-row items-center gap-4 w-full md:w-auto">
            <div class="w-full md:w-auto text-center md:text-left bg-slate-50 px-6 py-3 md:px-8 md:py-4 rounded-[1.5rem] md:rounded-3xl border border-slate-100">
                <p class="text-[9px] font-black text-slate-400 uppercase tracking-widest leading-none mb-1 md:mb-0">Total Uang Masuk</p>
                @if($filtered)
                    <p class="text-xl md:text-2xl font-black text-gas3kg mt-1 flex items-center gap-1 justify-center md:justify-start">
                        Rp {{ number_format($totalPendapatan,0,',','.') }}
                    </p>
                @else
                    <p class="text-base md:text-xl font-black text-slate-300 mt-1 flex justify-center md:block">— Belum difilter</p>
                @endif
            </div>
        </div>
    </div>

    {{-- ===== FILTER TANGGAL ===== --}}
    <form method="GET" action="{{ route('admin.laporan-penjualan') }}"
            class="bg-white rounded-[2rem] border border-slate-100 shadow-sm p-4 md:p-6">
        <div class="flex flex-col gap-3">
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
                <div>
                    <label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1.5">Dari Tanggal</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none">
                            <i data-lucide="calendar" class="w-4 h-4 text-slate-400"></i>
                        </div>
                        <input type="date" name="dari" value="{{ $tanggalAwal }}"
                                class="w-full pl-10 pr-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm font-semibold text-slate-700 focus:outline-none focus:border-gas3kg focus:ring-2 focus:ring-gas3kg/10 transition-all" required>
                    </div>
                </div>
                <div>
                    <label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1.5">Sampai Tanggal</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none">
                            <i data-lucide="calendar" class="w-4 h-4 text-slate-400"></i>
                        </div>
                        <input type="date" name="sampai" value="{{ $tanggalAkhir }}"
                                class="w-full pl-10 pr-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm font-semibold text-slate-700 focus:outline-none focus:border-gas3kg focus:ring-2 focus:ring-gas3kg/10 transition-all" required>
                    </div>
                </div>
            </div>
            <div class="flex gap-2">
                <button type="submit"
                        class="flex-1 inline-flex items-center justify-center gap-2 bg-gas3kg hover:bg-green-700 text-white font-black text-sm px-6 py-3 rounded-xl transition-all shadow-lg shadow-gas3kg/25">
                    <i data-lucide="filter" class="w-4 h-4"></i> Tampilkan
                </button>
                <a href="{{ route('admin.laporan-penjualan') }}"
                    class="inline-flex items-center justify-center gap-2 bg-slate-100 hover:bg-slate-200 text-slate-600 font-bold text-sm px-5 py-3 rounded-xl transition-all">
                    <i data-lucide="rotate-ccw" class="w-4 h-4"></i>
                    <span class="hidden sm:inline">Reset</span>
                </a>
            </div>
        </div>
    </form>

    {{-- ===== DATA SECTION (hanya muncul setelah filter) ===== --}}
    @if($filtered)
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
        {{-- Pendapatan --}}
        <div class="bg-gradient-to-br from-gas3kg to-green-700 rounded-[2rem] p-5 shadow-xl shadow-gas3kg/25 text-white transition-transform hover:scale-[1.02]">
            <div class="w-10 h-10 rounded-xl bg-white/15 flex items-center justify-center mb-3">
                <i data-lucide="banknote" class="w-5 h-5 text-white"></i>
            </div>
            <p class="text-[10px] font-black text-green-100 uppercase tracking-widest mb-1">Total Uang Masuk</p>
            <p class="text-2xl font-black leading-none">Rp {{ number_format($totalPendapatan,0,',','.') }}</p>
        </div>

        {{-- Pengeluaran --}}
        <div class="bg-white rounded-[2rem] p-5 border border-slate-100 shadow-sm transition-transform hover:scale-[1.02]">
            <div class="w-10 h-10 rounded-xl bg-red-50 flex items-center justify-center mb-3">
                <i data-lucide="trending-down" class="w-5 h-5 text-red-500"></i>
            </div>
            <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1">Total Pengeluaran</p>
            <p class="text-2xl font-black text-slate-800 leading-none">Rp {{ number_format($totalPengeluaran,0,',','.') }}</p>
        </div>

        {{-- Total Bersih --}}
        <div class="bg-emerald-600 rounded-[2rem] p-5 shadow-xl shadow-emerald-500/20 text-white transition-transform hover:scale-[1.02]">
            <div class="w-10 h-10 rounded-xl bg-white/15 flex items-center justify-center mb-3">
                <i data-lucide="wallet" class="w-5 h-5 text-white"></i>
            </div>
            <p class="text-[10px] font-black text-emerald-100 uppercase tracking-widest mb-1">Total Bersih</p>
            <p class="text-2xl font-black leading-none">Rp {{ number_format($totalBersih,0,',','.') }}</p>
        </div>

        {{-- Total Transaksi --}}
        <div class="bg-white rounded-[2rem] p-5 border border-slate-100 shadow-sm transition-transform hover:scale-[1.02]">
            <div class="w-10 h-10 rounded-xl bg-purple-50 flex items-center justify-center mb-3">
                <i data-lucide="shopping-cart" class="w-5 h-5 text-purple-500"></i>
            </div>
            <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1">Total Transaksi</p>
            <p class="text-2xl font-black text-slate-800 leading-none">{{ number_format($totalTransaksi,0,',','.') }} </p>
        </div>

        {{-- Terjual --}}
        <div class="bg-white rounded-[2rem] p-5 border border-slate-100 shadow-sm transition-transform hover:scale-[1.02]">
            <div class="w-10 h-10 rounded-xl bg-orange-50 flex items-center justify-center mb-3">
                <i data-lucide="package" class="w-5 h-5 text-orange-500"></i>
            </div>
            <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1">Total Terjual</p>
            <p class="text-2xl font-black text-slate-800 leading-none">{{ number_format($totalTabung,0,',','.') }}</p>
        </div>
    </div>

    {{-- ===== RINGKASAN PER PENGIRIM ===== --}}
    @if(isset($summaryPerPengirim) && $summaryPerPengirim->count() > 0)
    <div class="bg-white rounded-[2rem] border border-slate-100 shadow-sm overflow-hidden">
        <div class="px-5 md:px-6 py-5 flex items-center justify-between border-b border-slate-100">
            <div class="flex items-center gap-3">
                <div class="w-9 h-9 rounded-xl bg-emerald-50 flex items-center justify-center">
                    <i data-lucide="user-check" class="w-5 h-5 text-emerald-500"></i>
                </div>
                <div class="text-left">
                    <h2 class="font-black text-slate-800 text-base">Ringkasan per Pengirim</h2>
                    <p class="text-xs text-slate-400 font-semibold">Distribusi pendapatan berdasarkan Kurir pengirim</p>
                </div>
            </div>
        </div>
        <div class="p-4 md:p-6 grid grid-cols-1 sm:grid-cols-2 gap-4 bg-slate-50/50">
            @foreach($summaryPerPengirim as $sum)
            <div class="bg-white p-4 rounded-2xl border border-slate-100 shadow-sm flex flex-col justify-between">
                <div class="flex justify-between items-start mb-2">
                    <h3 class="font-bold text-slate-800 flex items-center gap-2">
                        <div class="w-6 h-6 rounded-full bg-slate-100 flex items-center justify-center text-xs text-slate-500 font-bold">
                            {{ substr($sum['nama_pengirim'], 0, 1) }}
                        </div>
                        {{ $sum['nama_pengirim'] }}
                    </h3>
                    <span class="inline-flex items-center justify-center px-2 py-1 bg-slate-50 text-slate-600 rounded-lg text-xs font-bold border border-slate-100">
                        {{ $sum['total_transaksi'] }} Trx
                    </span>
                </div>

                @if(!empty($sum['nama_pelanggan']))
                <div class="mb-3">
                    <p class="text-[9px] font-black text-slate-400 uppercase tracking-widest mb-1.5 flex items-center gap-1.5">
                        <i data-lucide="users" class="w-3 h-3 text-slate-300"></i> Pelanggan:
                    </p>
                    <p class="text-[11px] font-bold text-slate-700 leading-relaxed line-clamp-2">
                        {{ $sum['nama_pelanggan'] }}
                    </p>
                </div>
                @endif
                <div class="mt-3 border-t border-slate-100 pt-3" x-data="{ expanded: false }">
                    <div class="flex justify-between items-center mb-2">
                        <p class="text-[10px] text-slate-400 font-bold uppercase tracking-wider">Total Uang Masuk</p>
                        <p class="font-mono text-base font-black text-gas3kg">Rp {{ number_format($sum['total_pendapatan'],0,',','.') }}</p>
                    </div>
                    
                    <div class="flex justify-between items-center mb-2">
                        <div class="flex items-center gap-1">
                            <p class="text-[10px] text-slate-400 font-bold uppercase tracking-wider">Total Pengeluaran</p>
                            @if($sum['detail_pengeluaran']->count() > 0)
                            <button @click="expanded = !expanded" class="text-slate-400 hover:text-red-500 transition-colors">
                                <i data-lucide="info" class="w-3 h-3" :class="expanded ? 'text-red-500' : ''"></i>
                            </button>
                            @endif
                        </div>
                        <p class="font-mono text-base font-black text-red-500">Rp {{ number_format($sum['total_pengeluaran'],0,',','.') }}</p>
                    </div>

                    @if($sum['detail_pengeluaran']->count() > 0)
                    <div :class="expanded ? 'max-h-[500px] opacity-100 mb-3' : 'max-h-0 opacity-0 pointer-events-none'" class="transition-all duration-350 ease-in-out overflow-hidden" x-cloak>
                        <div class="bg-red-50/50 rounded-xl p-3 border border-red-100/50">
                            <p class="text-[9px] font-black text-red-400 uppercase tracking-widest mb-2">Rincian Uang Keluar:</p>
                            <ul class="space-y-1.5">
                                @foreach($sum['detail_pengeluaran'] as $out)
                                <li class="flex justify-between items-start gap-2">
                                    <div class="flex flex-col">
                                        <span class="text-[11px] font-bold text-slate-700 leading-tight">{{ $out->keterangan }}</span>
                                        <span class="text-[9px] text-slate-400 capitalize">{{ $out->kategori }} &middot; {{ \Carbon\Carbon::parse($out->tanggal)->format('d/m') }}</span>
                                    </div>
                                    <span class="text-[11px] font-black text-red-600 font-mono flex-shrink-0">Rp{{ number_format($out->jumlah,0,',','.') }}</span>
                                </li>
                                @endforeach
                            </ul>
                        </div>
                    </div>
                    @endif

                    <div class="flex justify-between items-center p-3 bg-emerald-50 rounded-xl border border-emerald-100 mb-2">
                        <p class="text-[10px] text-emerald-600 font-black uppercase tracking-widest">Total Bersih</p>
                        <p class="font-mono text-lg font-black text-emerald-700">Rp {{ number_format($sum['total_bersih'],0,',','.') }}</p>
                    </div>

                    <div class="text-right">
                        <p class="text-[10px] text-slate-400 font-bold bg-slate-50 inline-block px-2 py-1 rounded-lg border border-slate-100">
                            <i data-lucide="package" class="w-3 h-3 inline"></i> {{ $sum['total_tabung'] }} tabung terjual
                        </p>
                    </div>
                </div>
            </div>
            @endforeach
        </div>
    </div>
    @endif

    {{-- ===== DETAIL TRANSAKSI ===== --}}
    @if($transaksi->count() > 0)
    <div class="bg-white rounded-[2rem] border border-slate-100 shadow-sm overflow-hidden">
        <div class="px-5 md:px-6 py-5 flex items-center justify-between border-b border-slate-100">
            <div class="flex items-center gap-3">
                <div class="w-9 h-9 rounded-xl bg-slate-100 flex items-center justify-center">
                    <i data-lucide="list" class="w-5 h-5 text-slate-500"></i>
                </div>
                <div class="text-left">
                    <h2 class="font-black text-slate-800 text-base">Detail Transaksi Penjualan</h2>
                    <p class="text-xs text-slate-400 font-semibold">{{ $transaksi->count() }} transaksi dalam periode ini</p>
                </div>
            </div>
        </div>

        <div>
            <div class="hidden md:block overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="bg-slate-50 text-left">
                            <th class="px-6 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest">Tanggal</th>
                            <th class="px-4 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest">Pelanggan</th>
                            <th class="px-4 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest">Produk</th>
                            <th class="px-4 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest text-center">Qty</th>
                            <th class="px-4 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest">Total</th>
                            <th class="px-6 py-3 text-[10px] font-black text-slate-400 uppercase tracking-widest">Metode</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-50">
                        @foreach($transaksi as $trx)
                        <tr class="hover:bg-slate-50/60 transition-colors">
                            <td class="px-6 py-3.5">
                                <div class="text-xs font-bold text-slate-700">{{ \Carbon\Carbon::parse($trx->tanggal_transaksi ?? $trx->created_at)->translatedFormat('d M Y') }}</div>
                                <div class="text-[10px] text-slate-400">{{ \Carbon\Carbon::parse($trx->created_at)->format('H:i') }} WIB</div>
                            </td>
                            <td class="px-4 py-3.5">
                                <div class="text-xs font-bold text-slate-700">{{ $trx->pelanggan?->name ?? '—' }}</div>
                                <div class="flex items-center gap-1.5 mt-0.5">
                                    <span class="text-[9px] text-slate-400 font-mono bg-slate-50 px-1 py-0.5 rounded border border-slate-100">{{ $trx->customers_id }}</span>
                                    @php
                                        $ongkirData = \App\Models\Transaksi::getOngkirAndDistance($trx->pelanggan->titik_koordinat ?? null, $trx->pelanggan->address ?? null, $trx->customers_id ?? null, $trx->qty_beli_isi, $trx->id_produk);
                                    @endphp
                                    @if($ongkirData['distance'] !== null)
                                        <span class="inline-flex items-center gap-0.5 px-1.5 py-0.5 bg-blue-50 text-blue-600 rounded text-[9px] font-bold border border-blue-100">
                                            <i data-lucide="navigation" class="w-2 h-2"></i>
                                            {{ $ongkirData['distance'] }} km
                                        </span>
                                    @endif
                                </div>
                            </td>
                            <td class="px-4 py-3.5">
                                @php
                                    $pNama = $trx->produk?->nama_produk ?? $trx->id_produk;
                                    $is3kg = str_contains(strtolower($pNama), '3kg') || str_contains(strtolower($pNama), '3 kg') || (str_contains(strtolower($pNama), '3') && !str_contains(strtolower($pNama), '5'));
                                @endphp
                                @if($is3kg)
                                    <span class="inline-flex items-center gap-1 bg-emerald-50 border border-emerald-200 px-2 py-1 rounded-lg text-xs font-bold text-emerald-700">
                                        {{ $pNama }}
                                    </span>
                                @else
                                    <span class="inline-flex items-center gap-1 bg-pink-50 border border-pink-200 px-2 py-1 rounded-lg text-xs font-bold text-pink-700">
                                        {{ $pNama }}
                                    </span>
                                @endif
                            </td>
                            <td class="px-4 py-3.5 text-center">
                                <span class="text-slate-700 text-xs font-black">{{ $trx->qty_beli_isi }}</span>
                            </td>
                            <td class="px-4 py-3.5 font-mono">
                                <div class="text-[10px] font-semibold text-slate-400">Sub: Rp {{ number_format($trx->total_harga - $trx->ongkir, 0, ',', '.') }}</div>
                                <div class="text-[10px] font-semibold text-emerald-600">Ongkir: Rp {{ number_format($trx->ongkir, 0, ',', '.') }}</div>
                                <div class="text-sm font-black text-slate-800 mt-0.5">Rp {{ number_format($trx->total_harga, 0, ',', '.') }}</div>
                            </td>
                            <td class="px-6 py-3.5">
                                <span class="text-[10px] font-black px-2 py-1 rounded-full border bg-slate-50 text-slate-500 uppercase">
                                    {{ $trx->metode_bayar ?? '—' }}
                                </span>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="md:hidden divide-y divide-slate-50">
                @foreach($transaksi as $trx)
                <div class="p-4 flex items-center justify-between gap-3">
                    <div class="flex flex-col min-w-0">
                        <div class="text-sm font-bold text-slate-800 truncate">{{ $trx->pelanggan?->name ?? '—' }}</div>
                        @php
                            $pNama = $trx->produk?->nama_produk ?? $trx->id_produk;
                            $is3kg = str_contains(strtolower($pNama), '3kg') || str_contains(strtolower($pNama), '3 kg') || (str_contains(strtolower($pNama), '3') && !str_contains(strtolower($pNama), '5'));
                        @endphp
                        <div class="text-[10px] {{ $is3kg ? 'text-emerald-700 font-bold' : 'text-pink-700 font-bold' }}">
                            {{ $pNama }} · {{ $trx->qty_beli_isi }} tabung
                        </div>
                    </div>
                    <div class="text-right flex flex-col items-end">
                        <div class="text-sm font-black text-slate-800 font-mono">Rp {{ number_format($trx->total_harga,0,',','.') }}</div>
                        @if($trx->ongkir > 0)
                            <div class="text-[9px] text-emerald-600 font-bold">Ongkir: Rp {{ number_format($trx->ongkir,0,',','.') }}</div>
                        @endif
                        <div class="text-[10px] text-slate-400 mt-0.5">{{ \Carbon\Carbon::parse($trx->created_at)->format('d/m H:i') }}</div>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif

    @else
    {{-- ===== EMPTY STATE ===== --}}
    <div class="bg-white rounded-[2rem] border border-slate-100 shadow-sm p-10 md:p-16 text-center">
        <div class="mx-auto w-20 h-20 rounded-3xl bg-gas3kg/10 flex items-center justify-center mb-5">
            <i data-lucide="calendar-search" class="w-10 h-10 text-gas3kg"></i>
        </div>
        <h3 class="text-xl font-black text-slate-800 mb-2">Pilih Periode Terlebih Dahulu</h3>
        <p class="text-sm text-slate-400 font-medium max-w-sm mx-auto mb-6">Tentukan rentang tanggal pada form filter di atas untuk melihat laporan.</p>
        <div class="flex flex-wrap justify-center gap-2">
            <a href="{{ route('admin.laporan-penjualan', ['dari' => now()->startOfMonth()->format('Y-m-d'), 'sampai' => now()->format('Y-m-d')]) }}" class="bg-slate-100 hover:bg-slate-200 text-slate-600 font-bold text-xs px-4 py-2 rounded-xl transition-all">Bulan Ini</a>
            <a href="{{ route('admin.laporan-penjualan', ['dari' => now()->subDays(29)->format('Y-m-d'), 'sampai' => now()->format('Y-m-d')]) }}" class="bg-gas3kg hover:bg-green-700 text-white font-bold text-xs px-4 py-2 rounded-xl transition-all">30 Hari Terakhir</a>
        </div>
    </div>
    @endif
</div>
@endsection
