@extends('layouts.app')

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

    {{-- 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 2"/></svg>
            </x-slot>
        </x-posgraf.stat-card>
        <x-posgraf.stat-card
            title="Order Lunas"
            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="Order 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>
    </div>

    {{-- Filter --}}
    <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">
                    Filter
                </button>
                <a href="{{ route('reports.transactions') }}" 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">
                    Reset
                </a>
            </div>
        </form>
    </div>

    {{-- Orders Table --}}
    <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="flex items-center justify-between 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">Riwayat Transaksi</h3>
            <a 
                href="{{ route('print.laporan-transaksi', array_merge(['start_date' => $startDate, 'end_date' => $endDate], request()->only(['machine_id', 'category_id']))) }}" 
                target="_blank"
                class="inline-flex items-center gap-2 rounded-lg bg-brand-500 px-4 py-2 text-sm font-medium text-white hover:bg-brand-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>
        </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 text-gray-500">Waktu</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase text-gray-500">Invoice</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase text-gray-500">Pelanggan</th>
                        <th class="px-5 py-3 text-center text-xs font-semibold uppercase text-gray-500">Bayar</th>
                        <th class="px-5 py-3 text-center text-xs font-semibold uppercase text-gray-500">Produksi</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase text-gray-500">Designer</th>
                        <th class="px-5 py-3 text-left text-xs font-semibold uppercase text-gray-500">Operator</th>
                        <th class="px-5 py-3 text-right text-xs font-semibold uppercase text-gray-500">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]">
                        <td class="px-5 py-4 text-sm text-gray-500">
                            <div>{{ $order->created_at->format('d/m/Y') }}</div>
                            <div class="text-xs">{{ $order->created_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">{{ $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-center">
                            @if($order->payment_status === 'paid')
                                <span class="inline-flex items-center rounded-full bg-success-50 px-2.5 py-0.5 text-xs font-medium text-success-700 dark:bg-success-500/10 dark:text-success-400">Lunas</span>
                            @else
                                <span class="inline-flex items-center rounded-full bg-warning-50 px-2.5 py-0.5 text-xs font-medium text-warning-700 dark:bg-warning-500/10 dark:text-warning-400">Belum</span>
                            @endif
                        </td>
                        <td class="px-5 py-4 text-center">
                            @if($order->production_status === 'selesai')
                                <span class="inline-flex items-center rounded-full bg-success-50 px-2.5 py-0.5 text-xs font-medium text-success-700 dark:bg-success-500/10 dark:text-success-400">Selesai</span>
                            @elseif($order->production_status === 'proses')
                                <span class="inline-flex items-center rounded-full bg-blue-50 px-2.5 py-0.5 text-xs font-medium text-blue-700 dark:bg-blue-500/10 dark:text-blue-400">Proses</span>
                            @else
                                <span class="inline-flex items-center rounded-full bg-gray-100 px-2.5 py-0.5 text-xs font-medium text-gray-700 dark:bg-gray-700 dark:text-gray-400">Antri</span>
                            @endif
                        </td>
                        <td class="px-5 py-4 text-sm text-gray-500">{{ $order->designer->name ?? '-' }}</td>
                        <td class="px-5 py-4 text-sm text-gray-500">{{ $order->operator->name ?? '-' }}</td>
                        <td class="px-5 py-4 text-right text-sm font-semibold text-gray-800 dark:text-white/90">Rp {{ number_format($order->total_price, 0, ',', '.') }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-5 py-12 text-center text-sm text-gray-500">Tidak ada data</td>
                    </tr>
                    @endforelse
                </tbody>
            </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>
@endsection
