@extends('admin.layouts.app')

@section('title', 'Edit Product')
@section('page-title', 'Edit Product: ' . $product->name)

@section('content')
<div class="space-y-6">
    <!-- Product Form -->
    <div class="bg-white rounded-lg shadow p-6">
        <h3 class="text-lg font-semibold mb-4">Product Details</h3>
        <form method="POST" action="{{ route('admin.products.update', $product) }}" enctype="multipart/form-data">
            @csrf
            @method('PUT')

            @if($errors->any())
                <div class="mb-4 p-4 bg-red-50 border border-red-200 rounded-md">
                    <p class="text-sm font-medium text-red-800">Please fix the following errors:</p>
                    <ul class="mt-2 list-disc list-inside text-sm text-red-700">
                        @foreach($errors->all() as $err)
                            <li>{{ $err }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label for="name" class="block text-sm font-medium text-gray-700">Product Name *</label>
                        <input type="text" name="name" id="name" value="{{ old('name', $product->name) }}" required
                               class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                        @error('name')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                    </div>

                    <div>
                        <label for="sku" class="block text-sm font-medium text-gray-700">SKU</label>
                        <input type="text" name="sku" id="sku" value="{{ old('sku', $product->sku) }}"
                               class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    </div>
                </div>

                <div>
                    <label for="slug" class="block text-sm font-medium text-gray-700">URL Slug</label>
                    <input type="text" name="slug" id="slug" value="{{ old('slug', $product->slug) }}"
                           placeholder="Leave empty to auto-generate from name"
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    @error('slug')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>

                <div>
                    <label for="category_id" class="block text-sm font-medium text-gray-700">Category *</label>
                    <select name="category_id" id="category_id" required
                            class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                        @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="grid grid-cols-3 gap-4">
                    <div>
                        <label for="base_price" class="block text-sm font-medium text-gray-700">Base Price *</label>
                        <input type="number" step="0.01" name="base_price" id="base_price" value="{{ old('base_price', $product->base_price) }}" required
                               class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    </div>

                    <div>
                        <label for="discount_price" class="block text-sm font-medium text-gray-700">Discount Price</label>
                        <input type="number" step="0.01" name="discount_price" id="discount_price" value="{{ old('discount_price', $product->discount_price) }}"
                               class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    </div>

                    <div>
                        <label for="tax_rate" class="block text-sm font-medium text-gray-700">Tax Rate (%)</label>
                        <input type="number" step="0.01" name="tax_rate" id="tax_rate" value="{{ old('tax_rate', $product->tax_rate) }}"
                               class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    </div>
                </div>

                <div class="flex items-center space-x-4">
                    <div class="flex items-center">
                        <input type="checkbox" name="is_active" id="is_active" value="1" {{ old('is_active', $product->is_active) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600">
                        <label for="is_active" class="ml-2 text-sm">Active</label>
                    </div>
                    <div class="flex items-center">
                        <input type="checkbox" name="is_featured" id="is_featured" value="1" {{ old('is_featured', $product->is_featured) ? 'checked' : '' }}
                               class="h-4 w-4 text-blue-600">
                        <label for="is_featured" class="ml-2 text-sm">Featured</label>
                    </div>
                </div>

                <div>
                    <label for="images" class="block text-sm font-medium text-gray-700">Add More Images</label>
                    <input type="file" name="images[]" id="images" multiple accept="image/jpeg,image/jpg,image/png,image/gif,image/webp"
                           class="mt-1 block w-full border border-gray-300 rounded-md py-2 px-3">
                    <p class="mt-1 text-sm text-gray-500">Select one or more images (JPEG, PNG, GIF, WebP, max 5MB each). They will be added to existing images.</p>
                    @error('images.*')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>

                <div>
                    <label for="video" class="block text-sm font-medium text-gray-700">Product Video</label>
                    <input type="file" name="video" id="video" accept="video/mp4,video/quicktime,video/webm"
                           class="mt-1 block w-full border border-gray-300 rounded-md py-2 px-3">
                    <p class="mt-1 text-sm text-gray-500">MP4, MOV or WebM, max 100MB. Replaces existing video if set.</p>
                    @error('video')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>

                <div class="flex justify-end">
                    <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                        Update Product
                    </button>
                </div>
            </div>
        </form>
    </div>

    <!-- Existing Images -->
    @if($product->images->count() > 0)
    <div class="bg-white rounded-lg shadow p-6">
        <h3 class="text-lg font-semibold mb-4">Product Images</h3>
        <div class="grid grid-cols-4 gap-4">
            @foreach($product->images as $image)
            <div class="relative">
                <img src="{{ asset('storage/' . $image->image_path) }}" alt="Product Image" class="w-full h-32 object-cover rounded">
                @if($image->is_primary)
                    <span class="absolute top-2 left-2 bg-blue-500 text-white text-xs px-2 py-1 rounded">Primary</span>
                @endif
                <form action="{{ route('admin.products.images.delete', $image) }}" method="POST" class="mt-2">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="text-red-600 hover:text-red-900 text-sm" onclick="return confirm('Delete this image?');">
                        <i class="fas fa-trash"></i> Delete
                    </button>
                </form>
            </div>
            @endforeach
        </div>
    </div>
    @endif

    <!-- Product Video -->
    @if($product->video_path)
    <div class="bg-white rounded-lg shadow p-6">
        <h3 class="text-lg font-semibold mb-4">Product Video</h3>
        <div class="flex items-center gap-4">
            <video src="{{ asset('storage/' . $product->video_path) }}" controls class="max-w-md rounded border"></video>
            <form action="{{ route('admin.products.video.delete', $product) }}" method="POST" onsubmit="return confirm('Remove this video?');">
                @csrf
                @method('DELETE')
                <button type="submit" class="text-red-600 hover:text-red-900 text-sm">
                    <i class="fas fa-trash"></i> Remove Video
                </button>
            </form>
        </div>
    </div>
    @endif

    <!-- Variants -->
    <div class="bg-white rounded-lg shadow p-6">
        <div class="flex justify-between items-center mb-4">
            <h3 class="text-lg font-semibold">Product Variants</h3>
            <button onclick="document.getElementById('variant-form').classList.toggle('hidden')" 
                    class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded">
                <i class="fas fa-plus mr-2"></i>Add Variant
            </button>
        </div>

        <!-- Add Variant Form -->
        <div id="variant-form" class="hidden mb-4 p-4 bg-gray-50 rounded">
            <form method="POST" action="{{ route('admin.products.variants.store', $product) }}">
                @csrf
                <div class="grid grid-cols-4 gap-4">
                    <input type="text" name="name" placeholder="Variant name (e.g., 1kg)" required class="border rounded px-3 py-2">
                    <input type="text" name="sku" placeholder="SKU" class="border rounded px-3 py-2">
                    <input type="number" step="0.01" name="price" placeholder="Price" required class="border rounded px-3 py-2">
                    <input type="number" name="stock_quantity" placeholder="Stock" required class="border rounded px-3 py-2">
                    <input type="number" name="min_stock_level" placeholder="Min Stock" value="0" class="border rounded px-3 py-2">
                    <div class="flex items-center">
                        <input type="checkbox" name="is_active" value="1" checked class="mr-2">
                        <label class="text-sm">Active</label>
                    </div>
                    <button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded">Add</button>
                </div>
            </form>
        </div>

        <!-- Variants List -->
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Name</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">SKU</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Price</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Stock</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($product->variants as $variant)
                    <tr>
                        <td class="px-4 py-3 text-sm">{{ $variant->name }}</td>
                        <td class="px-4 py-3 text-sm">{{ $variant->sku ?? '-' }}</td>
                        <td class="px-4 py-3 text-sm">{{ $currency_symbol }}{{ number_format($variant->price, 2) }}</td>
                        <td class="px-4 py-3 text-sm">
                            {{ $variant->stock_quantity }}
                            @if($variant->isLowStock())
                                <span class="text-red-600">(Low)</span>
                            @endif
                        </td>
                        <td class="px-4 py-3">
                            <span class="px-2 py-1 text-xs rounded-full {{ $variant->is_active ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
                                {{ $variant->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm">
                            <button onclick="editVariant({{ $variant->id }})" class="text-blue-600 hover:text-blue-900">
                                <i class="fas fa-edit"></i>
                            </button>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="px-4 py-8 text-center text-gray-500">No variants. Add one above.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
