@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb pageTitle="{{ $title ?? 'Laporan Keuangan' }}" />

    {{-- Stat Cards --}}
    <div class="mb-6 grid grid-cols-1 gap-4 sm:grid-cols-3">
        <x-posgraf.stat-card
            title="Total Order"
            value="{{ $summary['total_orders'] ?? 0 }}"
            color="brand"
        >
            <x-slot name="icon">
                <svg class="size-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card
            title="Total Pendapatan"
            value="Rp {{ number_format($summary['total_revenue'] ?? 0, 0, ',', '.') }}"
            color="success"
        >
            <x-slot name="icon">
                <svg class="size-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card
            title="Total Qty"
            value="{{ $summary['total_qty'] ?? 0 }} pcs"
            color="blue"
        >
            <x-slot name="icon">
                <svg class="size-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
    </div>

    {{-- Filter Card --}}
    <div class="mb-6 rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
        <form method="GET" class="flex flex-wrap items-end gap-4">
            <div class="w-full sm:w-auto">
                <x-form.date-picker
                    name="start_date"
                    label="Dari Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$startDate"
                />
            </div>
            <div class="w-full sm:w-auto">
                <x-form.date-picker
                    name="end_date"
                    label="Sampai Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$endDate"
                />
            </div>
            <button type="submit" class="h-11 rounded-lg bg-brand-500 px-5 text-sm font-medium text-white hover:bg-brand-600 transition-colors">
                <svg class="inline-block w-4 h-4 mr-1.5 -mt-0.5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
                Filter
            </button>
            <a href="{{ route('reports.index') }}" class="h-11 flex items-center rounded-lg border border-gray-300 px-4 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:border-gray-700 dark:text-gray-300 dark:hover:bg-gray-800 transition-colors">
                Reset
            </a>
            <a href="{{ route('print.laporan', ['start_date' => $startDate, 'end_date' => $endDate]) }}" target="_blank" class="ml-auto h-11 inline-flex items-center gap-2 rounded-lg bg-gray-800 px-4 text-sm font-medium text-white hover:bg-gray-900 dark:bg-gray-700 dark:hover:bg-gray-600 transition-colors">
                <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" /></svg>
                Cetak
            </a>
        </form>
    </div>

    {{-- Main Content: Detail Orders --}}
    <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
            <div class="flex flex-wrap items-center justify-between gap-4">
                <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Detail Transaksi</h3>
                
                @if($orders->total() > 0)
                <div class="flex items-center gap-3">
                    <div class="flex items-center gap-2">
                        <label class="text-sm text-gray-500 dark:text-gray-400">Tampilkan</label>
                        <select 
                            onchange="window.location.href = updateQueryParam(window.location.href, 'per_page', this.value)"
                            class="h-9 rounded-lg border border-gray-300 bg-transparent px-3 text-sm text-gray-700 focus:border-brand-500 focus:outline-none dark:border-gray-600 dark:bg-gray-800 dark:text-gray-300"
                        >
                            @foreach([10, 15, 25, 50] as $option)
                                <option value="{{ $option }}" {{ request('per_page', 15) == $option ? 'selected' : '' }}>{{ $option }}</option>
                            @endforeach
                        </select>
                    </div>
                    <span class="text-sm text-gray-500 dark:text-gray-400">
                        {{ $orders->firstItem() }}-{{ $orders->lastItem() }} dari {{ number_format($orders->total()) }}
                    </span>
                </div>
                @endif
            </div>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="border-b border-gray-200 bg-gray-50 dark:border-gray-800 dark:bg-gray-900/50">
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Tgl Bayar</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Invoice</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Pelanggan</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Item</th>
                        <th class="px-5 py-3 text-right text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Total</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                    @forelse($orders as $order)
                    <tr class="hover:bg-gray-50 dark:hover:bg-white/[0.02] transition-colors">
                        <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                            <div class="font-medium text-gray-800 dark:text-white/90">{{ $order->paid_at?->format('d M') }}</div>
                            <div class="text-xs">{{ $order->paid_at?->format('Y') }}</div>
                        </td>
                        <td class="px-5 py-4">
                            <a href="{{ route('orders.show', $order) }}" class="text-sm font-medium text-brand-600 hover:text-brand-700 dark:text-brand-400">{{ $order->invoice_number }}</a>
                        </td>
                        <td class="px-5 py-4 text-sm text-gray-800 dark:text-white/90">{{ $order->customer_name }}</td>
                        <td class="px-5 py-4 text-sm text-gray-600 dark:text-gray-400">
                            {{ Str::limit($order->items->pluck('file_name')->join(', '), 40) }}
                        </td>
                        <td class="px-5 py-4 text-sm text-right font-semibold text-gray-800 dark:text-white/90">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-5 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="mb-3 h-12 w-12 text-gray-300 dark:text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                </svg>
                                <p class="text-sm text-gray-500 dark:text-gray-400">Tidak ada data pada rentang tanggal ini</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
                @if($orders->count() > 0)
                <tfoot class="border-t-2 border-gray-300 bg-gray-50 dark:border-gray-700 dark:bg-gray-900/50">
                    <tr>
                        <td colspan="4" class="px-5 py-4 text-sm font-semibold text-gray-800 dark:text-white/90">Total Keseluruhan</td>
                        <td class="px-5 py-4 text-right text-base font-bold text-brand-600 dark:text-brand-400">Rp {{ number_format($summary['total_revenue'], 0, ',', '.') }}</td>
                    </tr>
                </tfoot>
                @endif
            </table>
        </div>

        @if($orders->hasPages())
        <div class="border-t border-gray-200 px-5 py-4 dark:border-gray-800">
            {{ $orders->links() }}
        </div>
        @endif
    </div>

    @push('scripts')
    <script>
    function updateQueryParam(url, param, value) {
        const urlObj = new URL(url);
        urlObj.searchParams.set(param, value);
        urlObj.searchParams.delete('page');
        return urlObj.toString();
    }
    </script>
    @endpush
@endsection
