@extends('layouts.admin')

@section('title', 'Product Variants - ' . $product->name)

@section('content')
<div class="admin-page-header">
    <h1>Variants: {{ $product->name }}</h1>
    <div class="d-flex gap-2">
        <a href="{{ route('admin.products.index', ['type' => 'variant']) }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Back to Variant Products
        </a>
        @can('products.update')
        @if($adminCatalogueWritable ?? false)
        <a href="{{ route('admin.products.variant.edit', $product->id) }}" class="btn btn-outline-primary">
            <i class="bi bi-pencil"></i> Edit Product
        </a>
        @endif
        @endcan
    </div>
</div>

<div class="admin-card mb-3">
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <h5>Product Information</h5>
                <p><strong>Name:</strong> {{ $product->name }}</p>
                <p><strong>Category:</strong> {{ $product->category->name ?? 'N/A' }}</p>
                <p><strong>Slug:</strong> {{ $product->slug }}</p>
            </div>
            <div class="col-md-6">
                <h5>Variants Summary</h5>
                <p><strong>Total Variants:</strong> {{ $variants->count() }}</p>
                <p><strong>Active Variants:</strong> {{ $variants->where('is_active', true)->count() }}</p>
                <p><strong>Total Stock:</strong> {{ $variants->sum('current_stock') }}</p>
            </div>
        </div>
    </div>
</div>

@if($variants->isEmpty())
    <div class="admin-card">
        <div class="card-body text-center py-5">
            <i class="bi bi-inbox" style="font-size: 3rem; color: #6c757d;"></i>
            <h5 class="mt-3">No Variants Found</h5>
            <p class="text-muted">Variants are created when you create the variant product. You can edit the product to add more variants.</p>
            @can('products.update')
            @if($adminCatalogueWritable ?? false)
            <a href="{{ route('admin.products.variant.edit', $product->id) }}" class="btn btn-primary mt-3">
                <i class="bi bi-pencil"></i> Edit Product
            </a>
            @endif
            @endcan
        </div>
    </div>
@else
    <div class="admin-card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="admin-table table table-hover">
                    <thead>
                        <tr>
                            <th>Image</th>
                            <th>Barcode</th>
                            <th>Attributes</th>
                            <th>Price</th>
                            <th>Stock</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($variants as $variant)
                            <tr>
                                <td>
                                    @if($variant->image_path || $variant->image)
                                        <x-product-image :product="$variant" context="thumb" style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;" onerror="this.onerror=null; this.src='data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTAiIGhlaWdodD0iNTAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjZGVlMmU2Ii8+PHRleHQgeD0iNTAlIiB5PSI1MCUiIGZvbnQtZmFtaWx5PSJBcmlhbCIgZm9udC1zaXplPSIxMiIgZmlsbD0iIzZjNjU3OSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZHk9Ii4zZW0iPk5vIEltYWdlPC90ZXh0Pjwvc3ZnPg==';" />
                                    @else
                                        <div class="bg-secondary rounded d-flex align-items-center justify-content-center" style="width: 50px; height: 50px;">
                                            <i class="bi bi-image text-white" style="font-size: 0.875rem;"></i>
                                        </div>
                                    @endif
                                </td>
                                <td>{{ $variant->barcode }}</td>
                                <td>
                                    @if($variant->attributeValues->isNotEmpty())
                                        @foreach($variant->attributeValues as $attrValue)
                                            <span class="badge bg-secondary me-1">
                                                {{ $attrValue->attribute->name }}: {{ $attrValue->value }}
                                            </span>
                                        @endforeach
                                    @else
                                        <span class="text-muted">No attributes</span>
                                    @endif
                                </td>
                                <td>₹{{ number_format($variant->price, 2) }}</td>
                                <td>{{ $variant->current_stock }}</td>
                                <td>
                                    <span class="admin-badge {{ $variant->is_active ? 'admin-badge-success' : 'admin-badge-danger' }}">
                                        {{ $variant->is_active ? 'Active' : 'Inactive' }}
                                    </span>
                                </td>
                                <td>
                                    <div class="d-flex gap-2">
                                        @can('products.restock')
                                        @if($adminCatalogueWritable ?? false)
                                        <button type="button" 
                                                class="admin-action-btn admin-action-btn-sm" 
                                                style="background: #0d6efd; color: white; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; padding: 0;"
                                                title="Restock"
                                                data-bs-toggle="modal" 
                                                data-bs-target="#restockModal{{ $variant->id }}">
                                            <i class="bi bi-box-arrow-in-down"></i>
                                        </button>
                                        @endif
                                        @endcan
                                        <a href="{{ route('admin.products.variants.stock-history', $variant->id) }}" 
                                           class="admin-action-btn admin-action-btn-sm" 
                                           style="background: #6c757d; color: white; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; padding: 0;"
                                           title="Stock History">
                                            <i class="bi bi-clock-history"></i>
                                        </a>
                                        @can('products.delete')
                                        @if($adminCatalogueWritable ?? false)
                                        <form action="{{ route('admin.products.variants.destroy', $variant->id) }}" 
                                              method="POST" 
                                              class="d-inline" 
                                              onsubmit="return confirm('Are you sure you want to delete this variant?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" 
                                                    class="admin-action-btn admin-action-btn-sm" 
                                                    style="background: #ef4444; height: -webkit-fill-available; color: white; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; padding: 0;"
                                                    title="Delete">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </form>
                                        @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endif

@if($adminCatalogueWritable ?? false)
@foreach($variants as $variant)
    <!-- Restock Modal for Variant {{ $variant->id }} -->
    <div class="modal fade mt-5" id="restockModal{{ $variant->id }}" tabindex="-1" aria-labelledby="restockModalLabel{{ $variant->id }}" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="restockModalLabel{{ $variant->id }}">
                        <i class="bi bi-box-arrow-in-down"></i> Restock Variant
                    </h5>
                    <button type="button" class="btn p-0 text-white" data-bs-dismiss="modal" aria-label="Close"><i class="bi-x-lg"></i></button>
                </div>
                <form action="{{ route('admin.products.variants.restock', $variant->id) }}" method="POST">
                    @csrf
                    @if(!empty($adminCentralWarehouseId))
                        <input type="hidden" name="location_id" value="{{ $adminCentralWarehouseId }}">
                    @endif
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label"><strong>Variant SKU:</strong></label>
                            <p class="mb-0">{{ $variant->sku }}</p>
                        </div>
                        <div class="alert alert-info py-2">
                            Stock is added to <strong>warehouse</strong> only. Transfer to stores afterwards.
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><strong>Attributes:</strong></label>
                            <p class="mb-0">
                                @if($variant->attributeValues->isNotEmpty())
                                    @foreach($variant->attributeValues as $attrValue)
                                        <span class="badge bg-secondary me-1">
                                            {{ $attrValue->attribute->name }}: {{ $attrValue->value }}
                                        </span>
                                    @endforeach
                                @else
                                    <span class="text-muted">No attributes</span>
                                @endif
                            </p>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><strong>Current Stock:</strong></label>
                            <p class="mb-0">{{ $variant->current_stock }}</p>
                        </div>
                        <div class="mb-3">
                            <label for="quantity{{ $variant->id }}" class="form-label">Quantity to Add *</label>
                            <input type="number" 
                                   name="quantity" 
                                   id="quantity{{ $variant->id }}"
                                   class="form-control @error('quantity') is-invalid @enderror" 
                                   value="{{ old('quantity') }}" 
                                   required 
                                   min="1"
                                   placeholder="Enter quantity to add">
                            @error('quantity')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <small class="form-text text-muted">Enter the quantity to add to warehouse stock</small>
                        </div>
                        <div class="mb-3">
                            <label for="notes{{ $variant->id }}" class="form-label">Notes (Optional)</label>
                            <textarea name="notes" 
                                      id="notes{{ $variant->id }}"
                                      class="form-control @error('notes') is-invalid @enderror" 
                                      rows="3"
                                      placeholder="e.g., New shipment arrived, Returned items, etc.">{{ old('notes') }}</textarea>
                            @error('notes')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                            <small class="form-text text-muted">Add any notes about this restock</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-plus-circle"></i> Restock Variant
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endforeach
@endif

@endsection
