<x-admin-layout title="Inventory" subtitle="Pembelian, opname, adjustment, warning stok minimum, dan histori mutasi stok.">
    <div class="grid gap-5 xl:grid-cols-[1fr_420px]">
        <div class="space-y-5">
            <section class="overflow-hidden rounded-lg bg-white shadow-sm">
                <div class="flex items-center justify-between border-b p-5">
                    <div>
                        <h2 class="font-semibold">Stok per Cabang</h2>
                        <p class="text-sm text-slate-500">Produk service tidak mengurangi stok; hanya retail track stock.</p>
                    </div>
                    <form>
                        <select name="branch_id" onchange="this.form.submit()" class="rounded-md border px-3 py-2 text-sm">
                            <option value="">Semua cabang</option>
                            @foreach ($branches as $branch)
                                <option value="{{ $branch->id }}" @selected((int) $selectedBranchId === $branch->id)>{{ $branch->name }}</option>
                            @endforeach
                        </select>
                    </form>
                </div>
                <table class="w-full text-left text-sm">
                    <thead class="bg-slate-100 text-slate-600">
                    <tr><th class="p-3">Produk</th><th>Cabang</th><th>Stok</th><th>Minimum</th><th>Status</th></tr>
                    </thead>
                    <tbody class="divide-y">
                    @forelse ($stocks as $stock)
                        @php $low = (float) $stock->quantity <= (float) $stock->product->minimum_stock; @endphp
                        <tr>
                            <td class="p-3 font-medium">{{ $stock->product->name }}</td>
                            <td>{{ $stock->branch->name }}</td>
                            <td>{{ rtrim(rtrim(number_format((float) $stock->quantity, 3, ',', '.'), '0'), ',') }}</td>
                            <td>{{ rtrim(rtrim(number_format((float) $stock->product->minimum_stock, 3, ',', '.'), '0'), ',') }}</td>
                            <td><span class="rounded-full border px-2 py-0.5 text-xs {{ $low ? 'border-red-200 bg-red-50 text-red-700' : 'border-emerald-200 bg-emerald-50 text-emerald-700' }}">{{ $low ? 'Minimum' : 'Aman' }}</span></td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="p-6 text-center text-slate-500">Belum ada data stok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </section>

            <section class="rounded-lg bg-white p-5 shadow-sm">
                <h2 class="font-semibold">Histori Mutasi Stok</h2>
                <div class="mt-4 divide-y text-sm">
                    @forelse ($mutations as $mutation)
                        <div class="grid gap-2 py-3 md:grid-cols-[1fr_auto]">
                            <div>
                                <div class="font-medium">{{ $mutation->product?->name }} - {{ $mutation->branch?->name }}</div>
                                <div class="text-slate-500">{{ $mutation->source_type }} {{ $mutation->note ? '- '.$mutation->note : '' }}</div>
                            </div>
                            <div class="text-right">
                                <div class="font-semibold {{ (float) $mutation->quantity_change < 0 ? 'text-red-700' : 'text-emerald-700' }}">{{ (float) $mutation->quantity_change > 0 ? '+' : '' }}{{ rtrim(rtrim(number_format((float) $mutation->quantity_change, 3, ',', '.'), '0'), ',') }}</div>
                                <div class="text-xs text-slate-500">Akhir {{ rtrim(rtrim(number_format((float) $mutation->quantity_after, 3, ',', '.'), '0'), ',') }}</div>
                            </div>
                        </div>
                    @empty
                        <div class="py-3 text-slate-500">Belum ada mutasi.</div>
                    @endforelse
                </div>
            </section>
        </div>

        <aside class="space-y-5">
            <form method="post" action="{{ route('inventory.purchases.store') }}" class="rounded-lg bg-white p-5 shadow-sm">
                @csrf
                <h2 class="font-semibold">Pembelian Barang</h2>
                <div class="mt-4 grid gap-3 md:grid-cols-2">
                    <x-master.select name="branch_id" label="Cabang" :value="$selectedBranchId" :options="$branches->pluck('name', 'id')->all()" />
                    <x-master.select name="supplier_id" label="Supplier" :value="old('supplier_id')" :options="$suppliers->pluck('name', 'id')->all()" empty="Tanpa supplier" />
                </div>
                <div class="mt-3 grid gap-3 md:grid-cols-2">
                    <x-master.input name="purchase_date" label="Tanggal" type="date" :value="old('purchase_date', now()->toDateString())" required />
                    <x-master.input name="invoice_no" label="No. invoice supplier" :value="old('invoice_no')" />
                </div>
                <div class="mt-4 space-y-2">
                    @foreach ($products->take(5) as $i => $product)
                        <div class="grid grid-cols-[1fr_80px_110px] gap-2">
                            <input type="hidden" name="items[{{ $i }}][product_id]" value="{{ $product->id }}">
                            <div class="rounded-md border bg-slate-50 px-3 py-2 text-sm">{{ $product->name }}</div>
                            <input name="items[{{ $i }}][quantity]" type="number" step="0.001" min="0" placeholder="Qty" class="rounded-md border px-2 py-2 text-sm">
                            <input name="items[{{ $i }}][unit_cost]" type="number" min="0" value="{{ (int) $product->cost_price }}" class="rounded-md border px-2 py-2 text-sm">
                        </div>
                    @endforeach
                </div>
                <textarea name="note" placeholder="Catatan pembelian" class="mt-3 w-full rounded-md border px-3 py-2 text-sm"></textarea>
                <button class="mt-4 w-full rounded-md bg-slate-900 px-4 py-2 font-medium text-white">Simpan Pembelian</button>
            </form>

            <form method="post" action="{{ route('inventory.opname.store') }}" class="rounded-lg bg-white p-5 shadow-sm">
                @csrf
                <h2 class="font-semibold">Stock Opname</h2>
                <div class="mt-4 grid gap-3 md:grid-cols-2">
                    <x-master.select name="branch_id" label="Cabang" :value="$selectedBranchId" :options="$branches->pluck('name', 'id')->all()" />
                    <x-master.input name="opname_date" label="Tanggal" type="date" :value="now()->toDateString()" required />
                </div>
                <div class="mt-4 space-y-2">
                    @foreach ($products->take(5) as $i => $product)
                        <div class="grid grid-cols-[1fr_120px] gap-2">
                            <input type="hidden" name="items[{{ $i }}][product_id]" value="{{ $product->id }}">
                            <div class="rounded-md border bg-slate-50 px-3 py-2 text-sm">{{ $product->name }}</div>
                            <input name="items[{{ $i }}][actual_quantity]" type="number" step="0.001" min="0" placeholder="Fisik" class="rounded-md border px-2 py-2 text-sm">
                        </div>
                    @endforeach
                </div>
                <textarea name="note" placeholder="Catatan opname" class="mt-3 w-full rounded-md border px-3 py-2 text-sm"></textarea>
                <button class="mt-4 w-full rounded-md border px-4 py-2 font-medium">Posting Opname</button>
            </form>

            <form method="post" action="{{ route('inventory.adjustments.store') }}" class="rounded-lg bg-white p-5 shadow-sm">
                @csrf
                <h2 class="font-semibold">Adjustment Cepat</h2>
                <div class="mt-4 grid gap-3">
                    <x-master.select name="branch_id" label="Cabang" :value="$selectedBranchId" :options="$branches->pluck('name', 'id')->all()" />
                    <x-master.select name="product_id" label="Produk" :value="old('product_id')" :options="$products->pluck('name', 'id')->all()" />
                    <x-master.input name="quantity" label="Stok akhir" type="number" :value="old('quantity', 0)" required />
                </div>
                <textarea name="note" placeholder="Alasan adjustment" class="mt-3 w-full rounded-md border px-3 py-2 text-sm"></textarea>
                <button class="mt-4 w-full rounded-md border px-4 py-2 font-medium">Simpan Adjustment</button>
            </form>
        </aside>
    </div>
</x-admin-layout>
