@*   Xafari Framework version : 17.2.3011.1336   *@

@using DevExpress.ExpressApp.Actions
@using DevExpress.ExpressApp.Templates
@using Xafari.Mvc
@using Xafari.Mvc.MVCx.Templates.ActionContainer
@using Xafari.Mvc.Templates
@using Xafari.Utils

@model  Xafari.Mvc.MVCx.Templates.ActionContainer.MVCxNavigationTabsActionContainer

@helper BuildPagesWithItemsIntoTabs(PageControlSettings settings, SingleChoiceAction navigationAction, bool showTabsImages, bool showImages)
{
    foreach (ChoiceActionItem item in navigationAction.Items)
    {
        if (item.Active)
        {
            MVCxTabPage tabPage = new TabPageChoiceActionItem(item, navigationAction, showTabsImages).TabPage;
            settings.TabPages.Add(tabPage);
            tabPage.Name = item.Id;
            
            var choiceActionItem = item;
            tabPage.SetContent(() =>
            {
                var menuSettings = new MenuSettings();
                menuSettings.AccessibilityCompliant = true;
                menuSettings.ShowAsToolbar = true;
                menuSettings.Name = "M" + tabPage.Index;
                menuSettings.SkinID = "MenuLinks";
                menuSettings.EnableClientSideAPI = true;
                
                menuSettings.Items.Clear();
                foreach (ChoiceActionItem item2 in choiceActionItem.Items)
                {
                    if (item2.Active)
                    {
                        MenuChoiceActionItem item3 = new MenuChoiceActionItem(item2, navigationAction, showImages);
                        MVCxMenuItem menuItem = item3.MenuItem;
                        menuItem.DataItem = item2;
                        menuSettings.Items.Add(menuItem);
                    }
                }
                //OnLoad
                var xafActionItemExecuteScript = navigationAction.GetXafActionExecuteScript(this.ViewData.Window(), navigationAction.ConfirmationMessage, " s.cpNodeKeyToItemPathIndex[e.item.name]");
                menuSettings.ClientSideEvents.ItemClick = string.Format("function(s, e) {{\r\n    var hasPathIndex = s.cpNodeKeyToItemPathIndex[e.item.name];\r\n    e.processOnServer = false;\r\n    if(typeof xaf == 'undefined' || !xaf.ConfirmUnsavedChangedController || xaf.ConfirmUnsavedChangedController.CanProcessCallbackForNavigation(s, e)) {{\r\n        if(hasPathIndex) {{\r\n          MarkingAsNavigationRequest(); {0}\r\n       }}\r\n     }}\r\n}}", xafActionItemExecuteScript);
                menuSettings.CustomJSProperties += (sender, args) =>
                {
                    var dictionary = choiceActionItem.Items.ToDictionary(item2 => item2.Id, item2 => item2.GetItemPathIndex(navigationAction));
                    args.Properties["cpNodeKeyToItemPathIndex"] = dictionary;

                };
                
                Html.DevExpress().Menu(menuSettings).Render();
            });
        }
    }

}

@helper BuildPagesWithItemsAsTabs(PageControlSettings settings, SingleChoiceAction navigationAction, bool showTabsImages)
{
    settings.Styles.Content.Font.Size = new FontUnit(0);
    foreach (ChoiceActionItem item in navigationAction.Items)
    {
        if (item.Active)
        {
            foreach (ChoiceActionItem item2 in item.Items)
            {
                if (item2.Active)
                {
                    MVCxTabPage tabPage = new TabPageChoiceActionItem(item2, navigationAction, showTabsImages).TabPage;
                    tabPage.Name = item2.Id;
                    settings.TabPages.Add(tabPage);
                }
            }
        }
    }
    var xafActionItemExecuteScript = navigationAction.GetXafActionExecuteScript(this.ViewData.Window(), navigationAction.ConfirmationMessage, " s.cpNodeKeyToItemPathIndex[e.tab.name]");
    settings.ClientSideEvents.ActiveTabChanging = string.Format("function(s, e) {{\r\n    var hasPathIndex = s.cpNodeKeyToItemPathIndex[e.tab.name];\r\n    e.processOnServer = false;\r\n    if(typeof xaf == 'undefined' || !xaf.ConfirmUnsavedChangedController || xaf.ConfirmUnsavedChangedController.CanProcessCallbackForNavigation(s, e)) {{\r\n        if(hasPathIndex) {{\r\n          MarkingAsNavigationRequest(); {0}\r\n       }}\r\n     }}\r\n}}", xafActionItemExecuteScript);
    settings.CustomJSProperties += (sender, args) =>
    {
        args.Properties["cpNodeKeyToItemPathIndex"] = navigationAction.Items.SelectMany(item => item.Items).ToDictionary(item2 => item2.Id, item2 => item2.GetItemPathIndex(navigationAction)); 
    };
}

@{
    this.Model.FrameTemplate.RegisterActionContainers(new IActionContainer[] { this.Model });
}

@{
    var navigationAction = this.Model.Navigation;
    
    var settings = new PageControlSettings();
    settings.EnableClientSideAPI = true;
    settings.SettingsLoadingPanel.ShowImage = false;
    settings.Name = this.ViewData.Frame().ControlId("PC");
    settings.Width = Unit.Percentage(100);
    settings.SaveStateToCookies = false;
    
    //SetSpaceAfterTabsContent
    var spaceAfterTabsContent = this.Model.SpaceAfterTabsTemplate;
    if (spaceAfterTabsContent != null)
    {
        settings.SetSpaceAfterTabsContent(spaceAfterTabsContent);
    }
    
    //RebuildPages()
    settings.TabPages.Clear();
    if (this.Model.GroupingStyle == DevExpress.ExpressApp.Web.Templates.ActionContainers.GroupingStyle.GroupItemsIntoTabs)
    {
        @BuildPagesWithItemsIntoTabs(settings, navigationAction, this.Model.ShowTabsImages,this.Model.ShowImages)
    }
    else
    {
        @BuildPagesWithItemsAsTabs(settings, navigationAction, this.Model.ShowTabsImages)
    }
    //UpdateActiveTab()
    settings.PreRender += (sender, args) =>
    {
        var pageControl = sender as MVCxPageControl;
        if (pageControl == null) return;
        
        if ((navigationAction != null) && (navigationAction.SelectedItem != null))
        {
            if (this.Model.GroupingStyle == DevExpress.ExpressApp.Web.Templates.ActionContainers.GroupingStyle.GroupItemsIntoTabs)
            {
                var selectedTabName = string.Empty;
                var parentItem = navigationAction.SelectedItem;
                while (parentItem != null)
                {
                    if (parentItem.ParentItem == null)
                    {
                        selectedTabName = parentItem.Id;
                    }
                    parentItem = parentItem.ParentItem;
                }
                var activeTabPage = pageControl.TabPages.FindByName(selectedTabName);
                if (activeTabPage != null)
                {
                    pageControl.ActiveTabPage = activeTabPage;
                }
            }
            else
            {
                var activeTabPage = pageControl.TabPages.FindByName(navigationAction.SelectedItem.Id);
                if (activeTabPage != null)
                {
                    pageControl.ActiveTabPage = activeTabPage;
                }
            }
        }
    };
}

@if (!string.IsNullOrEmpty(this.Model.CssClass))
{
    <div id="NTAC" class="@(this.Model.CssClass)">
        @Html.DevExpress().PageControl(settings).GetHtml()
    </div>
}
else
{
    <div id="NTAC">
        @Html.DevExpress().PageControl(settings).GetHtml()
    </div>
}

@{
    this.Model.FrameTemplate.UnregisterActionContainers(new IActionContainer[] { this.Model });
}

