@extends('layouts.app')

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

    {{-- Date Navigation --}}
    <div class="mb-6 rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="flex flex-wrap items-center justify-between gap-4">
            <div>
                <h3 class="text-xl font-bold text-gray-800 dark:text-white/90">{{ \Carbon\Carbon::parse($date)->translatedFormat('l, d F Y') }}</h3>
                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">Ringkasan aktivitas order pada tanggal ini</p>
            </div>
            <div class="flex items-center gap-2">
                <a href="{{ route('reports.daily', ['date' => \Carbon\Carbon::parse($date)->subDay()->toDateString()]) }}"
                    class="h-10 rounded-lg bg-gray-100 px-4 flex items-center gap-2 text-sm font-medium text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700 transition-colors">
                    <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg>
                    Kemarin
                </a>
                <form method="GET" class="flex flex-wrap items-center gap-2">
                    <div class="w-40">
                        <x-form.date-picker name="date" placeholder="Pilih tanggal" :defaultDate="$date" />
                    </div>
                    <div class="w-40">
                        <x-form.select name="machine_id" placeholder="Mesin">
                            @foreach($machines as $machine)
                                <option value="{{ $machine->id }}" {{ request('machine_id') == $machine->id ? 'selected' : '' }}>{{ $machine->name }}</option>
                            @endforeach
                        </x-form.select>
                    </div>
                    <div class="w-40">
                        <x-form.select name="category_id" placeholder="Kategori">
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
                            @endforeach
                        </x-form.select>
                    </div>
                    <button type="submit" class="h-11 rounded-lg bg-brand-500 px-4 text-sm font-medium text-white hover:bg-brand-600 transition-colors">
                        Tampilkan
                    </button>
                </form>
                <a href="{{ route('reports.daily', ['date' => \Carbon\Carbon::parse($date)->addDay()->toDateString()]) }}"
                    class="h-10 rounded-lg bg-gray-100 px-4 flex items-center gap-2 text-sm font-medium text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700 transition-colors">
                    Besok
                    <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
                </a>
            </div>
        </div>
    </div>

    {{-- Stat Cards --}}
    <div class="mb-6 grid grid-cols-2 gap-4 sm:grid-cols-3 lg:grid-cols-5">
        <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 2"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="Sudah Bayar" value="{{ $summary['paid_orders'] ?? 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="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="Belum Bayar" value="{{ $summary['unpaid_orders'] ?? 0 }}" color="warning">
            <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 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="Selesai" value="{{ $summary['completed_orders'] ?? 0 }}" 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="M5 13l4 4L19 7"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="Sudah Diambil" value="{{ $summary['picked_up_orders'] ?? 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="M5 13l4 4L19 7"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="Belum Diambil" value="{{ $summary['not_picked_up_orders'] ?? 0 }}" color="warning">
            <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 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card title="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>
    </div>

    {{-- Orders Table --}}
    <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">Daftar Order</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">Waktu</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-right text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Total</th>
                        <th class="px-5 py-3 text-center text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Bayar</th>
                        <th class="px-5 py-3 text-center text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Produksi</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 font-medium text-gray-800 dark:text-white/90">{{ $order->created_at->format('H:i') }}</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-right font-medium text-gray-800 dark:text-white/90">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                        <td class="px-5 py-4 text-center">
                            <span class="inline-flex rounded-full px-2.5 py-0.5 text-xs font-medium {{ $order->getPaymentStatusBadgeClass() }}">
                                {{ $order->getPaymentStatusLabel() }}
                            </span>
                        </td>
                        <td class="px-5 py-4 text-center">
                            <span class="inline-flex rounded-full px-2.5 py-0.5 text-xs font-medium {{ $order->getProductionStatusBadgeClass() }}">
                                {{ $order->getProductionStatusLabel() }}
                            </span>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" 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="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                                </svg>
                                <p class="text-sm text-gray-500 dark:text-gray-400">Tidak ada order pada 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="3" class="px-5 py-4 text-sm font-semibold text-gray-800 dark:text-white/90">Total Pendapatan (Lunas)</td>
                        <td class="px-5 py-4 text-right text-base font-bold text-success-600 dark:text-success-400">Rp {{ number_format($summary['total_revenue'], 0, ',', '.') }}</td>
                        <td colspan="2"></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
