@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Orders</h4>
    <a href="{{ route('pos.index') }}" class="btn btn-primary">
        <i class="bi bi-cart"></i> New Sale
    </a>
</div>

<div class="card">
    <div class="card-header">
        <form method="GET" action="{{ route('orders.index') }}" class="row g-3">
            <div class="col-md-2">
                <input type="text" name="invoice_number" class="form-control" placeholder="Invoice #" value="{{ request('invoice_number') }}">
            </div>
            <div class="col-md-2">
                <input type="date" name="date_from" class="form-control" value="{{ request('date_from') }}">
            </div>
            <div class="col-md-2">
                <input type="date" name="date_to" class="form-control" value="{{ request('date_to') }}">
            </div>
            <div class="col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed</option>
                    <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                    <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">Filter</button>
            </div>
            <div class="col-md-2">
                <a href="{{ route('orders.index') }}" class="btn btn-secondary w-100">Reset</a>
            </div>
        </form>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Invoice #</th>
                    <th>Date</th>
                    <th>Customer</th>
                    <th>Total</th>
                    <th>Paid</th>
                    <th>Change</th>
                    <th>Payment</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($orders as $order)
                    <tr>
                        <td>{{ $order->invoice_number }}</td>
                        <td>{{ $order->created_at->format('M d, Y H:i') }}</td>
                        <td>{{ $order->customer->name ?? 'Walk-in' }}</td>
                        <td>{{ number_format($order->total, 2) }}</td>
                        <td>{{ number_format($order->paid_amount, 2) }}</td>
                        <td>{{ number_format($order->change_amount, 2) }}</td>
                        <td>
                            @if($order->payment_methods)
                                @foreach(json_decode($order->payment_methods, true) as $pm)
                                    <span class="badge bg-info me-1">{{ $pm['method'] }}: {{ number_format($pm['amount'], 2) }}</span>
                                @endforeach
                            @else
                                {{ ucfirst($order->payment_method) }}
                            @endif
                        </td>
                        <td>
                            <span class="badge {{ $order->status === 'completed' ? 'bg-success' : ($order->status === 'pending' ? 'bg-warning' : 'bg-danger') }}">
                                {{ ucfirst($order->status) }}
                            </span>
                        </td>
                        <td>
                            <a href="{{ route('orders.show', $order->id) }}" class="btn btn-sm btn-info">
                                <i class="bi bi-eye"></i>
                            </a>
                            <a href="{{ route('orders.receipt', $order->id) }}" class="btn btn-sm btn-secondary" target="_blank">
                                <i class="bi bi-printer"></i>
                            </a>
                            @if($order->status !== 'cancelled')
                                @if(Auth::user()->hasPermission('orders.cancel'))
                                <form action="{{ route('orders.destroy', $order->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Cancel this order?')">
                                        <i class="bi bi-x-circle"></i>
                                    </button>
                                </form>
                                @endif
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" class="text-center">No orders found.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="card-footer">
        {{ $orders->withQueryString()->links() }}
    </div>
</div>
@endsection
