@if (IsVisible)
{
    <div class="modal show d-block" tabindex="-1" style="background: rgba(0,0,0,0.4);">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h6 class="modal-title">Compose '@SourceName' into…</h6>
                    <button type="button" class="btn-close" @onclick="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label small text-muted mb-1">Parent template</label>
                        <select class="form-select form-select-sm" @bind="_parentTemplateId">
                            <option value="0" disabled selected>Select a parent template…</option>
                            @foreach (var opt in ParentOptions)
                            {
                                <option value="@opt.Id">@opt.Label</option>
                            }
                        </select>
                    </div>
                    <div class="mb-1">
                        <label class="form-label small text-muted mb-1">Slot name</label>
                        <input type="text" class="form-control form-control-sm"
                               placeholder="Slot name"
                               @bind="_slotName" />
                    </div>
                    @if (!string.IsNullOrEmpty(ErrorMessage)) { <div class="text-danger small mt-2">@ErrorMessage</div> }
                </div>
                <div class="modal-footer">
                    <button class="btn btn-outline-secondary btn-sm" @onclick="Close">Cancel</button>
                    <button class="btn btn-primary btn-sm" @onclick="Submit" disabled="@(_parentTemplateId == 0 || string.IsNullOrWhiteSpace(_slotName))">Compose</button>
                </div>
            </div>
        </div>
    </div>
}

@code {
    [Parameter] public bool IsVisible { get; set; }
    [Parameter] public EventCallback<bool> IsVisibleChanged { get; set; }
    [Parameter] public int SourceTemplateId { get; set; }
    [Parameter] public string SourceName { get; set; } = string.Empty;
    [Parameter] public IEnumerable<(int Id, string Label)> ParentOptions { get; set; } = Array.Empty<(int, string)>();
    [Parameter] public string? ErrorMessage { get; set; }
    [Parameter] public EventCallback<(int SourceTemplateId, int ParentTemplateId, string SlotName)> OnSubmit { get; set; }

    private bool _wasVisible;
    private int _parentTemplateId;
    private string _slotName = string.Empty;

    protected override void OnParametersSet()
    {
        if (IsVisible && !_wasVisible)
        {
            _parentTemplateId = 0;
            _slotName = SourceName;
        }
        _wasVisible = IsVisible;
    }

    private async Task Close() => await IsVisibleChanged.InvokeAsync(false);

    private async Task Submit()
    {
        if (_parentTemplateId == 0 || string.IsNullOrWhiteSpace(_slotName)) return;
        await OnSubmit.InvokeAsync((SourceTemplateId, _parentTemplateId, _slotName.Trim()));
    }
}
