@extends('layouts.admin', ['title' => 'مركز الطلبات'])

@php
    $fmt = fn ($minor) => number_format(($minor ?? 0) / 100, 2).' EGP';
    $statusLabel = fn ($status) => [
        'pending' => 'معلق',
        'paid' => 'مدفوع',
        'failed' => 'فاشل',
        'refunded' => 'مسترد',
        'processing' => 'قيد التنفيذ',
        'completed' => 'مكتمل',
        'info_required' => 'يحتاج بيانات',
    ][$status] ?? $status;
@endphp

@section('content')
    <section class="ac-section">
        <div class="ac-panel">
            <div class="ac-panel-head">
                <div>
                    <h2>مركز الطلبات</h2>
                    <p>إدارة الطلبات بفلترة سريعة، حالات واضحة، وربط مباشر بالتنفيذ والعميل.</p>
                </div>
                <div class="ac-actions">
                    <a class="ac-btn secondary" href="{{ route('admin.exports.orders') }}">تصدير CSV</a>
                    <a class="ac-btn secondary" href="{{ route('admin.fulfillment.index') }}">مكتب التنفيذ</a>
                </div>
                <form method="GET" class="ac-filter">
                    <input name="q" value="{{ $query }}" placeholder="رقم الطلب، العميل، البريد...">
                    <select name="payment_status">
                        <option value="">كل حالات الدفع</option>
                        @foreach(['pending', 'paid', 'failed', 'refunded'] as $status)
                            <option value="{{ $status }}" @selected($payment === $status)>{{ $statusLabel($status) }}</option>
                        @endforeach
                    </select>
                    <select name="fulfillment_status">
                        <option value="">كل حالات التنفيذ</option>
                        @foreach(['pending', 'processing', 'completed', 'failed', 'info_required'] as $status)
                            <option value="{{ $status }}" @selected($fulfillment === $status)>{{ $statusLabel($status) }}</option>
                        @endforeach
                    </select>
                    <button type="submit">فلترة</button>
                </form>
            </div>

            <div class="ac-kpi-grid">
                <div class="ac-kpi"><span>إجمالي الطلبات</span><strong>{{ number_format($summary['total']) }}</strong></div>
                <div class="ac-kpi" style="--accent:#16a34a"><span>مدفوعة</span><strong>{{ number_format($summary['paid']) }}</strong></div>
                <div class="ac-kpi" style="--accent:#f59e0b"><span>معلقة</span><strong>{{ number_format($summary['pending']) }}</strong></div>
                <div class="ac-kpi" style="--accent:#2563eb"><span>تحتاج تنفيذ</span><strong>{{ number_format($summary['needsFulfillment']) }}</strong></div>
                <div class="ac-kpi" style="--accent:#dc2626"><span>فاشلة</span><strong>{{ number_format($summary['failed']) }}</strong></div>
                <div class="ac-kpi" style="--accent:#7c3aed"><span>إيراد مدفوع</span><strong>{{ $fmt($summary['revenue']) }}</strong></div>
            </div>
            <div class="ac-status-strip">
                <a class="ac-status-chip teal {{ $payment === '' && $fulfillment === '' ? 'active' : '' }}" href="{{ route('admin.orders.index') }}">
                    <span>كل الطلبات</span><strong>{{ number_format($summary['total']) }}</strong>
                </a>
                <a class="ac-status-chip green {{ $payment === 'paid' ? 'active' : '' }}" href="{{ route('admin.orders.index', ['payment_status' => 'paid']) }}">
                    <span>مدفوعة</span><strong>{{ number_format($summary['paid']) }}</strong>
                </a>
                <a class="ac-status-chip amber {{ $payment === 'pending' ? 'active' : '' }}" href="{{ route('admin.orders.index', ['payment_status' => 'pending']) }}">
                    <span>انتظار الدفع</span><strong>{{ number_format($summary['pending']) }}</strong>
                </a>
                <a class="ac-status-chip blue {{ $fulfillment === 'pending' || $fulfillment === 'processing' ? 'active' : '' }}" href="{{ route('admin.orders.index', ['fulfillment_status' => 'pending']) }}">
                    <span>تحتاج تنفيذ</span><strong>{{ number_format($summary['needsFulfillment']) }}</strong>
                </a>
                <a class="ac-status-chip red {{ $payment === 'failed' ? 'active' : '' }}" href="{{ route('admin.orders.index', ['payment_status' => 'failed']) }}">
                    <span>فاشلة</span><strong>{{ number_format($summary['failed']) }}</strong>
                </a>
            </div>
        </div>

        <div class="ac-panel">
            <div class="ac-panel-head"><h2>طابور الطلبات</h2><span class="ac-badge blue">{{ $orders->total() }} نتيجة</span></div>
            <form method="POST" action="{{ route('admin.bulk.orders') }}">
                @csrf
                <div class="ac-bulkbar">
                    <select name="action" required>
                        <option value="">إجراء جماعي</option>
                        <option value="mark_paid">تعليم كمدفوع</option>
                        <option value="complete">تنفيذ الطلب</option>
                        <option value="provider_fulfill">تشغيل المزود تلقائيًا</option>
                    </select>
                    <button class="ac-btn secondary">تنفيذ</button>
                </div>
            <div class="ac-table-wrap">
                <table class="ac-table">
                    <thead><tr><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><input type="checkbox" name="ids[]" value="{{ $order->id }}"></td>
                                <td><a href="{{ route('admin.orders.show', $order) }}">{{ $order->order_number }}</a></td>
                                <td>
                                    <strong>{{ $order->customer_name }}</strong><br>
                                    <span class="ac-muted">{{ $order->customer_email ?: 'لا يوجد بريد' }}</span>
                                </td>
                                <td>{{ $order->items->sum('quantity') }} عنصر</td>
                                <td>{{ $fmt($order->total_minor) }}</td>
                                <td><span class="ac-badge status-{{ $order->payment_status }} {{ $order->payment_status === 'paid' ? 'green' : ($order->payment_status === 'failed' ? 'red' : 'orange') }}">{{ $statusLabel($order->payment_status) }}</span></td>
                                <td><span class="ac-badge status-{{ $order->fulfillment_status }} {{ $order->fulfillment_status === 'completed' ? 'green' : ($order->fulfillment_status === 'failed' ? 'red' : 'blue') }}">{{ $statusLabel($order->fulfillment_status) }}</span></td>
                                <td>{{ $order->created_at->format('Y-m-d H:i') }}</td>
                                <td>
                                    <div class="ac-actions">
                                        <a class="ac-btn secondary info" href="{{ route('admin.orders.show', $order) }}">عرض</a>
                                        <a class="ac-btn secondary purple" href="{{ route('admin.fulfillment.show', $order) }}">تنفيذ</a>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="9"><div class="ac-empty">لا توجد طلبات مطابقة للفلاتر.</div></td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            </form>
            <div style="margin-top:16px">{{ $orders->links() }}</div>
        </div>
    </section>
@endsection
