<div class="band-editor">
    <button class="band-editor-toggle" @onclick="() => IsExpanded = !IsExpanded">
        <span class="band-editor-toggle-icon @(IsExpanded ? "expanded" : "")">&#9658;</span>
        <span>Layout</span>
    </button>
    @if (IsExpanded)
    {
        <div class="band-editor-body">
            <div class="band-controls">
                <DxButton Text="Band hinzufügen" Click="OnAddBand" />
                <DxButton Text="Layout speichern" Click="OnSaveLayout" Enabled="@CanSave" />
                <DxButton Text="Layout zurücksetzen" Click="OnResetLayout" />
            </div>
            @foreach (var band in Bands)
            {
                <div class="band-row">
                    <DxTextBox Text="@band.Caption" TextChanged="@(value => OnBandCaptionChanged.InvokeAsync((band, value)))" />
                    <DxButton Text="Entfernen" Click="@(() => OnRemoveBand.InvokeAsync(band))" />
                </div>
            }
            <DxFormLayout CssClass="band-columns" ColCount="2">
                @foreach (var column in Columns)
                {
                    <DxFormLayoutItem Caption="@column.Caption">
                        <DxComboBox Data="@BandOptions"
                                    TData="BandOption"
                                    TValue="string"
                                    TextFieldName="Caption"
                                    ValueFieldName="Id"
                                    Value="@GetColumnBand(column.FieldName)"
                                    ValueChanged="@(value => OnColumnBandChanged.InvokeAsync((column.FieldName, value)))"
                                    Width="100%" />
                    </DxFormLayoutItem>
                }
            </DxFormLayout>
        </div>
    }
</div>

@code {
    private bool IsExpanded { get; set; }

    [Parameter, EditorRequired] public List<BandDefinition> Bands { get; set; } = new();
    [Parameter, EditorRequired] public List<BandOption> BandOptions { get; set; } = new();
    [Parameter, EditorRequired] public List<ColumnDefinition> Columns { get; set; } = new();
    [Parameter, EditorRequired] public Func<string, string> GetColumnBand { get; set; } = _ => string.Empty;
    [Parameter, EditorRequired] public bool CanSave { get; set; }

    [Parameter] public EventCallback OnAddBand { get; set; }
    [Parameter] public EventCallback OnSaveLayout { get; set; }
    [Parameter] public EventCallback OnResetLayout { get; set; }
    [Parameter] public EventCallback<BandDefinition> OnRemoveBand { get; set; }
    [Parameter] public EventCallback<(BandDefinition Band, string Value)> OnBandCaptionChanged { get; set; }
    [Parameter] public EventCallback<(string FieldName, string? BandId)> OnColumnBandChanged { get; set; }
}