@*   Xafari Framework version : 17.2.3011.1336   *@

@using DevExpress.ExpressApp.Editors
@using DevExpress.ExpressApp.Model
@using Xafari.Mvc
@using Xafari.Mvc.Layout
@using Xafari.Mvc.Templates

@model MvcLayoutManager

@{
    var layoutModel = this.Model.ModelViewLayout;
    var viewItems = this.Model.ViewItems;

    this.Model.UpdateItemsVisibility();
}

@if (this.Model.LayoutNode is IModelSplitLayout)
{
    if (viewItems.Count == 1)
    {
        <div class="Item ListViewItem">
            @Html.Xafari().MvcControl(viewItems[0].Control).GetHtml()
        </div>
    }
    if (viewItems.Count == 2 && viewItems[1].Id == DevExpress.ExpressApp.ListView.DetailViewControlID)
    {
        @Html.Xafari().RelativePartial("_LayoutContainerForListView")
    }
}
else
{
    // SetupContainerControl
    var id = string.Empty;
    var css = "Layout";

    if (((layoutModel != null) && (viewItems.Count != 0)) && (!this.Model.isSimpleLayout || (viewItems.Count != 1)))
    {
        if (this.Model.ViewEditMode == ViewEditMode.View)
        {
            id = "MainLayoutView";
            css = css + " LayoutViewMode";
        }
        else
        {
            id = "MainLayoutEdit";
            css = css + " LayoutEditMode";
        }
        if (layoutModel.Parent is IModelDashboardView)
        {
            css = css + " DashboardView";
        }
        if (this.Model.LayoutCSSInfo != null)
        {
            DevExpress.ExpressApp.Web.Layout.LayoutItemCSSInfoBase cssInfo = this.Model.LayoutCSSInfo.GetCssInfo(layoutModel);
            css = css + " " + cssInfo.CardCssClassNameCore;
        }
    }
    //

    <div id="@(id)" class="@(css)">
        @if ((layoutModel != null) && (viewItems.Count > 0))
        {
            if (this.Model.ViewEditMode == ViewEditMode.View || !this.ViewData.Frame().View.IsRoot)
            {
                if (this.Model.isSimpleLayout && (viewItems.Count == 1))
                {
                    @Html.Xafari().MvcControl(viewItems[0].Control).GetHtml()
                }
                else
                {
                    foreach (IModelViewLayoutElement element in layoutModel)
                    {
                        var item = this.Model.Items[DevExpress.ExpressApp.Web.SystemModule.WebIdHelper.GetLayoutItemId(element)];
                        if (item != null)
                        {
                            this.ViewData[MvcLayoutManager.ContainerKeyForViewData] = item;
                            @Html.Xafari().RelativePartial("_ProcessItem")
                        }
                    }
                }
            }
            else
            {
                if (this.Model.isSimpleLayout && (viewItems.Count == 1))
                {
                    @Html.Xafari().MvcControl(viewItems[0].Control).GetHtml()
                }
                else
                {
                    var formId = this.ViewData.Window().ViewEditModeFormId();
                    var formSubmitUrl = this.Url.Action("SubmitAndMVCActionExecute", "Application", new { windowId = this.ViewData.Window().WindowId(), windowState = this.ViewData.Window().WindowState });
                    using (Html.BeginForm(null, null, FormMethod.Post, new { @id = formId, @action = formSubmitUrl }))
                    {
                        @Html.Hidden("mvcActionUrl")
                        @Html.Hidden("ClientServerParams")

                        foreach (IModelViewLayoutElement element in layoutModel)
                        {
                            var item = this.Model.Items[DevExpress.ExpressApp.Web.SystemModule.WebIdHelper.GetLayoutItemId(element)];
                            if (item != null)
                            {
                                this.ViewData[MvcLayoutManager.ContainerKeyForViewData] = item;
                                @Html.Xafari().RelativePartial("_ProcessItem")
                            }
                        }

                    }
                    <script type="text/javascript" id="@Html.DXScriptId(formId)">
                        $(document).ready(function () {
                            $("#@formId").on('keypress', function (e) {
                                e = e || window.event;
                                if ((e.keyCode == 13 || e.which == 13) && (e.target.type != "textarea"))
                                    e.preventDefault ? e.preventDefault() : e.returnValue = false;
                            });

                            $("#@formId").validate(
                            {
                                debug: true,
                                submitHandler: function (form) {
                                    $(form).ajaxSubmit();
                                }
                            });
                        });
                    </script>
                }
            }
        }
    </div>
}