@extends('layouts.franchise', ['heading' => 'Stock Requests / रिक्वेस्ट'])

@section('content')
    @if(session('status'))
        <div class="mb-4 rounded bg-emerald-100 p-3 text-emerald-800">{{ session('status') }}</div>
    @endif
    @if($errors->any())
        <div class="mb-4 rounded bg-rose-100 p-3 text-rose-800">
            <ul class="list-disc pl-5 text-sm">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form method="POST" action="{{ route('franchise.stock-requests.store') }}" class="rounded-2xl bg-white p-4 shadow">
        @csrf
        <div class="mb-3 font-semibold">Create Stock Request</div>
        <div class="grid grid-cols-1 gap-3 md:grid-cols-2">
            <div class="md:col-span-2">
                <label class="mb-1 block text-xs text-slate-500">Preferred Warehouse</label>
                <select name="requested_warehouse_id" class="w-full rounded border p-2 text-sm">
                    <option value="">Auto (Admin Dispatch)</option>
                    @foreach($warehouses as $warehouse)
                        <option value="{{ $warehouse->id }}">{{ $warehouse->name }} ({{ $warehouse->code }})</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="mb-1 block text-xs text-slate-500">Product</label>
                <select name="product_id[]" class="w-full rounded border p-2 text-sm">
                    @foreach($products as $product)
                        <option value="{{ $product->id }}">{{ $product->name }} ({{ $product->sku }})</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="mb-1 block text-xs text-slate-500">Quantity</label>
                <input name="quantity[]" type="number" inputmode="decimal" step="0.01" min="0.01" class="w-full rounded border p-2 text-sm" />
            </div>
            <div class="md:col-span-2">
                <textarea name="notes" placeholder="Notes (optional)" class="w-full rounded border p-2 text-sm"></textarea>
            </div>
        </div>
        <button class="mt-3 rounded bg-emerald-600 px-4 py-2 text-sm text-white">Submit Request</button>
    </form>

    <div class="mt-6 grid grid-cols-1 gap-3">
        @forelse($requests as $requestRow)
            <div class="rounded-2xl bg-white p-4 shadow">
                <div class="flex items-center justify-between">
                    <div class="font-semibold">{{ $requestRow->request_number }}</div>
                    <div class="text-xs text-slate-500">{{ $requestRow->status }}</div>
                </div>
                <div class="mt-1 text-xs text-slate-500">
                    Warehouse: {{ $requestRow->requestedWarehouse?->name ?? 'Auto / Admin' }}
                </div>
                <div class="mt-2 text-xs text-slate-500">Requested: {{ optional($requestRow->requested_at)->format('d M Y') }}</div>
                <ul class="mt-2 text-sm text-slate-700">
                    @foreach($requestRow->items as $item)
                        <li>{{ $item->product?->name }} - {{ number_format((float) $item->quantity_requested, 2) }}</li>
                    @endforeach
                </ul>
                @if($requestRow->status !== 'delivered')
                    <form method="POST" action="{{ route('franchise.stock-requests.delivered', $requestRow->id) }}" class="mt-3">
                        @csrf
                        <button class="rounded bg-slate-900 px-3 py-1 text-xs text-white">Mark Delivered</button>
                    </form>
                @endif
            </div>
        @empty
            <div class="rounded-2xl bg-white p-4 text-center text-slate-500">No stock requests.</div>
        @endforelse
    </div>

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