<?php $attributes ??= new \Illuminate\View\ComponentAttributeBag;

$__newAttributes = [];
$__propNames = \Illuminate\View\ComponentAttributeBag::extractPropNames((['label', 'wireModel', 'options', 'placeholder' => 'اختر...', 'required' => false]));

foreach ($attributes->all() as $__key => $__value) {
    if (in_array($__key, $__propNames)) {
        $$__key = $$__key ?? $__value;
    } else {
        $__newAttributes[$__key] = $__value;
    }
}

$attributes = new \Illuminate\View\ComponentAttributeBag($__newAttributes);

unset($__propNames);
unset($__newAttributes);

foreach (array_filter((['label', 'wireModel', 'options', 'placeholder' => 'اختر...', 'required' => false]), 'is_string', ARRAY_FILTER_USE_KEY) as $__key => $__value) {
    $$__key = $$__key ?? $__value;
}

$__defined_vars = get_defined_vars();

foreach ($attributes->all() as $__key => $__value) {
    if (array_key_exists($__key, $__defined_vars)) unset($$__key);
}

unset($__defined_vars, $__key, $__value); ?>

<?php
    // Create a unique hash for the options so Livewire rebuilds the component when options change.
    // This perfectly solves the dynamic cascading problem!
    $optionsHash = md5(collect($options)->pluck('id')->join(','));
?>

<div class="w-full" <?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::$currentLoop['key'] = 'ts-wrapper-'.e($wireModel).'-'.e($optionsHash).''; ?>wire:key="ts-wrapper-<?php echo e($wireModel); ?>-<?php echo e($optionsHash); ?>">
    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($label): ?>
        <label class="block text-xs font-bold text-slate-700 mb-1.5"><?php echo e($label); ?> <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($required): ?> <span class="text-red-500">*</span> <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?></label>
    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
    
    <div wire:ignore>
        <select x-data="{
                    ts: null,
                    value: <?php if ((object) ($wireModel) instanceof \Livewire\WireDirective) : ?>window.Livewire.find('<?php echo e($__livewire->getId()); ?>').entangle('<?php echo e($wireModel->value()); ?>')<?php echo e($wireModel->hasModifier('live') ? '.live' : ''); ?><?php else : ?>window.Livewire.find('<?php echo e($__livewire->getId()); ?>').entangle('<?php echo e($wireModel); ?>')<?php endif; ?>.live,
                    init() {
                        this.ts = new TomSelect(this.$el, {
                            create: false,
                            placeholder: '<?php echo e($placeholder); ?>',
                            render: {
                                no_results: function(data, escape) {
                                    return '<div class=\'no-results p-2 text-sm text-slate-500 text-center\'>لا توجد نتائج مطابقة</div>';
                                }
                            },
                            onChange: (val) => {
                                if (this.value !== val) {
                                    this.value = val;
                                }
                            }
                        });
                        
                        // Ensure TomSelect has the correct initial value after Livewire injects it
                        if (this.value) {
                            this.ts.setValue(this.value, true);
                        }
                        
                        this.$watch('value', (val) => {
                            if (val !== this.ts.getValue()) {
                                this.ts.setValue(val, true); // update silent
                            }
                        });
                    }
                }" 
                x-ref="select"
                class="w-full text-sm font-bold text-slate-800" 
                dir="rtl">
            
            <option value=""><?php echo e($placeholder); ?></option>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $options; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $opt): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                <?php 
                    $text = (isset($opt->account_number) ? $opt->account_number . ' - ' : '') . $opt->name . (isset($opt->code) ? ' ('.$opt->code.')' : '');
                ?>
                <option value="<?php echo e($opt->id); ?>"><?php echo e($text); ?></option>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>
        </select>
    </div>
</div>
<?php /**PATH D:\laragon\www\Ledger\resources\views/components/searchable-select.blade.php ENDPATH**/ ?>