@include('includes.header')

<div class="page-wrapper">
    <div class="page-content">

        <!-- Page Header + Controls -->
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body d-flex justify-content-between align-items-center">

                <div>
                    <h5 class=" mb-0 text-uppercase">Edit Product</h5>
                    <small class="text-muted">Create a new Product</small>
                </div>

                <a href="{{ route('store.product.index') }}" class="btn btn-outline-secondary btn-sm">
                    <i class="bx bx-arrow-back"></i> Back
                </a>

            </div>


            <!-- Form Card -->
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">

                    <form action="{{ route('store.product.update', $product->id) }}" method="POST"
                        enctype="multipart/form-data">
                        @csrf
                        @method('PUT')

                        <div class="row g-3">

                            <!-- Category -->
                            <div class="col-md-6">
                                <label class="form-label">Category</label>
                                <select name="category_id"
                                    class="form-control @error('category_id') is-invalid @enderror">
                                    <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>
                                @error('category_id')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Product Name -->
                            <div class="col-md-6">
                                <label class="form-label">Product Name</label>
                                <input type="text" name="name" value="{{ old('name', $product->name) }}"
                                    placeholder="e.g. Silver Necklace with Floral Design"
                                    class="form-control @error('name') is-invalid @enderror">
                                @error('name')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Main Image -->
                            <div class="col-md-6">
                                <label class="form-label">Main Image</label>
                                <input type="file" name="image"
                                    class="form-control @error('image') is-invalid @enderror">

                                @if ($product->image)
                                    <div class="mt-2">
                                        <img src="{{ asset('uploads/products/' . $product->image) }}" width="80">
                                    </div>
                                @endif

                                @error('image')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Gallery -->
                            <div class="col-md-6">
                                <label class="form-label">Gallery (Max 5 Images)</label>
                                <input type="file" name="gallery[]" multiple
                                    class="form-control @error('gallery') is-invalid @enderror @error('gallery.*') is-invalid @enderror">

                                <!-- Existing Gallery -->
                                @if ($product->gallery)
                                    <div class="mt-2">
                                        @foreach (json_decode($product->gallery) as $img)
                                            <img src="{{ asset('uploads/products/gallery/' . $img) }}" width="60"
                                                class="me-1 mb-1">
                                        @endforeach
                                    </div>
                                @endif

                                @error('gallery')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror

                                @error('gallery.*')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Purity -->
                            <div class="col-md-6">
                                <label class="form-label">Purity</label>
                                <input type="text" name="purity" value="{{ old('purity', $product->purity) }}"
                                    placeholder="e.g. 92.5 Sterling Silver"
                                    class="form-control @error('purity') is-invalid @enderror">
                                @error('purity')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Weight -->
                            <div class="col-md-6">
                                <label class="form-label">Weight</label>
                                <input type="text" name="weight" value="{{ old('weight', $product->weight) }}"
                                    placeholder="e.g. 25 grams"
                                    class="form-control @error('weight') is-invalid @enderror">
                                @error('weight')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Making Charge + Price + Stock -->
                            <div class="col-md-4">
                                <label class="form-label">Making Charge</label>
                                <input type="number" name="making_charge"
                                    value="{{ old('making_charge', $product->making_charge) }}" placeholder="e.g. 500"
                                    class="form-control @error('making_charge') is-invalid @enderror">
                                @error('making_charge')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <div class="col-md-4">
                                <label class="form-label">Price</label>
                                <input type="number" name="price" value="{{ old('price', $product->price) }}"
                                    placeholder="e.g. 3500" class="form-control @error('price') is-invalid @enderror">
                                @error('price')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <div class="col-md-4">
                                <label class="form-label">Stock</label>
                                <input type="number" name="stock" value="{{ old('stock', $product->stock) }}"
                                    placeholder="e.g. 10" class="form-control @error('stock') is-invalid @enderror">
                                @error('stock')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                            <!-- Description -->
                            <div class="col-md-12">
                                <label class="form-label">Description</label>
                                <textarea name="description" class="form-control @error('description') is-invalid @enderror"
                                    placeholder="Enter product description">{{ old('description', $product->description) }}</textarea>
                                @error('description')
                                    <small class="text-danger">{{ $message }}</small>
                                @enderror
                            </div>

                        </div>

                        <!-- Buttons -->
                        <div class="d-flex justify-content-end gap-2 mt-4">
                            <a href="{{ route('store.product.index') }}" class="btn btn-light px-4">Cancel</a>
                            <button type="submit" class="btn btn-success px-4">
                                <i class="bx bx-save"></i> Update
                            </button>
                        </div>

                    </form>

                </div>
            </div>
        </div>
    </div>
</div>

@include('includes.footer')
