@using FastReport.Web.Application.Localizations
@inherits ExportSettingsBase

<label>@localization.PageRange</label>

<div class="fr-webreport-popup-content-export-parameters-row">

    <button type="button" class="fr-webreport-popup-content-export-parameters-button @IsActive("PageRange", "All")"
            @onclick="OnAllClick">
        @localization.All
    </button>

    <button type="button" class="fr-webreport-popup-content-export-parameters-button @IsActive("PageRange", "PageNumbers")"
            @onclick="OnFirstClick">
        @localization.First
    </button>

    <input type="text"
           class="fr-webreport-popup-content-export-parameters-input"
           pattern="[0-9,-\s]"
           placeholder="2, 5-132"
           @bind-value="PageNumbers"
           @onclick="OnRangeClick">
</div>

@code {

    PageSelectorLocalization localization;

    protected override void LocalizeForm()
    {
        var res = WebReport.Res;
        localization = new PageSelectorLocalization(res);
    }

    public string PageNumbers
    {
        get => GetPageNumbers();
        set => SetPageNumbers(value);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        ExportParameters["PageRange"] = "All";
    }

    protected string IsActive(string key, string val)
    {
        if (ExportParameters.TryGetValue(key, out string oldVal))
        {
            return ExportParameters[key] == val ? "active" : string.Empty;
        }
        else
            return string.Empty;
    }

    void OnAllClick(MouseEventArgs e)
    {
        ExportParameters["PageRange"] = "All";
        ExportParameters.Remove("PageNumbers");
    }

    void OnFirstClick(MouseEventArgs e)
    {
        ExportParameters["PageNumbers"] = (WebReport.CurrentPageIndex + 1).ToString();

        ExportParameters["PageRange"] = "PageNumbers";
    }

    void OnRangeClick(MouseEventArgs e)
    {
        ExportParameters["PageRange"] = "PageNumbers";
    }

    protected string GetPageNumbers()
    {
        if (ExportParameters.TryGetValue("PageNumbers", out string value))
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                return value;
            }
            else
                return null;
        }
        else
            return null;
    }

    protected void SetPageNumbers(string newPageNumbers)
    {
        ExportParameters["PageNumbers"] = newPageNumbers;
    }
}
