@extends('layouts.admin', ['title' => 'المنتجات والمخزون'])

@php $fmt = fn ($minor) => number_format(($minor ?? 0) / 100, 2).' EGP'; @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" href="{{ route('admin.products.create') }}">منتج جديد</a><a class="ac-btn secondary" href="{{ route('admin.stock-codes.index') }}">أكواد المخزون</a></div>
            </div>
            <div class="ac-kpi-grid">
                <div class="ac-kpi"><span>إجمالي المنتجات</span><strong>{{ $summary['total'] }}</strong></div>
                <div class="ac-kpi" style="--accent:#16a34a"><span>مفعلة</span><strong>{{ $summary['active'] }}</strong></div>
                <div class="ac-kpi" style="--accent:#64748b"><span>معطلة</span><strong>{{ $summary['inactive'] }}</strong></div>
                <div class="ac-kpi" style="--accent:#dc2626"><span>مخزون منخفض</span><strong>{{ $summary['lowStock'] }}</strong></div>
                <div class="ac-kpi" style="--accent:#7c3aed"><span>منتجات أكواد</span><strong>{{ $summary['storedCodes'] }}</strong></div>
                <div class="ac-kpi" style="--accent:#f59e0b"><span>متوسط الهامش</span><strong>{{ number_format($summary['avgMargin'], 1) }}%</strong></div>
            </div>
            <div class="ac-status-strip">
                <a class="ac-status-chip teal {{ $status === '' && $delivery === '' && $stock === '' ? 'active' : '' }}" href="{{ route('admin.products.index') }}"><span>كل المنتجات</span><strong>{{ $summary['total'] }}</strong></a>
                <a class="ac-status-chip green {{ $status === 'active' ? 'active' : '' }}" href="{{ route('admin.products.index', ['status' => 'active']) }}"><span>مفعلة</span><strong>{{ $summary['active'] }}</strong></a>
                <a class="ac-status-chip slate {{ $status === 'inactive' ? 'active' : '' }}" href="{{ route('admin.products.index', ['status' => 'inactive']) }}"><span>معطلة</span><strong>{{ $summary['inactive'] }}</strong></a>
                <a class="ac-status-chip red {{ $stock === 'low' ? 'active' : '' }}" href="{{ route('admin.products.index', ['stock' => 'low']) }}"><span>مخزون منخفض</span><strong>{{ $summary['lowStock'] }}</strong></a>
                <a class="ac-status-chip purple {{ $delivery === 'stored_code' ? 'active' : '' }}" href="{{ route('admin.products.index', ['delivery_method' => 'stored_code']) }}"><span>أكواد</span><strong>{{ $summary['storedCodes'] }}</strong></a>
            </div>
        </div>

        <div class="ac-panel">
            <div class="ac-panel-head">
                <h2>جدول الكتالوج</h2>
                <form method="GET" class="ac-filter">
                    <input name="q" value="{{ $query }}" placeholder="الاسم، SKU، اللعبة...">
                    <select name="status">
                        <option value="">كل الحالات</option>
                        <option value="active" @selected($status === 'active')>مفعل</option>
                        <option value="inactive" @selected($status === 'inactive')>معطل</option>
                    </select>
                    <select name="delivery_method">
                        <option value="">كل طرق التسليم</option>
                        @foreach(['manual', 'api', 'stored_code', 'hybrid'] as $method)
                            <option value="{{ $method }}" @selected($delivery === $method)>{{ $method }}</option>
                        @endforeach
                    </select>
                    <button>فلترة</button>
                </form>
            </div>
            <form method="POST" action="{{ route('admin.bulk.products') }}">
                @csrf
                <div class="ac-bulkbar">
                    <select name="action" required>
                        <option value="">إجراء جماعي</option>
                        <option value="activate">تفعيل</option>
                        <option value="deactivate">تعطيل</option>
                        <option value="feature">تمييز</option>
                        <option value="unfeature">إلغاء التمييز</option>
                        <option value="best_seller">الأكثر مبيعًا</option>
                        <option value="not_best_seller">إلغاء الأكثر مبيعًا</option>
                    </select>
                    <button class="ac-btn secondary">تنفيذ</button>
                </div>
            @if($products->count())
                <div class="ac-catalog-groups">
                    @foreach($products->getCollection()->groupBy(fn ($product) => $product->game?->name_ar ?: 'بدون لعبة') as $gameName => $groupProducts)
                        @php
                            $firstGame = $groupProducts->first()?->game;
                            $groupLowStock = $groupProducts->filter(fn ($product) => $product->stock_available <= $product->min_stock_alert)->count();
                            $groupActive = $groupProducts->where('status', 'active')->count();
                        @endphp
                        <section class="ac-catalog-group">
                            <header class="ac-catalog-group-head">
                                <div class="ac-catalog-title">
                                    @if($firstGame?->image_path)
                                        <img src="{{ asset($firstGame->image_path) }}" alt="">
                                    @else
                                        <span style="background: {{ $firstGame?->cover_color ?: '#0f9f98' }}"></span>
                                    @endif
                                    <div>
                                        <h3>{{ $gameName }}</h3>
                                        <p>{{ $groupProducts->count() }} منتج</p>
                                    </div>
                                </div>
                                <div class="ac-catalog-stats">
                                    <span class="ac-badge green">{{ $groupActive }} مفعل</span>
                                    <span class="ac-badge {{ $groupLowStock ? 'red' : 'green' }}">{{ $groupLowStock }} منخفض</span>
                                </div>
                            </header>
                            <div class="ac-table-wrap">
                                <table class="ac-table">
                                    <thead><tr><th></th><th>المنتج</th><th>SKU</th><th>السعر</th><th>التكلفة</th><th>الهامش</th><th>المخزون</th><th>التسليم</th><th>الحالة</th><th>إجراءات</th></tr></thead>
                                    <tbody>
                                        @foreach($groupProducts as $product)
                                            @php($margin = $product->price_minor > 0 ? (($product->price_minor - $product->cost_minor) / $product->price_minor) * 100 : 0)
                                            <tr>
                                                <td><input type="checkbox" name="ids[]" value="{{ $product->id }}"></td>
                                                <td><strong>{{ $product->name_ar }}</strong><br><span class="ac-muted">{{ $product->name_en }}</span></td>
                                                <td>{{ $product->sku }}</td>
                                                <td>{{ $fmt($product->sale_price_minor ?: $product->price_minor) }}</td>
                                                <td>{{ $fmt($product->cost_minor) }}</td>
                                                <td><span class="ac-badge {{ $margin >= 25 ? 'green' : ($margin >= 10 ? 'orange' : 'red') }}">{{ number_format($margin, 1) }}%</span></td>
                                                <td><span class="ac-badge {{ $product->stock_available <= $product->min_stock_alert ? 'red' : 'green' }}">{{ $product->stock_available }}</span></td>
                                                <td><span class="ac-badge method-{{ $product->delivery_method }}">{{ $product->delivery_method }}</span></td>
                                                <td><span class="ac-badge {{ $product->status === 'active' ? 'green' : 'red' }}">{{ $product->status === 'active' ? 'مفعل' : 'معطل' }}</span></td>
                                                <td>
                                                    <div class="ac-actions">
                                                        <a class="ac-btn secondary purple" href="{{ route('admin.products.show', $product) }}">360</a>
                                                        <a class="ac-btn secondary info" href="{{ route('admin.products.edit', $product) }}">تعديل</a>
                                                    </div>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </section>
                    @endforeach
                </div>
            @else
                <div class="ac-empty">لا توجد منتجات مطابقة للفلاتر.</div>
            @endif
            </form>
            <div style="margin-top:16px">{{ $products->links() }}</div>
        </div>
    </section>
@endsection
