@extends('layouts.admin', ['title' => 'مخزون الأكواد | RaseedNow'])

@php
    $fmt = fn ($minor) => number_format(($minor ?? 0) / 100, 2).' EGP';
@endphp

@section('content')
    <section class="lc-page-shell">
        <div class="section-heading">
            <div>
                <span>Code Inventory</span>
                <h1>مخزون الأكواد المشفرة</h1>
            </div>
            <div class="flex flex-wrap gap-2">
                <a class="secondary-button" href="{{ route('admin.dashboard') }}">لوحة الإدارة</a>
                <a class="secondary-button" href="{{ route('admin.products.index') }}">المنتجات</a>
                <a class="secondary-button" href="{{ route('admin.exports.stock-codes') }}">تصدير CSV</a>
                <a class="primary-button" href="{{ route('admin.stock-codes.create') }}">استيراد أكواد</a>
            </div>
        </div>

        @if(session('status'))
            <div class="mt-4 rounded border border-green-200 bg-green-50 p-3 text-sm text-green-700">{{ session('status') }}</div>
        @endif
        @if($errors->any())
            <div class="mt-4 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">{{ $errors->first() }}</div>
        @endif

        <div class="mt-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
            <a class="metric-card ac-metric-available" href="{{ route('admin.stock-codes.index', ['status' => 'available']) }}"><span>متاح</span><strong>{{ $metrics['available'] }}</strong></a>
            <a class="metric-card ac-metric-reserved" href="{{ route('admin.stock-codes.index', ['status' => 'reserved']) }}"><span>محجوز</span><strong>{{ $metrics['reserved'] }}</strong></a>
            <a class="metric-card ac-metric-sold" href="{{ route('admin.stock-codes.index', ['status' => 'sold']) }}"><span>مباع</span><strong>{{ $metrics['sold'] }}</strong></a>
            <a class="metric-card ac-metric-voided" href="{{ route('admin.stock-codes.index', ['status' => 'voided']) }}"><span>ملغي</span><strong>{{ $metrics['voided'] }}</strong></a>
            <div class="metric-card ac-metric-warning"><span>ينتهي قريبًا</span><strong>{{ $metrics['expiringSoon'] }}</strong></div>
            <div class="metric-card ac-metric-info"><span>عدد الباتشات</span><strong>{{ $metrics['batches'] }}</strong></div>
            <div class="metric-card ac-metric-purple"><span>مربوط بمزود</span><strong>{{ $metrics['providerLinked'] }}</strong></div>
            <div class="metric-card ac-metric-money"><span>تكلفة المتاح</span><strong>{{ $fmt($metrics['availableCost']) }}</strong></div>
        </div>

        <form class="surface mt-6 grid gap-3 p-4 md:grid-cols-[1fr_220px_160px_auto]" method="GET">
            <input class="h-11 rounded border border-[#dfe2ea] px-4" name="q" value="{{ $query }}" placeholder="بحث بالدفعة أو Serial أو SKU">
            <select class="h-11 rounded border border-[#dfe2ea] px-4" name="product">
                <option value="0">كل المنتجات</option>
                @foreach($products as $product)
                    <option value="{{ $product->id }}" @selected($productId === $product->id)>{{ $product->name_ar }} / {{ $product->sku }}</option>
                @endforeach
            </select>
            <select class="h-11 rounded border border-[#dfe2ea] px-4" name="status">
                @foreach(['available' => 'متاح', 'reserved' => 'محجوز', 'sold' => 'مباع', 'voided' => 'ملغي', 'all' => 'الكل'] as $value => $label)
                    <option value="{{ $value }}" @selected($status === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <button class="secondary-button" type="submit">تصفية</button>
        </form>

        <div class="ac-grid-2 ac-stock-layout">
            <div class="surface mt-6 overflow-hidden ac-stock-panel">
                <div class="ac-panel-head"><h2>الأكواد</h2><span class="ac-badge blue">{{ $codes->total() }} نتيجة</span></div>
                @if($codes->count())
                    <div class="ac-stock-groups">
                        @foreach($codes->getCollection()->groupBy(fn ($code) => $code->productVariant?->game?->name_ar ?: $code->productVariant?->name_ar ?: 'بدون منتج') as $groupName => $groupCodes)
                            @php($firstProduct = $groupCodes->first()?->productVariant)
                            <section class="ac-stock-group">
                                <header class="ac-stock-group-head">
                                    <div>
                                        <h3>{{ $groupName }}</h3>
                                        <p>{{ $groupCodes->count() }} كود ظاهر في الصفحة</p>
                                    </div>
                                    <span class="ac-badge method-{{ $firstProduct?->delivery_method }}">{{ $firstProduct?->delivery_method ?: 'stock' }}</span>
                                </header>
                                <div class="ac-stock-code-list">
                                    @foreach($groupCodes as $code)
                                        <div class="ac-stock-code-card">
                                            <div>
                                                <strong>{{ $code->productVariant->name_ar }}</strong>
                                                <small>{{ $code->productVariant->sku }} / {{ $code->batch_reference ?? 'بدون دفعة' }}</small>
                                            </div>
                                            <code>{{ $code->maskedCode() }}</code>
                                            <span class="ac-badge status-{{ $code->status }}">{{ $code->status }}</span>
                                            <div class="ac-actions">
                                                <a class="secondary-button info" href="{{ route('admin.stock-codes.show', $code) }}">تفاصيل</a>
                                                @if($code->status === 'available')
                                                    <form method="POST" action="{{ route('admin.stock-codes.destroy', $code) }}">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button class="secondary-button danger" type="submit">إلغاء</button>
                                                    </form>
                                                @endif
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            </section>
                        @endforeach
                    </div>
                @else
                    <div class="ac-empty">لا توجد أكواد مطابقة للتصفية الحالية.</div>
                @endif
            </div>

            <div class="surface mt-6 ac-stock-panel">
                <div class="ac-panel-head"><h2>ملخص الباتشات</h2></div>
                <div class="ac-list">
                    @forelse($batchSummary as $batch)
                        <div class="ac-batch-card">
                            <div><strong>{{ $batch->batch }}</strong><span>{{ $fmt($batch->cost_minor) }}</span></div>
                            <span class="ac-badge status-{{ $batch->status }}">{{ $batch->status }}</span>
                            <b>{{ $batch->total }}</b>
                        </div>
                    @empty
                        <div class="ac-empty">لا توجد باتشات بعد.</div>
                    @endforelse
                </div>
            </div>
        </div>

        <div class="mt-4">{{ $codes->links() }}</div>
    </section>
@endsection
