@extends('admin.layout')

@section('title','Admin - Produk')

@section('content')
    <div class="card">
        <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px">
            <h1 style="margin:0">Produk</h1>
            <div style="display:flex;gap:8px;flex-wrap:wrap">
                <a href="{{ route('admin.products.create') }}" class="btn">+ Tambah Produk</a>
                <a href="{{ route('inventory.index') }}" class="btn ghost">Lihat toko</a>
            </div>
        </div>

        @if(session('status'))<div class="notice">{{ session('status') }}</div>@endif

        @if($lowStockProducts->count() > 0)
            <div class="alert alert-warning">
                <strong>⚠ Peringatan Stok Rendah:</strong> {{ $lowStockProducts->count() }} produk di bawah stok minimum.
                <a href="{{ route('admin.products.index', ['status' => 'low']) }}" style="margin-left:8px;color:#92400e">Lihat detail →</a>
            </div>
            <div class="low-stock-list">
                @foreach($lowStockProducts->take(5) as $lp)
                    <span class="badge badge-low">{{ $lp->name }} ({{ $lp->total_stock }}/{{ $lp->min_stock }})</span>
                @endforeach
                @if($lowStockProducts->count() > 5)
                    <span class="badge badge-low">+{{ $lowStockProducts->count() - 5 }} lainnya</span>
                @endif
            </div>
        @endif

        <form method="get" class="search-box">
            <input type="text" name="q" placeholder="Cari nama produk..." value="{{ request('q') }}">
            <select name="status" style="width:auto">
                <option value="">Semua Status</option>
                <option value="low" @selected(request('status') === 'low')>Stok Rendah</option>
                <option value="over" @selected(request('status') === 'over')>Stok Berlebih</option>
            </select>
            <button class="btn" type="submit">Cari</button>
        </form>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nama</th>
                    <th>Kategori</th>
                    <th>Stok</th>
                    <th>Min</th>
                    <th>Max</th>
                    <th>Reorder Qty</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
            @foreach($products as $p)
                <tr>
                    <td>{{ $p->id }}</td>
                    <td>{{ $p->name }}</td>
                    <td>{{ $p->category->name ?? '-' }}</td>
                    <td>{{ $p->total_stock }}</td>
                    <td>{{ $p->min_stock ?? '-' }}</td>
                    <td>{{ $p->max_stock ?? '-' }}</td>
                    <td>{{ $p->isLowStock() ? $p->reorder_quantity : '-' }}</td>
                    <td>
                        @if($p->stock_status === 'low')
                            <span class="badge badge-low">{{ $p->stock_status_label }}</span>
                        @elseif($p->stock_status === 'over')
                            <span class="badge badge-over">{{ $p->stock_status_label }}</span>
                        @else
                            <span class="badge badge-ok">{{ $p->stock_status_label }}</span>
                        @endif
                    </td>
                    <td>
                        <a class="btn ghost" href="{{ route('admin.products.edit', $p->id) }}">Edit</a>
                        <form method="post" action="{{ route('admin.products.delete', $p->id) }}" style="display:inline">@csrf<button class="btn" onclick="return confirm('Hapus produk?')" type="submit">Hapus</button></form>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>

        <div style="margin-top:12px">{{ $products->links() }}</div>
    </div>
@endsection
