@extends('layouts.admin')

@section('title', 'Restock Products')

@section('content')
<div class="admin-page-header">
    <h1>Restock Products</h1>
    <a href="{{ route('admin.products.index') }}" class="admin-btn-primary" style="background: #6c757d;">
        <i class="bi bi-arrow-left"></i> Back
    </a>
</div>

<div class="admin-card">
    <div class="card-body">
        <h3 class="mb-4" style="color: var(--admin-primary);">
            <i class="bi bi-box-arrow-in-down"></i> Add Stock to Product
        </h3>
        
        <form action="{{ route('admin.products.restock.store') }}" method="POST">
            @csrf
            
            <div class="row restock-form">
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Product *</label>
                    <select name="product_id"
                            class="admin-form-control form-select product-select @error('product_id') is-invalid @enderror"
                            data-placeholder="Search by name or barcode"
                            required>
                        <option value="">Select a product</option>
                        @foreach($products as $product)
                            @php
                                $stock = $product->calculated_current_stock ?? $product->current_stock ?? 0;
                                $image = $product->image
                                    ? ($product->image['small'] ?? $product->image['thumb'] ?? $product->image['original'])
                                    : ($product->image_path ? asset('storage/' . $product->image_path) : 'https://via.placeholder.com/60?text=No+Image');
                            @endphp
                            <option value="{{ $product->id }}"
                                    data-name="{{ $product->name }}"
                                    data-barcode="{{ $product->barcode }}"
                                    data-stock="{{ $stock }}"
                                    data-image="{{ $image }}"
                                    {{ old('product_id', request('product_id')) == $product->id ? 'selected' : '' }}>
                                {{ $product->name }} - {{ $product->barcode ?? 'No barcode' }} (Stock: {{ $stock }})
                            </option>
                        @endforeach
                    </select>
                    @error('product_id')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <small class="form-text text-muted">Type a product name or barcode to search</small>
                </div>
                
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Quantity *</label>
                    <input type="number" 
                           name="quantity" 
                           class="admin-form-control form-control @error('quantity') is-invalid @enderror" 
                           value="{{ old('quantity') }}" 
                           required 
                           min="1"
                           placeholder="Enter quantity to add">
                    @error('quantity')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <small class="form-text text-muted">Enter the quantity to add to stock</small>
                </div>
                
                <div class="col-12 mb-3">
                    <label class="admin-form-label">Notes (Optional)</label>
                    <textarea name="notes" 
                              class="admin-form-control form-control @error('notes') is-invalid @enderror" 
                              rows="3"
                              placeholder="e.g., New shipment arrived, Returned items, etc.">{{ old('notes') }}</textarea>
                    @error('notes')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <small class="form-text text-muted">Add any notes about this restock (e.g., "New shipment arrived", "Returned items")</small>
                </div>
                
                <div class="col-12">
                    <button type="submit" class="admin-btn-primary">
                        <i class="bi bi-plus-circle"></i> Restock Product
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@endsection

