@using System.IO;

<div class="fr-webreport-tabs">

    @for (int i = 0; i < WebReport.Tabs.Count; i++)
    {
        int copyIndex = i;
        string active = (copyIndex == WebReport.CurrentTabIndex ? "active" : "");

        <div class="fr-webreport-tab @active">

            <a class="fr-webreport-tab-title" @onclick="() => SetTab(copyIndex)">
                @WebReport.GetTabName(copyIndex)
            </a>

            @if (WebReport.Tabs[copyIndex].Closeable)
            {
                <a class="fr-webreport-tab-close" title="Close" @onclick="() => CloseTab(copyIndex)">
                    <img src="_content/FastReport.Web/Resources/close.svg"/>
                </a>
            }

        </div>

    }

</div>

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


    private void SetTab(int i)
    {
        if(i != WebReport.CurrentTabIndex)
        {
            WebReport.SetTab(i);
            WebReport.OnUpdate(false);
        }
    }

    private void CloseTab(int i)
    {
        var activeTab = WebReport.CurrentTab;

        WebReport.Tabs[i].Report.Dispose();
        WebReport.Tabs.RemoveAt(i);

        if (activeTab == null)
        {
            WebReport.CurrentTabIndex = 0;
        }
        else
        {
            for (int j = 0; j < WebReport.Tabs.Count; j++)
                if (WebReport.Tabs[j] == activeTab)
                {
                    WebReport.CurrentTabIndex = j;
                    break;
                }
        }

        WebReport.OnUpdate(false);
    }
}
