<div
    class="autocomplete-component"
    data-live-component="{{componentId}}"
    data-state='{{stateJson}}'
>
    <div class="autocomplete-header">
        <h3 class="autocomplete-title">Intelligent Autocomplete</h3>
        <div class="context-selector">
            <for items="contextOptions" as="option">
                <button
                    class="context-btn {{option.isActive ? 'active' : ''}}"
                    data-live-action="changeContext"
                    data-param-context="{{option.value}}"
                >
                    {{option.icon}} {{option.label}}
                </button>
            </for>
        </div>
    </div>

    <div class="autocomplete-input-wrapper">
        <div class="input-icon">🔍</div>
        <input
            type="text"
            name="query"
            class="autocomplete-input"
            placeholder="{{inputPlaceholder}}"
            value="{{query}}"
            data-live-action="getSuggestions"
            data-live-debounce="200"
            autocomplete="off"
        />
    </div>

    <if condition="hasSuggestions">
        <div class="suggestions-dropdown {{dropdownClass}}">
            <div class="suggestions-header">
                <span class="suggestions-count">{{suggestionCount}} Vorschläge</span>
            </div>
            <div class="suggestions-list">
                <for items="suggestions" as="suggestion">
                    <div
                        class="suggestion-item {{suggestion.displayClass}}"
                        data-live-action="selectSuggestion"
                        data-param-suggestion="{{suggestion.text}}"
                        data-param-context="{{context}}"
                    >
                        <div class="suggestion-icon">{{suggestion.icon}}</div>
                        <div class="suggestion-content">
                            <div class="suggestion-text">{{suggestion.highlightedText}}</div>
                            <div class="suggestion-category">{{suggestion.category}}</div>
                        </div>
                    </div>
                </for>
            </div>
        </div>
    </if>

    <if condition="!hasQuery">
        <div class="autocomplete-hint">
            <div class="hint-icon">💡</div>
            <div class="hint-text">{{emptyStateMessage}}</div>
        </div>
    </if>

    <if condition="selected_value">
        <div class="selected-value-display">
            <div class="selected-label">Ausgewählter Wert:</div>
            <div class="selected-value">{{selected_value}}</div>
        </div>
    </if>

    <if condition="hasRecentSearches">
        <div class="recent-searches">
            <div class="recent-header">
                <span class="recent-title">Letzte Suchen ({{recentSearchCount}})</span>
                <if condition="showClearRecent">
                    <button
                        class="clear-recent-btn"
                        data-live-action="clearRecent"
                    >
                        Alle löschen
                    </button>
                </if>
            </div>
            <div class="recent-list">
                <for items="recent_searches" as="search">
                    <div
                        class="recent-item"
                        data-live-action="selectSuggestion"
                        data-param-suggestion="{{search.text}}"
                        data-param-context="{{search.context}}"
                    >
                        <div class="recent-icon">🕐</div>
                        <div class="recent-text">{{search.text}}</div>
                        <div class="recent-context">{{search.context}}</div>
                    </div>
                </for>
            </div>
        </div>
    </if>
</div>

<style>
.autocomplete-component {
    background: white;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.autocomplete-header {
    margin-bottom: 1.5rem;
}

.autocomplete-title {
    font-size: 1.25rem;
    margin-bottom: 1rem;
    color: #333;
}

.context-selector {
    display: flex;
    gap: 0.5rem;
}

.context-btn {
    padding: 0.5rem 1rem;
    border: 1px solid #e0e0e0;
    background: white;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.875rem;
}

.context-btn:hover {
    background: #f5f5f5;
}

.context-btn.active {
    background: #2196F3;
    color: white;
    border-color: #2196F3;
}

.autocomplete-input-wrapper {
    position: relative;
    margin-bottom: 0.5rem;
}

.input-icon {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.25rem;
    pointer-events: none;
}

.autocomplete-input {
    width: 100%;
    padding: 0.75rem 1rem 0.75rem 3rem;
    font-size: 1rem;
    border: 2px solid #e0e0e0;
    border-radius: 6px;
    transition: border-color 0.2s;
}

.autocomplete-input:focus {
    outline: none;
    border-color: #2196F3;
}

.suggestions-dropdown {
    background: white;
    border: 2px solid #2196F3;
    border-radius: 6px;
    margin-bottom: 1rem;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.suggestions-header {
    padding: 0.5rem 1rem;
    background: #f9f9f9;
    border-bottom: 1px solid #e0e0e0;
}

.suggestions-count {
    font-size: 0.875rem;
    color: #666;
    font-weight: 600;
}

.suggestions-list {
    max-height: 300px;
    overflow-y: auto;
}

.suggestion-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background 0.2s;
    border-bottom: 1px solid #f0f0f0;
}

.suggestion-item:last-child {
    border-bottom: none;
}

.suggestion-item:hover {
    background: #f5f5f5;
}

.suggestion-icon {
    font-size: 1.5rem;
    width: 2rem;
    text-align: center;
}

.suggestion-content {
    flex: 1;
}

.suggestion-text {
    font-weight: 600;
    color: #333;
    margin-bottom: 0.125rem;
}

.suggestion-category {
    font-size: 0.75rem;
    color: #999;
}

.autocomplete-hint {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem;
    background: #f9f9f9;
    border-radius: 6px;
    margin-bottom: 1rem;
}

.hint-icon {
    font-size: 1.5rem;
}

.hint-text {
    color: #666;
    font-size: 0.875rem;
}

.selected-value-display {
    background: #e3f2fd;
    padding: 1rem;
    border-radius: 6px;
    border-left: 4px solid #2196F3;
    margin-bottom: 1rem;
}

.selected-label {
    font-size: 0.875rem;
    color: #666;
    margin-bottom: 0.25rem;
}

.selected-value {
    font-weight: 600;
    color: #1976D2;
    font-size: 1.125rem;
}

.recent-searches {
    margin-top: 1.5rem;
}

.recent-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.recent-title {
    font-weight: 600;
    color: #555;
}

.clear-recent-btn {
    background: none;
    border: none;
    color: #f44336;
    cursor: pointer;
    font-size: 0.875rem;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    transition: background 0.2s;
}

.clear-recent-btn:hover {
    background: #ffebee;
}

.recent-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.recent-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    background: #f9f9f9;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s;
}

.recent-item:hover {
    background: #f0f0f0;
}

.recent-icon {
    font-size: 1.25rem;
}

.recent-text {
    flex: 1;
    font-weight: 500;
    color: #333;
}

.recent-context {
    font-size: 0.75rem;
    color: #999;
    text-transform: uppercase;
}
</style>
