@extends('layouts.franchise', ['heading' => 'Stock / स्टॉक'])

@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

    <div class="mb-6 grid grid-cols-1 gap-3 md:grid-cols-3">
        <form method="POST" action="{{ route('franchise.stock.transfer') }}" class="rounded-2xl bg-white p-4 shadow">
            @csrf
            <div class="mb-2 font-semibold">Transfer / Return Stock</div>
            <select name="product_id" class="mb-2 w-full rounded border p-2 text-sm">
                @foreach($products as $product)
                    <option value="{{ $product->id }}">{{ $product->name }} ({{ $product->sku }})</option>
                @endforeach
            </select>
            <input name="quantity" type="number" inputmode="decimal" step="0.01" min="0.01" placeholder="Quantity" class="mb-2 w-full rounded border p-2 text-sm" required>
            <input name="target_franchise_code" placeholder="Target Franchise Code (optional)" class="mb-2 w-full rounded border p-2 text-sm">
            <button class="w-full rounded bg-slate-900 px-4 py-2 text-sm text-white">Submit</button>
            <div class="mt-2 text-xs text-slate-500">Leave target blank to return stock to admin.</div>
        </form>

        <form method="POST" action="{{ route('franchise.stock.damage') }}" class="rounded-2xl bg-white p-4 shadow">
            @csrf
            <div class="mb-2 font-semibold">Damage Entry</div>
            <select name="product_id" class="mb-2 w-full rounded border p-2 text-sm">
                @foreach($products as $product)
                    <option value="{{ $product->id }}">{{ $product->name }} ({{ $product->sku }})</option>
                @endforeach
            </select>
            <input name="quantity" type="number" inputmode="decimal" step="0.01" min="0.01" placeholder="Quantity" class="mb-2 w-full rounded border p-2 text-sm" required>
            <input name="remark" placeholder="Remark" class="mb-2 w-full rounded border p-2 text-sm">
            <button class="w-full rounded bg-rose-600 px-4 py-2 text-sm text-white">Record Damage</button>
        </form>

        <div class="rounded-2xl bg-white p-4 shadow">
            <div class="mb-2 font-semibold">Stock Requests</div>
            <div class="text-sm text-slate-500">Create new requests from stock request page.</div>
            <a href="{{ route('franchise.stock-requests') }}" class="mt-3 inline-block rounded bg-emerald-600 px-4 py-2 text-sm text-white">Manage Requests</a>
        </div>
    </div>

    <div class="mb-6 grid grid-cols-1 gap-3 md:grid-cols-2">
        <form method="POST" action="{{ route('franchise.stock.bv-transfer') }}" class="rounded-2xl bg-white p-4 shadow">
            @csrf
            <div class="mb-2 font-semibold">Transfer BV to User</div>
            <input name="member_code" placeholder="Member Code / Mobile" class="mb-2 w-full rounded border p-2 text-sm" required>
            <input name="bv_units" type="number" inputmode="decimal" step="0.01" min="0.01" placeholder="BV Units" class="mb-2 w-full rounded border p-2 text-sm" required>
            <input name="remark" placeholder="Remark" class="mb-2 w-full rounded border p-2 text-sm">
            <button class="w-full rounded bg-indigo-600 px-4 py-2 text-sm text-white">Transfer BV</button>
        </form>
    </div>

    <div class="grid grid-cols-1 gap-3">
        @forelse($stocks as $stock)
            <div class="rounded-2xl bg-white p-4 shadow">
                <div class="flex items-center justify-between">
                    <div class="font-semibold">{{ $stock->name }}</div>
                    <div class="text-xs text-slate-500">SKU: {{ $stock->sku }}</div>
                </div>
                <div class="mt-2 text-sm">Qty: <span class="font-semibold">{{ number_format((float) $stock->quantity, 2) }}</span></div>
                <div class="text-xs text-slate-500">Reserved: {{ number_format((float) $stock->reserved_quantity, 2) }}</div>
                <div class="text-xs text-slate-500">DP: ₹ {{ number_format((float) $stock->dp_price, 2) }} | BV: {{ number_format((float) $stock->bv, 2) }}</div>
            </div>
        @empty
            <div class="rounded-2xl bg-white p-4 text-center text-slate-500">No stock found.</div>
        @endforelse
    </div>

    <div class="mt-6 grid grid-cols-1 gap-3">
        <div class="font-semibold">Stock Ledger</div>
        @forelse($transactions as $txn)
            <div class="rounded-2xl bg-white p-4 shadow">
                <div class="flex items-center justify-between">
                    <div class="font-semibold">{{ $txn->transaction_type }}</div>
                    <div class="text-xs text-slate-500">{{ \Illuminate\Support\Carbon::parse($txn->transacted_at)->format('d M Y, h:i A') }}</div>
                </div>
                <div class="mt-1 text-sm text-slate-600">Qty: {{ number_format((float) ($txn->quantity ?? 0), 2) }}</div>
                <div class="text-xs text-slate-500">Ref: {{ $txn->reference }}</div>
            </div>
        @empty
            <div class="rounded-2xl bg-white p-4 text-center text-slate-500">No stock transactions found.</div>
        @endforelse
    </div>

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