<div x-data>

    {{-- Header --}}
    <div class="flex items-center justify-between mb-6">
        <div>
            <h1 class="text-2xl font-bold text-slate-800">Laporan</h1>
            <p class="text-sm text-slate-500 mt-0.5">Ringkasan penjualan dari pesanan yang sudah selesai.</p>
        </div>

        @can('report.export')
            <div class="flex gap-2">
                <a href="{{ route('admin.laporan.export', array_merge(['type' => 'csv'], request()->query())) }}"
                    class="inline-flex items-center gap-2 px-4 py-2 text-sm font-medium text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50 transition-colors">
                    <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="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3"/>
                    </svg>
                    Export CSV
                </a>
                <a href="{{ route('admin.laporan.export', array_merge(['type' => 'excel'], request()->query())) }}"
                    class="inline-flex items-center gap-2 px-4 py-2 text-sm font-medium text-white bg-orange-500 rounded-lg hover:bg-orange-600 transition-colors">
                    <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="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3"/>
                    </svg>
                    Export Excel
                </a>
            </div>
        @endcan
    </div>

    {{-- Filter Periode (tab) --}}
    <div class="flex flex-wrap gap-2 mb-4">
        <button type="button" wire:click="setPeriod('today')"
            class="px-3.5 py-1.5 rounded-lg text-sm font-medium transition-colors
                {{ $period === 'today' ? 'bg-orange-500 text-white' : 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-50' }}">
            Hari Ini
        </button>
        <button type="button" wire:click="setPeriod('week')"
            class="px-3.5 py-1.5 rounded-lg text-sm font-medium transition-colors
                {{ $period === 'week' ? 'bg-orange-500 text-white' : 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-50' }}">
            Minggu Ini
        </button>
        <button type="button" wire:click="setPeriod('month')"
            class="px-3.5 py-1.5 rounded-lg text-sm font-medium transition-colors
                {{ $period === 'month' ? 'bg-orange-500 text-white' : 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-50' }}">
            Bulan Ini
        </button>
        <button type="button" wire:click="setPeriod('custom')"
            class="px-3.5 py-1.5 rounded-lg text-sm font-medium transition-colors
                {{ $period === 'custom' ? 'bg-orange-500 text-white' : 'bg-white text-slate-600 border border-slate-200 hover:bg-slate-50' }}">
            Custom Tanggal
        </button>
    </div>

    {{-- Custom Date Range --}}
    @if ($period === 'custom')
        <div class="bg-white rounded-xl border border-slate-200 p-4 mb-4">
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 max-w-md">
                <div>
                    <label class="block text-xs font-medium text-slate-500 mb-1">Dari Tanggal</label>
                    <input wire:model.live="dateFrom" type="date"
                        class="w-full px-3 py-2 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-orange-500" />
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-500 mb-1">Sampai Tanggal</label>
                    <input wire:model.live="dateTo" type="date"
                        class="w-full px-3 py-2 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-orange-500" />
                </div>
            </div>
        </div>
    @endif

    {{-- Loading indicator --}}
    <div wire:loading.flex wire:target="period, dateFrom, dateTo, perPage"
        class="items-center gap-2 text-sm text-slate-400 mb-3">
        <svg class="animate-spin w-4 h-4 text-orange-500" fill="none" viewBox="0 0 24 24">
            <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
            <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
        </svg>
        Memuat data...
    </div>

    {{-- Statistik --}}
    <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-6" wire:loading.class="opacity-60" wire:target="period, dateFrom, dateTo">
        <div class="bg-white rounded-xl border border-slate-200 p-4">
            <p class="text-xs text-slate-400 mb-1">Total Penjualan</p>
            <p class="text-lg font-bold text-slate-800">Rp {{ number_format($stats['total_penjualan'], 0, ',', '.') }}</p>
        </div>
        <div class="bg-white rounded-xl border border-slate-200 p-4">
            <p class="text-xs text-slate-400 mb-1">Total Pesanan</p>
            <p class="text-xl font-bold text-slate-800">{{ $stats['total_pesanan'] }}</p>
        </div>
        <div class="bg-white rounded-xl border border-slate-200 p-4">
            <p class="text-xs text-slate-400 mb-1">Produk Terjual</p>
            <p class="text-xl font-bold text-slate-800">{{ $stats['total_produk_terjual'] }}</p>
        </div>
        <div class="bg-white rounded-xl border border-slate-200 p-4">
            <p class="text-xs text-slate-400 mb-1">Total Customer</p>
            <p class="text-xl font-bold text-slate-800">{{ $stats['total_customer'] }}</p>
        </div>
    </div>

    <p class="text-xs text-slate-400 mb-3">
        Menampilkan data pesanan <strong>Selesai</strong> periode {{ $from->translatedFormat('d F Y') }} – {{ $to->translatedFormat('d F Y') }}
    </p>

    {{-- Tabel --}}
    <div class="bg-white rounded-xl border border-slate-200 overflow-hidden" wire:loading.class="opacity-60" wire:target="period, dateFrom, dateTo, perPage">

        {{-- Mobile: card list (hidden di sm+) --}}
        <div class="sm:hidden divide-y divide-slate-100">
            @forelse ($orders as $order)
                @php
                    $sc = match($order->status->value) {
                        'pending'    => 'bg-slate-100 text-slate-600',
                        'paid'       => 'bg-blue-100 text-blue-700',
                        'processing' => 'bg-indigo-100 text-indigo-700',
                        'delivering' => 'bg-cyan-100 text-cyan-700',
                        'completed'  => 'bg-teal-100 text-teal-700',
                        'cancelled'  => 'bg-red-100 text-red-600',
                        default      => 'bg-slate-100 text-slate-600',
                    };
                @endphp
                <a href="{{ route('admin.pesanan.show', $order->id) }}"
                   class="block px-4 py-3 hover:bg-slate-50 transition-colors"
                   wire:navigate>
                    <div class="flex items-center justify-between gap-2 mb-1">
                        <span class="font-mono text-xs font-semibold text-orange-600">
                            {{ $order->order_number }}
                        </span>
                        <span class="text-[11px] font-semibold px-2 py-0.5 rounded-full {{ $sc }}">
                            {{ $order->status->label() }}
                        </span>
                    </div>
                    <div class="flex items-center justify-between gap-2">
                        <div class="min-w-0">
                            <p class="text-sm font-medium text-slate-800 truncate">{{ $order->customer_name }}</p>
                            <p class="text-xs text-slate-400">{{ $order->created_at->format('d M Y, H:i') }}</p>
                        </div>
                        <p class="text-sm font-bold text-slate-700 shrink-0">
                            Rp {{ number_format($order->grand_total, 0, ',', '.') }}
                        </p>
                    </div>
                </a>
            @empty
                <div class="px-4 py-12 text-center text-sm text-slate-400">
                    Belum ada pesanan selesai pada periode ini.
                </div>
            @endforelse
        </div>

        {{-- Desktop: tabel (hidden di mobile) --}}
        <div class="hidden sm:block overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-slate-50 border-b border-slate-200">
                    <tr>
                        <th class="text-left px-4 py-3 font-semibold text-slate-600 whitespace-nowrap">Tanggal</th>
                        <th class="text-left px-4 py-3 font-semibold text-slate-600 whitespace-nowrap">Invoice</th>
                        <th class="text-left px-4 py-3 font-semibold text-slate-600 whitespace-nowrap">Customer</th>
                        <th class="text-right px-4 py-3 font-semibold text-slate-600 whitespace-nowrap">Total</th>
                        <th class="text-center px-4 py-3 font-semibold text-slate-600 whitespace-nowrap">Status</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse ($orders as $order)
                        <tr class="hover:bg-slate-50 transition-colors" wire:key="laporan-order-{{ $order->id }}">
                            <td class="px-4 py-3 text-slate-600 whitespace-nowrap">
                                {{ $order->created_at->format('d M Y') }}
                                <p class="text-xs text-slate-400">{{ $order->created_at->format('H:i') }}</p>
                            </td>
                            <td class="px-4 py-3">
                                <a href="{{ route('admin.pesanan.show', $order->id) }}" wire:navigate
                                    class="font-mono text-xs font-semibold text-orange-600 hover:text-orange-700">
                                    {{ $order->order_number }}
                                </a>
                            </td>
                            <td class="px-4 py-3">
                                <p class="font-medium text-slate-800">{{ $order->customer_name }}</p>
                            </td>
                            <td class="px-4 py-3 text-right font-semibold text-slate-800 whitespace-nowrap">
                                Rp {{ number_format($order->grand_total, 0, ',', '.') }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="inline-flex px-2.5 py-1 rounded-full text-xs font-semibold bg-green-100 text-green-700">
                                    {{ $order->status->label() }}
                                </span>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="px-4 py-16 text-center">
                                <svg class="w-12 h-12 mx-auto mb-3 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 13.125C3 12.504 3.504 12 4.125 12h2.25c.621 0 1.125.504 1.125 1.125v6.75C7.5 20.496 6.996 21 6.375 21h-2.25A1.125 1.125 0 013 19.875v-6.75zM9.75 8.625c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125v11.25c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V8.625zM16.5 4.125c0-.621.504-1.125 1.125-1.125h2.25C20.496 3 21 3.504 21 4.125v15.75c0 .621-.504 1.125-1.125 1.125h-2.25a1.125 1.125 0 01-1.125-1.125V4.125z"/>
                                </svg>
                                <p class="text-slate-400">Belum ada pesanan selesai pada periode ini.</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Footer: Pagination + Per Page --}}
        <div class="flex flex-col sm:flex-row items-center justify-between gap-3 px-4 py-3 border-t border-slate-100">
            <div class="flex items-center gap-2 text-sm text-slate-500">
                <span>Tampilkan</span>
                <select wire:model.live="perPage"
                    class="px-2 py-1 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-orange-500 bg-white">
                    <option value="10">10</option>
                    <option value="25">25</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
                <span>data per halaman</span>
            </div>
            <div>{{ $orders->links() }}</div>
        </div>
    </div>

</div>
