<div>
    <x-layouts.app title="{{ __('Purchase Report') }}">
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Purchase Report') }}</h1>
                <p class="text-muted mb-0">{{ __('Purchase orders summary and details') }}</p>
            </div>
            <div class="d-flex gap-2">
                <button wire:click="export" class="btn btn-outline-success">
                    <i class="fas fa-file-excel me-2"></i>{{ __('Export') }}
                </button>
                <a href="{{ route('pdf.report.purchases', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'supplier_id' => $supplierFilter, 'status' => $statusFilter]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.purchases', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'supplier_id' => $supplierFilter, 'status' => $statusFilter, 'preview' => true]) }}" target="_blank" class="btn btn-outline-secondary">
                    <i class="fas fa-print me-2"></i>{{ __('Print') }}
                </a>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="row g-3 mb-3">
            <div class="col-md-6">
                <div class="card bg-primary text-white">
                    <div class="card-body text-center">
                        <h5>{{ __('Total Orders') }}</h5>
                        <h2>{{ number_format($totals->total_count) }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card bg-success text-white">
                    <div class="card-body text-center">
                        <h5>{{ __('Total Amount') }}</h5>
                        <h2>{{ number_format($totals->total_amount, 2) }} {{ $currency }}</h2>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-3">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <input type="date" class="form-control" wire:model.live="dateFrom">
                    </div>
                    <div class="col-md-3">
                        <input type="date" class="form-control" wire:model.live="dateTo">
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" wire:model.live="supplierFilter">
                            <option value="">{{ __('All Suppliers') }}</option>
                            @foreach($suppliers as $s)
                                <option value="{{ $s->id }}">{{ $s->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" wire:model.live="statusFilter">
                            <option value="">{{ __('All Status') }}</option>
                            <option value="draft">{{ __('Draft') }}</option>
                            <option value="ordered">{{ __('Ordered') }}</option>
                            <option value="received">{{ __('Received') }}</option>
                            <option value="cancelled">{{ __('Cancelled') }}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Table -->
        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>{{ __('PO #') }}</th>
                                <th>{{ __('Date') }}</th>
                                <th>{{ __('Supplier') }}</th>
                                <th class="text-end">{{ __('Total') }}</th>
                                <th class="text-center">{{ __('Status') }}</th>
                                <th>{{ __('Created By') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($orders as $order)
                                <tr>
                                    <td><strong>{{ $order->po_number }}</strong></td>
                                    <td>{{ $order->order_date->format('Y-m-d') }}</td>
                                    <td>{{ $order->supplier?->name ?? '-' }}</td>
                                    <td class="text-end">{{ number_format($order->total, 2) }}</td>
                                    <td class="text-center">
                                        <span class="badge bg-{{ $order->status === 'received' ? 'success' : ($order->status === 'ordered' ? 'primary' : ($order->status === 'cancelled' ? 'danger' : 'secondary')) }}">
                                            {{ __(ucfirst($order->status)) }}
                                        </span>
                                    </td>
                                    <td>{{ $order->creator?->name ?? '-' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="6" class="text-center py-4 text-muted">{{ __('No purchase orders found.') }}</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($orders->hasPages())
                <div class="card-footer">{{ $orders->links() }}</div>
            @endif
        </div>
    </x-layouts.app>
</div>
