@extends('layouts.app')

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

    {{-- Stat Cards --}}
    <div class="mb-6 grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
        <x-posgraf.stat-card
            title="Total Selesai"
            value="{{ $summary['total_completed'] ?? 0 }} Order"
            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="Total Qty"
            value="{{ $summary['total_qty'] ?? 0 }} pcs"
            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="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="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4 items-end">
            <div class="w-full">
                <x-form.date-picker
                    name="start_date"
                    label="Dari Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$startDate"
                />
            </div>
            <div class="w-full">
                <x-form.date-picker
                    name="end_date"
                    label="Sampai Tanggal"
                    placeholder="Pilih tanggal"
                    :defaultDate="$endDate"
                />
            </div>
            <div class="w-full">
                <x-form.select name="machine_id" label="Mesin" placeholder="Semua 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-full">
                <x-form.select name="category_id" label="Kategori" placeholder="Semua Kategori">
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
                    @endforeach
                </x-form.select>
            </div>
            <div class="flex gap-2 w-full">
                <button type="submit" class="h-11 flex-1 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.production') }}" class="h-11 flex items-center justify-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>
            </div>
            <a href="{{ route('print.laporan-produksi', array_merge(['start_date' => $startDate, 'end_date' => $endDate], request()->only(['machine_id', 'category_id']))) }}" 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>

    {{-- Summary by Operator --}}
    @if($byOperator->count() > 0)
    <div class="mb-6 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">
            <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Produksi Per Operator</h3>
        </div>
        <div class="p-5 grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
            @foreach($byOperator as $item)
            <div class="rounded-xl border border-gray-200 p-4 dark:border-gray-700 hover:border-brand-200 hover:bg-brand-50/30 dark:hover:border-brand-800 dark:hover:bg-brand-500/5 transition-colors">
                <div class="flex items-center gap-3">
                    <div class="flex h-10 w-10 items-center justify-center rounded-full bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400">
                        <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/></svg>
                    </div>
                    <div class="flex-1">
                        <div class="text-sm font-medium text-gray-800 dark:text-white/90">{{ $item->operator?->name ?? 'Unknown' }}</div>
                        <div class="flex items-center gap-3 mt-1">
                            <span class="text-xs text-gray-500 dark:text-gray-400">{{ $item->total_orders }} order</span>
                            <span class="text-sm font-semibold text-brand-600 dark:text-brand-400">{{ $item->total_qty }} pcs</span>
                        </div>
                    </div>
                </div>
            </div>
            @endforeach
        </div>
    </div>
    @endif

    {{-- 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 Produksi</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 Selesai</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">Qty</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Operator</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->finished_at?->format('d M') }}</div>
                            <div class="text-xs">{{ $order->finished_at?->format('H:i') }}</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-medium text-gray-800 dark:text-white/90">{{ $order->items->sum('qty') }} pcs</td>
                        <td class="px-5 py-4 text-sm text-gray-600 dark:text-gray-400">{{ $order->operator->name ?? '-' }}</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="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
                                </svg>
                                <p class="text-sm text-gray-500 dark:text-gray-400">Tidak ada data produksi</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</td>
                        <td class="px-5 py-4 text-right text-base font-bold text-brand-600 dark:text-brand-400">{{ $summary['total_qty'] }} pcs</td>
                        <td></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
