@extends('layouts.app')
@section('title', isset($product) ? 'Edit Product' : 'Add Product')
@section('content')
<h4 class="mb-3">{{ isset($product) ? 'Edit Product' : 'Add Product' }}</h4>
<div class="card">
    <div class="card-body">
        <form action="{{ isset($product) ? route('products.update', $product) : route('products.store') }}" method="POST">
            @csrf
            @if(isset($product)) @method('PUT') @endif
            <div class="row g-3">
                <div class="col-md-3">
                    <label for="category_id" class="form-label">Category</label>
                    <select name="category_id" id="category_id" class="form-select" required>
                        <option value="">Select Category</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ old('category_id', $product->category_id ?? '') == $cat->id ? 'selected' : '' }}>{{ $cat->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="weight_id" class="form-label">Weight</label>
                    <select name="weight_id" id="weight_id" class="form-select" required>
                        <option value="">Select Weight</option>
                        @foreach($weights as $w)
                            <option value="{{ $w->id }}" {{ old('weight_id', $product->weight_id ?? '') == $w->id ? 'selected' : '' }}>{{ $w->label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="price" class="form-label">Price per Bag (Rs.)</label>
                    <input type="number" step="0.01" name="price" id="price" class="form-control" value="{{ old('price', $product->price ?? '') }}" required>
                </div>
                <div class="col-md-3">
                    <label for="stock" class="form-label">Stock (Bags)</label>
                    <input type="number" step="1" name="stock" id="stock" class="form-control" value="{{ old('stock', $product->stock ?? 0) }}">
                </div>
            </div>
            <div class="mt-3">
                <button type="submit" class="btn btn-primary">{{ isset($product) ? 'Update' : 'Save' }}</button>
                <a href="{{ route('products.index') }}" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
@endsection
