<?php
    $addClass = '';
    $addData = '';
    if(!empty($input['get']) && is_array($input['get'])){
        $addClass .= " getData";
        $addData .= " data-ajax-get-data='".$input['get']['ajax']."'";
        $addData .= " data-empty-get-data='".json_encode($input['get']['empty'])."'";
        $addData .= " data-select-get-data='".json_encode($input['get']['select'])."'";
    }

    if(!empty($input['data']) && is_array($input['data'])){
        $addClass .= " dataModel";
        $addData .= " data-ajax-data-model='".$input['data']['ajax']."'";
        $addData .= " data-remove-data-model='".json_encode($input['data']['remove'])."'";
        $addData .= " data-after-data-model='".$input['data']['after']."'";
    }
?>
<script>

//    console.log(<?php echo json_encode($input['value'], 15, 512) ?>);
</script>
<div class="form-group">
    
    <label for="<?php echo e($input['id']); ?>" class="<?php echo e($input['required'] ?? ''); ?> form-label"><?php echo e($input['label']); ?></label>
    <select class="form-select form-select-lg form-select-solid <?php echo $addClass ?? ''; ?>" <?php echo $addData ?? ''; ?> data-control="select2" <?php if(isset($input['add'])): ?> data-tags="true" <?php endif; ?> <?php if(isset($input['readonly'])): ?> disabled <?php endif; ?> data-allow-clear="true" <?php echo $input['modal'] ? 'data-dropdown-parent="#modalAddEdit"' : ''; ?> data-hide-search="<?php echo e($input['hide-search']); ?>" data-placeholder="<?php echo e($input['placeholder']); ?>" id="<?php echo e($input['id']); ?>" name="<?php echo e($input['name']); ?>" <?php echo $input['multiple'] ? 'multiple="multiple"' : ''; ?> <?php echo $input['other']; ?>>
        <option></option>
        <?php
            $string = $input['id'];
            preg_match('/\d+/', $string, $matches);
            $number = isset($matches[0]) ? $matches[0] : '-';
        ?>
        <?php $arrExist = []; ?>
        <?php if(is_array($input['options'])): ?>
            <?php $__currentLoopData = $input['options']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php if(is_array($value)): ?>
                    <?php $__currentLoopData = $value; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key2 => $value2): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php if(!in_array($key2,$arrExist)): ?>
                        
                        <option class="rmv" data-id="<?php echo e($key2); ?>" value="<?php echo e($key2); ?>" <?php echo e(($input['multiple'] && is_array($input['value']) && in_array($key2, $input['value'])) || (!$input['multiple'] && $key2 == $input['value']) ? 'selected' : ''); ?>><?php echo e($value2); ?></option>
                        
                        <?php endif; ?>
                        <?php
                        array_push($arrExist,$key2);
                        ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <?php else: ?>
                <option class="rmv" data-id="<?php echo e($key); ?>" value="<?php echo e($key); ?>" <?php echo e(($input['multiple'] && is_array($input['value']) && in_array($key, $input['value'])) || (!$input['multiple'] && $key == $input['value']) ? 'selected' : ''); ?>><?php echo e($value); ?></option>
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <?php endif; ?>
    </select>
</div>



<?php if(isset($input['options3'])): ?>
<script>
    $(document).ready(function() {

        $('body').on('change', 'select', function(e) {
            // id of the changed select element
            var id = $(this).attr('id');
            var selectedIndex = $(this).prop('selectedIndex');
            

            var selectedValue = $(this).val();
            
            console.log('brahimmmmm')

            var options2 = <?php echo json_encode($input['options2'], 15, 512) ?>;
            var options3 = <?php echo json_encode($input['options3'], 15, 512) ?>;
            
            selectedIndex--;
            if(options3[selectedIndex]==undefined || options3[selectedIndex]==null || options3[selectedIndex]==[]){
                var arr = [selectedValue];
            }else{
                var arr = Object.values(options3[selectedIndex]);
                if(arr.length==0){
                    var arr = [selectedValue];
                }
            }

            console.log(selectedIndex);
            console.log(arr);

            // id of the next select element
            var nextSelect = $(this).closest('.col-md-4').next('.col-md-4').find('select');
            console.log(nextSelect)

            // check if the next select exists
            if (nextSelect.length > 0) {
                var options = arr;
                nextSelect.empty(); // clear existing options
                nextSelect.append('<option value="">Select an option</option>'); // default option
                options.forEach(function(option) {
                    nextSelect.append('<option value="' + option + '">' + option + '</option>');
                });


            }

            
        });
    });
</script>
<?php endif; ?>
<script>
    var showHideDivsType = $(this).data('showHideDivsType');
    var showHideDivs = $(this).data('showHideDivs');

    if (showHideDivsType == 'select') {
        var inputValue = $(this).val();
    } else if (showHideDivsType == 'radio') {
        var inputValue = $(this).closest('div').find('input[type="radio"]:checked').val();
    }

    console.log(showHideDivsType);
    console.log(showHideDivs);

    $.each(showHideDivs.hide, function(key, value) {
        var div = $('body').find('[data-id="' + key + '"]');
        $.each(value, function(valueKey, valueValue) {
            if (inputValue == valueValue) {
                if (div.is(':visible')) {
                    div.fadeOut('slow');
                }
            }
        });
    });

    $.each(showHideDivs.show, function(key, value) {
        var div = $('body').find('[data-id="' + key + '"]');
        $.each(value, function(valueKey, valueValue) {
            if (inputValue == valueValue) {
                if (div.is(':hidden')) {
                    div.fadeIn('slow');
                }
            }
        });
    });

    e.preventDefault();
</script>
<?php /**PATH /Users/haladigitally/Desktop/TR/operations_manager/resources/views/form/select.blade.php ENDPATH**/ ?>