@using static FastReport.Web.ExportsHelper
@inject NavigationManager navManager

@if (WebReport.EnableModalDialog)
{
    @*CONTENT*@
    <div class="fr-webreport-popup" @onclick="CancelClick">
        <div class="fr-webreport-popup-content" @onclick="() => { }" @onclick:stopPropagation="true" @onclick:preventDefault="true">
            <ModalContainerContent WebReport="WebReport" ExportParameters="ExportParameters" />
            @*BUTTONS*@
            <div class="fr-webreport-popup-content-buttons">
                <button class="fr-webreport-popup-content-btn-submit" @onclick="CancelClick">@LocalizedCancel</button>
                <button class="fr-webreport-popup-content-btn-submit" @onclick="OkClick">OK</button>
            </div>
        </div>
    </div>
}

@code {

    private string LocalizedCancel;

    Dictionary<string, string> ExportParameters = new Dictionary<string, string>();


    [Parameter]
    public WebReport WebReport { get; set; }

    internal void Update()
    {
        StateHasChanged();
    }

    protected override void OnParametersSet()
    {
        var res = WebReport.Res;
        res.Root("Buttons");
        LocalizedCancel = res.Get("Cancel");
    }

    private string ExportParametersToString()
    {
        if (!ExportParameters.Any())
            return "";

        var uri = string.Join('&',
                ExportParameters.Select(kvp =>
                    $"{kvp.Key}={kvp.Value}"));

#if DEBUG
        Console.WriteLine(uri);
#endif
        return "&" + uri;
    }

    private void OkClick()
    {
        string href = $"{GetHref(WebReport)}{ExportParametersToString()}";

        ExportParameters.Clear();
        WebReport.EnableModalDialog = false;
        navManager.NavigateTo(href, true);

    }


    private void CancelClick()
    {
        ExportParameters.Clear();

        WebReport.EnableModalDialog = false;
    }
}
