{{--
    Structured part-number / compatibility block injected into the product
    view modal by ProductCompatServiceProvider's view composer. Renders only
    when there is structured data, so products that haven't been processed yet
    keep their original (legacy) modal untouched.
--}}
@php
    $pcPartNumbers  = $pcPartNumbers  ?? collect();
    $pcDeviceModels = $pcDeviceModels ?? collect();
    $pcCompatibles  = $pcCompatibles  ?? collect();
    $pcBarcodes     = $pcBarcodes     ?? collect();
    $pcTypeLabels   = ['mpn' => 'MPN', 'oem' => 'OEM', 'model_code' => 'Model code', 'internal' => 'Internal', 'other' => 'Other'];
    $pcGroupedDevices = $pcDeviceModels->groupBy(fn ($m) => $m->brand ?: '—');
@endphp

@if($pcPartNumbers->count() || $pcDeviceModels->count() || $pcCompatibles->count() || $pcBarcodes->count())
<div class="row" style="margin-top:10px;">
    <div class="col-md-12">
        <div class="box box-solid">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-microchip"></i> @lang('productcompat::lang.product_specs')</h3>
                @if(!empty($pcStatus) && $pcStatus->checked_at)
                    <span class="label label-success pull-right"><i class="fa fa-check"></i> @lang('productcompat::lang.checked')</span>
                @endif
            </div>
            <div class="box-body">

                {{-- Part numbers --}}
                @if($pcPartNumbers->count())
                    <div style="margin-bottom:12px;">
                        <strong>@lang('productcompat::lang.part_numbers'):</strong>
                        <div style="margin-top:6px;">
                            @foreach($pcPartNumbers as $pn)
                                <a href="{{ url('products?search=' . urlencode($pn->part_number)) }}" target="_blank"
                                   class="label {{ $pn->is_primary ? 'label-primary' : 'label-default' }} pc-chip"
                                   title="{{ $pcTypeLabels[$pn->type] ?? $pn->type }}{{ $pn->needs_review ? ' · needs review' : '' }}"
                                   style="display:inline-block;margin:2px;font-size:90%;{{ $pn->needs_review ? 'border:1px dashed #f0ad4e;' : '' }}">
                                    {{ $pn->part_number }}
                                    <small style="opacity:.7;">[{{ $pcTypeLabels[$pn->type] ?? $pn->type }}]</small>
                                </a>
                            @endforeach
                        </div>
                    </div>
                @endif

                {{-- Compatible devices, grouped by brand --}}
                @if($pcDeviceModels->count())
                    <div style="margin-bottom:12px;">
                        <strong>@lang('productcompat::lang.fits_devices'):</strong>
                        <input type="text" class="form-control input-sm pc-device-filter" placeholder="@lang('productcompat::lang.filter_devices')" style="max-width:280px;margin:6px 0;">
                        <div class="pc-device-groups">
                            @foreach($pcGroupedDevices as $brand => $models)
                                <div class="pc-device-group" style="margin-bottom:4px;">
                                    <span class="text-muted" style="font-weight:600;">{{ $brand }}</span>
                                    <div style="margin-left:8px;">
                                        @foreach($models as $m)
                                            <span class="label label-info pc-device" data-label="{{ strtolower($m->label()) }}"
                                                  style="display:inline-block;margin:2px;font-weight:400;">
                                                {{ trim(($m->series ? $m->series . ' ' : '') . $m->model) }}
                                            </span>
                                        @endforeach
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    </div>
                @endif

                {{-- Barcodes / GTINs --}}
                @if($pcBarcodes->count())
                    <div style="margin-bottom:12px;">
                        <strong>@lang('productcompat::lang.barcodes'):</strong>
                        <div style="margin-top:6px;">
                            @foreach($pcBarcodes as $bc)
                                <span class="label label-default pc-chip" title="{{ $bc->type }}{{ $bc->needs_review ? ' · needs review' : '' }}"
                                      style="display:inline-block;margin:2px;font-size:90%;{{ $bc->needs_review ? 'border:1px dashed #f0ad4e;' : '' }}">
                                    {{ $bc->barcode }}
                                </span>
                            @endforeach
                        </div>
                    </div>
                @endif

                {{-- Compatible catalog products --}}
                @if($pcCompatibles->count())
                    <div style="margin-bottom:4px;">
                        <strong>@lang('productcompat::lang.compatible_products'):</strong>
                        <div style="margin-top:6px;">
                            @foreach($pcCompatibles as $c)
                                @if($c->compatibleProduct)
                                    <button type="button" class="btn btn-xs btn-default pc-view-product"
                                            data-href="{{ action('\App\Http\Controllers\ProductController@view', [$c->compatibleProduct->id]) }}"
                                            style="margin:2px;">
                                        <i class="fa fa-cube"></i>
                                        {{ $c->compatibleProduct->sku }} — {{ \Illuminate\Support\Str::limit($c->compatibleProduct->name, 40) }}
                                        <small class="text-muted">({{ $c->relation_type }})</small>
                                    </button>
                                @endif
                            @endforeach
                        </div>
                    </div>
                @endif

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

<script>
(function () {
    // Live filter for the device list
    document.querySelectorAll('.pc-device-filter').forEach(function (input) {
        input.addEventListener('keyup', function () {
            var q = this.value.toLowerCase();
            var scope = this.closest('.box-body');
            scope.querySelectorAll('.pc-device').forEach(function (el) {
                el.style.display = el.getAttribute('data-label').indexOf(q) !== -1 ? '' : 'none';
            });
            scope.querySelectorAll('.pc-device-group').forEach(function (g) {
                var anyVisible = Array.prototype.some.call(g.querySelectorAll('.pc-device'), function (e) { return e.style.display !== 'none'; });
                g.style.display = anyVisible ? '' : 'none';
            });
        });
    });
    // Open a compatible product's own modal (reuses the core view action via AJAX)
    document.querySelectorAll('.pc-view-product').forEach(function (btn) {
        btn.addEventListener('click', function () {
            var href = this.getAttribute('data-href');
            if (window.jQuery && jQuery('#view_product_modal').length) {
                jQuery.get(href, function (data) {
                    jQuery('#view_product_modal').html(data).modal('show');
                });
            } else {
                window.open(href, '_blank');
            }
        });
    });
})();
</script>
@endif
