@extends('layouts.admin')

@section('title', 'Edit Product')

@section('admin-content')
<div class="container-fluid px-0">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0 text-gray-800">Edit Product</h1>
        <a href="{{ route('admin.products') }}" class="btn btn-secondary btn-sm">
            <i class="bi bi-arrow-left me-1"></i> Back to Products
        </a>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <form action="{{ route('admin.products.update', $product->id) }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')

                <div class="row g-3">
                    <!-- Basic Information -->
                    <div class="col-md-12">
                        <h5 class="card-title mb-3">Basic Information</h5>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Product Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name', $product->name) }}" required>
                        @error('name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Category <span class="text-danger">*</span></label>
                        <select name="category_id" class="form-select @error('category_id') is-invalid @enderror" required>
                            <option value="">Select Category</option>
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}" {{ old('category_id', $product->category_id) == $category->id ? 'selected' : '' }}>
                                    {{ $category->name }}
                                </option>
                            @endforeach
                        </select>
                        @error('category_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Price (₹) <span class="text-danger">*</span></label>
                        <input type="number" step="0.01" name="price" class="form-control @error('price') is-invalid @enderror" value="{{ old('price', $product->price) }}" required>
                        @error('price')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">MOQ <span class="text-danger">*</span></label>
                        <input type="number" name="moq" class="form-control @error('moq') is-invalid @enderror" value="{{ old('moq', $product->moq) }}" required>
                        @error('moq')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-12">
                        <label class="form-label">Description <span class="text-danger">*</span></label>
                        <textarea name="description" rows="5" class="form-control @error('description') is-invalid @enderror" required>{{ old('description', $product->description) }}</textarea>
                        @error('description')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <!-- Images -->
                    <div class="col-12 mt-4">
                        <h5 class="card-title mb-3">Product Images</h5>
                        
                        <div class="mb-3">
                            <label class="form-label">Add New Images</label>
                            <input type="file" name="images[]" class="form-control" multiple accept="image/*">
                            <div class="form-text">You can select multiple images. Max 2MB each.</div>
                        </div>

                        @php
                            $images = $product->images_json;
                            if (is_string($images)) {
                                $decoded = json_decode($images, true);
                                $images = (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) ? $decoded : [];
                            }
                            if (!is_array($images)) {
                                $images = [];
                            }
                        @endphp

                        @if(count($images) > 0)
                            <div class="row g-2 mt-2">
                                @foreach($images as $index => $image)
                                    <div class="col-6 col-md-3 col-lg-2">
                                        <div class="card h-100 border">
                                            <div class="position-relative">
                                                <img src="{{ asset('storage/' . ($image['thumbnail'] ?? $image['original'] ?? $image)) }}" class="card-img-top" alt="Product Image" style="height: 100px; object-fit: cover;">
                                            </div>
                                            <div class="card-body text-center p-1 bg-light">
                                                <div class="form-check d-inline-block">
                                                    <input class="form-check-input" type="checkbox" name="delete_images[]" value="{{ $index }}" id="delete_img_{{ $index }}">
                                                    <label class="form-check-label text-danger small fw-bold" for="delete_img_{{ $index }}">
                                                        Delete
                                                    </label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @endif
                    </div>

                    <div class="col-12 mt-4">
                        <hr>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save me-1"></i> Update Product
                        </button>
                        <a href="{{ route('admin.products') }}" class="btn btn-outline-secondary">Cancel</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
