{{-- @php
$sticker_id = $attributes->get('sticker_id');
\Log::info('Sticker ID in Blade:', ['sticker_id' => $sticker_id]);
@endphp

<div class="flex flex-col items-start space-y-2">
    @if (!empty($sticker_id))
        <a href="{{ route('stickers.pdf', $sticker_id) }}"
           target="_blank"
           class="px-2 py-1 border border-success-500 text-success-600 rounded hover:bg-success-50 hover:border-success-700 transition text-sm">
            Generate Template
        </a>
    @else
        <span class="text-gray-500">
            Enter Sticker ID to generate template
        </span>
    @endif
</div> --}}

{{-- @props(['sticker_id']) --}}
@props([
    'sticker_id',
    'plant_id',
    'item_characteristic_id',
])


{{-- @php
    $var = $sticker_id ?? 'undefined';
    \Log::info('Blade Debug: sticker_id type = ' . gettype($var));
    \Log::info('Blade Debug: sticker_id value = ', ['value' => $var]);

    $isValid = $sticker_id && $sticker_id != 'empty';
    $url = $isValid ? route('stickers.pdf', ['stickerId' => $sticker_id]) : '#';
@endphp --}}

{{-- @php
    use App\Models\StickerStructureDetail;

    $isValid = false;

    if (!empty($sticker_id) && $sticker_id != 'empty') {
        $isValid = StickerStructureDetail::where('sticker_id', $sticker_id)->exists();
    }

    $url = $isValid
        ? route('stickers.pdf', ['stickerId' => $sticker_id])
        : '#';
@endphp --}}

@php
    use App\Models\StickerStructureDetail;

    $hasStickerId = !empty($sticker_id) && $sticker_id != 'empty';

    $hasPlantAndItem = !empty($plant_id) && !empty($item_characteristic_id);

    $isValid = false;

    if ($hasStickerId) {
        $isValid = StickerStructureDetail::where('sticker_id', $sticker_id)->exists();
    }

    $url = $isValid
        ? route('stickers.pdf', [
            'stickerId' => $sticker_id,
            // include only if present
            'plant_id' => $hasPlantAndItem ? $plant_id : null,
            'item_characteristic_id' => $hasPlantAndItem ? $item_characteristic_id : null,
        ])
        : '#';
@endphp


<div class="flex flex-col items-start space-y-2">
    <a
        href="{{ $url }}"
        @if(!$isValid)
            onclick="alert('Sticker ID not found in sticker structure details'); return false;"
        @endif
        target="_blank"
        class="px-2 py-1 border
               {{ $isValid
                   ? 'border-success-500 text-success-600 hover:bg-success-50 hover:border-success-700'
                   : 'border-gray-300 text-gray-400 cursor-not-allowed' }}
               rounded transition text-sm"
    >
        Generate Template
    </a>
</div>







{{-- <div class="flex flex-col items-start space-y-2">
    <a
        href="{{ $url }}"
        @if(!$isValid)
            onclick="alert('Please enter a Sticker ID first'); return false;"
        @endif
        target="_blank"
        class="px-2 py-1 border
               {{ $isValid
                   ? 'border-success-500 text-success-600 hover:bg-success-50 hover:border-success-700'
                   : 'border-gray-300 text-gray-400 cursor-not-allowed' }}
               rounded transition text-sm"
    >
        Generate Template
    </a>
</div> --}}




