@using System.Collections.Generic
@using System.Linq
@using MrCMS.Entities.Documents.Layout
@using MrCMS.Entities.Widget
@using MrCMS.Helpers
@model MrCMS.Entities.Documents.Web.Webpage
<div class="form-group">
    @Html.LabelFor(x => x.PageTemplate.Id, Resource("Page Template"))
    @Html.DropDownListFor(webpage => webpage.PageTemplate.Id, ViewData["template-options"] as IEnumerable<SelectListItem>, new { @class = "form-control" })
</div>
<div id="custom-layout">
    <label>@Resource("Admin Webpage Layout Layout Areas", "Layout Areas")</label>
    <div class="panel-group" id="accordion-layout-areas">
        @{
            var currentLayout = GetCurrentLayout.Get(Model);
            if (currentLayout.GetLayoutAreas().Any())
            {
                foreach (LayoutArea layoutArea in currentLayout.GetLayoutAreas())
                {
                    List<Widget> widgets = layoutArea.GetWidgets(Model, true);
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                                <a data-toggle="collapse" data-parent="#accordion-layout-areas" href="#collapse-@layoutArea.Id.ToString()">
                                    @layoutArea.AreaName (@widgets.Count(widget => !Model.IsHidden(widget)) @Resource("Admin Webpage Layout Shown Widgets", "Shown Widgets"), @widgets.Count(widget => Model.IsHidden(widget)) @Resource("Admin Webpage Layout Hidden Widgets", "Hidden Widgets"))
                                </a>
                            </h4>
                        </div>
                        <div id="collapse-@layoutArea.Id.ToString()" class="panel-collapse collapse @(Request.QueryString["layoutAreaId"] == layoutArea.Id.ToString() ? "in" : "")">
                            <div class="panel-body">

                                @if (widgets.Any())
                                {
                                    <table class="table table-striped table-condensed">
                                        <tbody>
                                            @foreach (Widget widget in widgets)
                                            {
                                                <tr>
                                                    <td>
                                                        @(!string.IsNullOrWhiteSpace(widget.Name) ? string.Format("{0} ({1})", widget.Name, widget.WidgetTypeFormatted) : widget.WidgetTypeFormatted)
                                                    </td>
                                                    <td>
                                                        <div class="btn-group">
                                                            @if (widget.Webpage != Model)
                                                            {
                                                                if (!Model.IsHidden(widget))
                                                                {
                                                                    @Html.FormLink(Resource("Admin Webpage Layout Hide Widget", "Hide Widget"), "Hide", "WebpageWidget", new { id = Model.Id, widgetId = widget.Id, layoutAreaId = layoutArea.Id }, new { @class = "btn btn-default btn-xs" })
                                                                }
                                                                else
                                                                {
                                                                    @Html.FormLink(Resource("Admin Webpage Layout Show Widget", "Show Widget"), "Show", "WebpageWidget", new { id = Model.Id, widgetId = widget.Id, layoutAreaId = layoutArea.Id }, new { @class = "btn btn-default btn-xs" })
                                                                }
                                                            }
                                                            else
                                                            {
                                                                <a href="@Url.Action("Delete", "Widget", new { id = widget.Id, layoutAreaId = layoutArea.Id })" class="btn btn-xs btn-danger" data-toggle="fb-modal">@Resource("Delete")</a>
                                                            }
                                                            @Html.ActionLink(Resource("Admin Webpage Layout Edit Widget", "Edit Widget"), "Edit", "Widget", new { id = widget.Id, layoutAreaId = layoutArea.Id }, new { @class = "btn btn-default btn-xs", @data_toggle = "fb-modal" })
                                                        </div>
                                                    </td>
                                                </tr>
                                            }
                                        </tbody>
                                    </table>
                                }
                                <div class="btn-group">
                                    <a href="@Url.Action("Add", "Widget", new { pageId = Model.Id, id = layoutArea.Id })" class="btn btn-xs btn-primary" data-toggle="fb-modal">
                                        @Resource("Admin Webpage Layout Add widget to area", "Add widget to area")
                                    </a>
                                    @if (widgets.Any())
                                    {
                                        @Html.ActionLink(Resource("Sort"), "SortWidgetsForPage", "LayoutArea", new { id = layoutArea.Id, pageId = Model.Id }, new { @class = "btn btn-default btn-xs", @data_toggle = "fb-modal" })
                                        if (layoutArea.PageWidgetSorts.Any(sort => sort.Webpage == Model))
                                        {
                                            @Html.FormLink(Resource("Reset Sort"), "ResetSorting", "LayoutArea", new { id = layoutArea.Id, pageId = Model.Id }, new { @class = "btn btn-default btn-xs" })
                                        }
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                }
            }
            else
            {
                @Resource("Admin Webpage Layout Areas None", "No layout areas available.")
            }
        }
    </div>
</div>
