@* Shared modal shell for per-driver address pickers. Parent page toggles `Visible`;
   the child fragment renders the per-driver picker body. The shell handles modal
   chrome, the "Use this address" button, and dismisses on close. *@

@if (Visible)
{
    <div class="modal-backdrop fade show" style="display:block"></div>
    <div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">@Title</h5>
                    <button type="button" class="btn-close" aria-label="Close" @onclick="OnCloseAsync"></button>
                </div>
                <div class="modal-body">
                    @ChildContent
                </div>
                <div class="modal-footer">
                    @if (!string.IsNullOrEmpty(CurrentAddress))
                    {
                        <code class="me-auto mono">@CurrentAddress</code>
                    }
                    <button type="button" class="btn btn-outline-secondary" @onclick="OnCloseAsync">Close</button>
                    <button type="button" class="btn btn-primary" disabled="@string.IsNullOrEmpty(CurrentAddress)"
                            @onclick="OnUseAsync">
                        Use this address
                    </button>
                </div>
            </div>
        </div>
    </div>
}

@code {
    [Parameter] public bool Visible { get; set; }
    [Parameter] public EventCallback<bool> VisibleChanged { get; set; }
    [Parameter] public string Title { get; set; } = "Address builder";
    [Parameter] public string CurrentAddress { get; set; } = "";
    [Parameter] public EventCallback<string> OnPickAddress { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }

    private async Task OnCloseAsync()
    {
        await VisibleChanged.InvokeAsync(false);
    }

    private async Task OnUseAsync()
    {
        await OnPickAddress.InvokeAsync(CurrentAddress);
        await VisibleChanged.InvokeAsync(false);
    }
}
