@extends('admin.layouts.app')
@section('title', 'Edit Product')

@section('content')
<div class="form-container bg-white p-4 shadow-sm rounded">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="h4">Edit Product</h2>
        <a href="{{ route('admin.product') }}" class="btn btn-secondary">Back to Products</a>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    
   <form action="{{ route('admin.product.update', $product->id) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')
        <div class="row">
            <div class="col-md-6">
                <div class="form-group mb-3">
                    <label for="name">Product Name</label>
                    <input type="text" id="name" name="name" placeholder="Product Name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name', $product->name) }}">
                    @error('name')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group mb-3">
                    <label for="price">Price</label>
                    <input type="number" id="price" name="price" placeholder="Product Price" step="0.01" class="form-control @error('price') is-invalid @enderror" value="{{ old('price', $product->price) }}">
                    @error('price')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-6">
                <div class="form-group mb-3">
                    <label for="stock">Stock</label>
                    <input type="number" id="stock" name="stock" placeholder="Product Stock" class="form-control @error('stock') is-invalid @enderror" value="{{ old('stock', $product->stock) }}">
                    @error('stock')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group mb-3">
                    <label for="category">Category</label>
                    <select id="category" name="category" class="form-control @error('category') is-invalid @enderror">
                        <option value="">Select a category</option>
                        @foreach($categories as $category)
                            <option value="{{ $category->id }}" {{ old('category', $product->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
                        @endforeach
                    </select>
                    @error('category')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
            </div>
        </div>

        <div class="form-group mb-3">
            <label for="description">Product Description</label>
            <textarea id="description" name="description" rows="5" placeholder="Product Description" class="form-control @error('description') is-invalid @enderror">{{ old('description', $product->description) }}</textarea>
            @error('description')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
        
        <div class="form-group mb-3">
            <label for="image_url">Product Image</label>
            <input type="file" id="image_url" name="image_url" class="dropify form-control @error('image_url') is-invalid @enderror">
            @if($product->image)
                <small class="form-text text-muted">Current image:</small><br>
                <img src="{{ asset($product->image) }}" alt="Current Product Image" class="mt-2 rounded" style="width: 150px; height: 150px; object-fit: cover;">
            @endif
            @error('image_url')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>

        <button type="submit" class="btn btn-primary float-end"><i class="fas fa-save me-2"></i>Update Product</button>
    </form>
</div>
@endsection
