@extends('admin.layouts.app')

@section('title', 'طلبات المطعم - ' . $restaurant->name)

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">طلبات المطعم - {{ $restaurant->name }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('admin.restaurants.show', $restaurant->id) }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> العودة للمطعم
                        </a>
                        <a href="{{ route('admin.restaurants') }}" class="btn btn-info">
                            <i class="fas fa-utensils"></i> جميع المطاعم
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <!-- إحصائيات سريعة -->
                    <div class="row mb-4">
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon bg-info"><i class="fas fa-shopping-cart"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">إجمالي الطلبات</span>
                                    <span class="info-box-number">{{ $stats['total'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon bg-warning"><i class="fas fa-clock"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">معلقة</span>
                                    <span class="info-box-number">{{ $stats['pending'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon bg-primary"><i class="fas fa-check-circle"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">مؤكدة</span>
                                    <span class="info-box-number">{{ $stats['confirmed'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon" style="background-color: #6f42c1;"><i class="fas fa-utensils"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">قيد التجهيز</span>
                                    <span class="info-box-number">{{ $stats['preparing'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon bg-success"><i class="fas fa-check"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">جاهزة</span>
                                    <span class="info-box-number">{{ $stats['ready'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="info-box">
                                <span class="info-box-icon bg-success"><i class="fas fa-truck"></i></span>
                                <div class="info-box-content">
                                    <span class="info-box-text">مكتملة</span>
                                    <span class="info-box-number">{{ $stats['delivered'] }}</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- فلترة -->
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <form method="GET" action="{{ route('admin.restaurants.orders.index', $restaurant->id) }}" class="form-inline">
                                <div class="form-group mr-3">
                                    <label for="order_number" class="mr-2">رقم الطلب:</label>
                                    <input type="text" name="order_number" id="order_number" class="form-control" 
                                           value="{{ request('order_number') }}" placeholder="رقم الطلب">
                                </div>
                                <div class="form-group mr-3">
                                    <label for="customer" class="mr-2">اسم العميل:</label>
                                    <input type="text" name="customer" id="customer" class="form-control" 
                                           value="{{ request('customer') }}" placeholder="اسم العميل">
                                </div>
                                <div class="form-group mr-3">
                                    <label for="status" class="mr-2">الحالة:</label>
                                    <select name="status" id="status" class="form-control">
                                        <option value="all" {{ request('status') == 'all' || !request('status') ? 'selected' : '' }}>جميع الحالات</option>
                                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>معلق</option>
                                        <option value="confirmed" {{ request('status') == 'confirmed' ? 'selected' : '' }}>مؤكد</option>
                                        <option value="preparing" {{ request('status') == 'preparing' ? 'selected' : '' }}>قيد التجهيز</option>
                                        <option value="ready" {{ request('status') == 'ready' ? 'selected' : '' }}>جاهز</option>
                                        <option value="picked_up" {{ request('status') == 'picked_up' ? 'selected' : '' }}>تم الاستلام</option>
                                        <option value="delivered" {{ request('status') == 'delivered' ? 'selected' : '' }}>مكتمل</option>
                                        <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>ملغي</option>
                                    </select>
                                </div>
                                <div class="form-group mr-3">
                                    <label for="date_from" class="mr-2">من تاريخ:</label>
                                    <input type="date" name="date_from" id="date_from" class="form-control" 
                                           value="{{ request('date_from') }}">
                                </div>
                                <div class="form-group mr-3">
                                    <label for="date_to" class="mr-2">إلى تاريخ:</label>
                                    <input type="date" name="date_to" id="date_to" class="form-control" 
                                           value="{{ request('date_to') }}">
                                </div>
                                <button type="submit" class="btn btn-primary">
                                    <i class="fas fa-search"></i> بحث
                                </button>
                                <a href="{{ route('admin.restaurants.orders.index', $restaurant->id) }}" class="btn btn-secondary">
                                    <i class="fas fa-times"></i> مسح
                                </a>
                            </form>
                        </div>
                    </div>

                    <!-- جدول الطلبات -->
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>رقم الطلب</th>
                                    <th>العميل</th>
                                    <th>السائق</th>
                                    <th>المجموع</th>
                                    <th>العنوان</th>
                                    <th>الحالة</th>
                                    <th>طريقة الدفع</th>
                                    <th>تاريخ الطلب</th>
                                    <th>الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($orders as $order)
                                <tr>
                                    <td>{{ $order->id }}</td>
                                    <td>
                                        <strong>{{ $order->order_number ?? '#' . $order->id }}</strong>
                                    </td>
                                    <td>
                                        @if($order->customer)
                                            {{ $order->customer->name }}
                                            <br><small class="text-muted">{{ $order->customer->phone ?? '' }}</small>
                                        @else
                                            <span class="text-muted">غير محدد</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($order->driver)
                                            {{ $order->driver->name }}
                                            <br><small class="text-muted">{{ $order->driver->phone ?? '' }}</small>
                                        @else
                                            <span class="text-muted">لم يتم تعيين</span>
                                        @endif
                                    </td>
                                    <td>
                                        <strong>{{ number_format($order->total, 0) }} د.ع</strong>
                                        <br><small class="text-muted">
                                            فرعي: {{ number_format($order->subtotal, 0) }} د.ع
                                        </small>
                                    </td>
                                    <td>{{ Str::limit($order->delivery_address ?? 'غير محدد', 30) }}</td>
                                    <td>
                                        @php
                                            $statusColors = [
                                                'pending' => 'warning',
                                                'confirmed' => 'info',
                                                'preparing' => 'primary',
                                                'ready' => 'success',
                                                'picked_up' => 'secondary',
                                                'delivered' => 'success',
                                                'cancelled' => 'danger'
                                            ];
                                            $statusLabels = [
                                                'pending' => 'معلق',
                                                'confirmed' => 'مؤكد',
                                                'preparing' => 'قيد التجهيز',
                                                'ready' => 'جاهز',
                                                'picked_up' => 'تم الاستلام',
                                                'delivered' => 'مكتمل',
                                                'cancelled' => 'ملغي'
                                            ];
                                        @endphp
                                        <span class="badge bg-{{ $statusColors[$order->status] ?? 'secondary' }}">
                                            {{ $statusLabels[$order->status] ?? $order->status }}
                                        </span>
                                    </td>
                                    <td>
                                        @php
                                            $paymentLabels = [
                                                'cash' => 'نقدي',
                                                'card' => 'بطاقة',
                                                'online' => 'أونلاين'
                                            ];
                                        @endphp
                                        {{ $paymentLabels[$order->payment_method] ?? $order->payment_method ?? 'غير محدد' }}
                                        <br>
                                        <small class="text-muted">
                                            <span class="badge bg-{{ ($order->payment_status ?? 'unpaid') == 'paid' ? 'success' : 'warning' }}">
                                                {{ ($order->payment_status ?? 'unpaid') == 'paid' ? 'مدفوع' : 'غير مدفوع' }}
                                            </span>
                                        </small>
                                    </td>
                                    <td>{{ $order->created_at->format('Y-m-d H:i') }}</td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ route('admin.restaurants.orders.show', [$restaurant->id, $order->id]) }}" 
                                               class="btn btn-info btn-sm" title="عرض التفاصيل">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="10" class="text-center py-4">
                                        <i class="fas fa-shopping-cart fa-3x text-muted mb-3"></i>
                                        <p class="text-muted">لا توجد طلبات</p>
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <div class="d-flex justify-content-center mt-3">
                        {{ $orders->appends(request()->query())->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

