@using GridCore.Resources
@using GridMvc
@using GridMvc.Demo.Models
@using GridShared
@using GridShared.Filtering
@using GridShared.Sorting
@using Microsoft.AspNetCore.Localization
@model SGrid<Order>

@{
    string returnUrl = (string)ViewData["returnUrl"];

    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var locale = requestCulture.RequestCulture.UICulture.TwoLetterISOLanguageName;
    SharedResource.Culture = requestCulture.RequestCulture.UICulture;

    IEnumerable<SelectItem> shipperList = (IEnumerable<SelectItem>)ViewData["shipperList"];

    Action<IGridColumnCollection<Order>> columns = c =>
    {
/* Adding not mapped column, that renders body, using inline Razor html helper */
        c.Add()
            .Encoded(false)
            .Sanitized(false)
            .SetWidth(60)
            .Css("hidden-xs") //hide on phones
            .RenderValueAs(o => $"<b><a class='modal_link' href='/Home/Edit/{o.OrderID}?returnUrl={returnUrl}&gridState={c.Grid.GetState()}'>Edit</a></b>");

/* Adding "OrderID" column: */

        c.Add(o => o.OrderID)
            .Titled(SharedResource.Number)
            .SetWidth(100)
            .Sum(true);

/* Adding "OrderDate" column: */
        c.Add(o => o.OrderDate, "OrderCustomDate")
            .Titled(SharedResource.OrderCustomDate)
            .SortInitialDirection(GridSortDirection.Descending)
            .ThenSortByDescending(o => o.OrderID)
            .SetCellCssClassesContraint(o => o.OrderDate.HasValue && o.OrderDate.Value >= DateTime.Parse("1997-01-01") ? "red" : "")
            .Format("{0:yyyy-MM-dd}")
            .SetWidth(110)
            .Max(true).Min(true);

/* Adding "ShipVia" column: */
        c.Add(o => o.ShipVia)
                    .Titled("Via")
                    .SetWidth(250)
                    .RenderValueAs(o => o.Shipper?.CompanyName)
                    .SetListFilter(shipperList);

/* Adding "CompanyName" column: */
        c.Add(o => o.Customer.CompanyName)
            .Titled(SharedResource.CompanyName)
            .SetWidth(250)
            .SetInitialFilter(GridFilterType.StartsWith, "a")
            .SetFilterWidgetType("CustomCompanyNameFilterWidget")
            .Max(true).Min(true);

/* Adding "ContactName" column: */
        c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250)
            .Max(true).Min(true);

/* Adding "Freight" column: */
        c.Add(o => o.Freight)
            .Titled(SharedResource.Freight)
            .SetWidth(100)
            .Format("{0:F}")
            .Sum(true).Average(true).Max(true).Min(true)
            .Calculate("Average 2", x => x.Grid.ItemsCount == 0 ? 0 : x.Get("Freight").SumValue.Number / x.Grid.ItemsCount)
            .Calculate("Average 3", x => x.Get("OrderID").SumValue.Number == 0 ? 0 : x.Get("Freight").SumValue.Number / x.Get("OrderID").SumValue.Number);

/* Adding "Vip customer" column: */
        c.Add(o => o.Customer.IsVip)
            .Titled(SharedResource.IsVip)
            .SetWidth(80)
            .Css("hidden-xs") //hide on phones
            .RenderValueAs(o => o.Customer.IsVip ? Strings.BoolTrueLabel : Strings.BoolFalseLabel);

/* Adding "OrderDetails" column: */
        c.Add(o => o.OrderDetails.Count)
            .Titled("Details");
    };
}

@await Html.Grid(Model).Named("ordersGrid").Columns(columns).SetRowCssClasses(item => item.Customer.IsVip ? "success" : string.Empty).WithPaging(10).SetLanguage(locale).Sortable().Filterable().WithMultipleFilters().Searchable(true, false).Groupable(true).ClearFiltersButton(true).Selectable(true).SetStriped(true).ChangePageSize(true).WithGridItemsCount().SubGrid("OrderID").RenderAsync()
