@using GridCore
@using GridCore.Filtering
@using GridCore.Pagination
@using GridCore.Resources
@using GridShared
@using GridShared.Columns
@using GridShared.Filtering
@using GridShared.Utility
@using GridShared.Pagination
@using Microsoft.AspNetCore.Mvc.ModelBinding
@model ISGrid

@if (Model == null) { return; }

@{
    var columns = Model.Columns.Where(r => r.InitialFilterSettings != ColumnFilterValue.Null).Select(r => r.Name);
    string initialFilters = "";
    if (columns != null)
    {
        initialFilters = string.Join(",", columns);
    }
    string keys = "";
    if (Model.SubGridKeys != null)
    {
        keys = string.Join(",", Model.SubGridKeys);
    }
    var firstColumn = (ITotalsColumn)Model.Columns.FirstOrDefault();
    bool hasSubGrid = Model.SubGridKeys != null && Model.SubGridKeys.Length > 0;
    bool hasTotals = Model.IsSumEnabled || Model.IsAverageEnabled || Model.IsMaxEnabled || Model.IsMinEnabled;
    bool requiredTotalsColumn = hasTotals
              && firstColumn != null
              && (firstColumn.IsSumEnabled || firstColumn.IsAverageEnabled
                  || firstColumn.IsMaxEnabled || firstColumn.IsMinEnabled);

    //determine current url:
    var queryBuilder = new CustomQueryStringBuilder(Model.Settings.SearchSettings.Query);
    var exceptQueryParameters = new List<string> { GridPager.DefaultPageSizeQueryParameter };
    string _changePageSizeUrl = queryBuilder.GetQueryStringExcept(exceptQueryParameters);
    exceptQueryParameters = new List<string> { GridPager.DefaultPageQueryParameter };
    string _goToUrl = queryBuilder.GetQueryStringExcept(exceptQueryParameters);
    var pagerViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
    pagerViewData.Add("changePageSizeUrl", _changePageSizeUrl);
    pagerViewData.Add("pagingType", Model.PagingType);
    pagerViewData.Add("goToUrl", _goToUrl);
    exceptQueryParameters = new List<string> { QueryStringFilterSettings.DefaultTypeQueryParameter, QueryStringFilterSettings.DefaultClearInitFilterQueryParameter };
    string _removeAllFilters = queryBuilder.GetQueryStringExcept(exceptQueryParameters);
    string gridTableHead = Guid.NewGuid().ToString("N");
    string gridTableBody = Guid.NewGuid().ToString("N");
    string gridTableTotals = Guid.NewGuid().ToString("N");
}

@if (Model.RenderOptions.RenderRowsOnly)
{
    @await Html.PartialAsync("_GridBody", Model)
}
else
{
    <div class="grid-mvc" data-lang="@Model.Language"
         data-gridname="@Model.RenderOptions.GridName"
         data-selectable="@Model.RenderOptions.Selectable.ToString().ToLower()"
         data-extsortable="@Model.ExtSortingEnabled.ToString().ToLower()"
         data-groupable="@Model.GroupingEnabled.ToString().ToLower()"
         data-multiplefilters="@Model.RenderOptions.AllowMultipleFilters.ToString().ToLower()"
         data-initfilters="@initialFilters" @(string.IsNullOrWhiteSpace(keys) ? "" : "data-keys=" + keys)
         dir="@Model.Direction.ToDirection()">
        <div class="grid-wrap">
            <div class="row">
                <div class="col-md-6 grid-header-buttons">
                    @if (Model.ClearFiltersButtonEnabled)
                    {
                        <div class="grid-all-filters-clear">
                            <button class="grid-button grid-button-all-filters-clear btn btn-default btn-primary" title="@Strings.ClearAllFilters" data-all-filters-clear-url="@_removeAllFilters" type="button"></button>
                        </div>
                    }
                </div>
                @if (Model.SearchOptions.Enabled)
                {
                    @await Html.PartialAsync("_GridSearch", Model)
                }
            </div>
            @if (Model.ExtSortingEnabled)
            {
                @await Html.PartialAsync("_GridExtSortHeader", Model)
            }
            @if (Model.TableLayout == TableLayout.Auto)
            {
                <div class="grid-table-wrap">
                    <table class="table @(Model.RenderOptions.Striped && hasSubGrid ? "substriped" : Model.RenderOptions.Striped ? "striped" : "") grid-table">
                        @* Draw grid header *@
                        <thead>
                            <tr>
                                @if (hasSubGrid)
                                {
                                    <th class="grid-header" style="width:20px;"></th>
                                }
                                @if (requiredTotalsColumn)
                                {
                                    <th class="grid-header" style="width:60px;"></th>
                                }
                                @foreach (IGridColumn column in Model.Columns)
                                {
                                    @await Html.PartialAsync("_GridHeader", column)
                                }
                            </tr>
                        </thead>
                        <tbody>
                            @await Html.PartialAsync("_GridBody", Model)
                            @if (hasTotals)
                            {
                                @await Html.PartialAsync("_GridTotals", Model)
                            }
                        </tbody>
                    </table>
                </div>
            }
            else
            {
                <div id="@gridTableHead" class="grid-table-head" style="@(Model.Width == null ? "" : "width:" + Model.Width + ";")">
                    <table class="table grid-table" style="table-layout:fixed;">
                        <colgroup>
                            @if (hasSubGrid)
                            {
                                <col style="width:2em;" />
                            }
                            @if (requiredTotalsColumn)
                            {
                                <col style="width:5em;" />
                            }
                            @foreach (IGridColumn column in Model.Columns)
                            {
                                @await Html.PartialAsync("_GridCol", column)
                            }
                            @if (Model.Height != null)
                            {
                                <col style="width:16px;" />
                            }
                        </colgroup>
                        @* Draw grid header *@
                        <thead>
                            <tr>
                                @if (hasSubGrid)
                                {
                                    <th class="grid-header">
                                        <div class="grid-header-group">
                                            <div class="grid-header-title" data-column="">
                                                <span data-column=""></span>
                                            </div>
                                        </div>
                                    </th>
                                }
                                @if (requiredTotalsColumn)
                                {
                                    <th class="grid-header">
                                        <div class="grid-header-group">
                                            <div class="grid-header-title" data-column="">
                                                <span data-column=""></span>
                                            </div>
                                        </div>
                                    </th>
                                }
                                @foreach (IGridColumn column in Model.Columns)
                                {
                                    @await Html.PartialAsync("_GridHeader", column)
                                }
                                @if (Model.Height != null)
                                {
                                    <th></th>
                                }
                            </tr>
                        </thead>
                        <tbody style="display:none;">
                            <tr>
                                @if (hasSubGrid)
                                {
                                    <td></td>
                                }
                                @if (requiredTotalsColumn)
                                {
                                    <td></td>
                                }
                                @foreach (IGridColumn column in Model.Columns)
                                {
                                    <td></td>
                                }
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div id="@gridTableBody" class="grid-table-body" style="@(Model.Width == null ? "" : "width:" + Model.Width + ";")@(Model.Height == null ? "" : "height:" + Model.Height + ";")">
                    <table class="table grid-table @(Model.RenderOptions.Striped && hasSubGrid ? "substriped" : Model.RenderOptions.Striped ? "striped" : "")" style="table-layout:fixed;">
                        <colgroup>
                            @if (hasSubGrid)
                            {
                                <col style="width:2em;" />
                            }
                            @if (requiredTotalsColumn)
                            {
                                <col style="width:5em;" />
                            }
                            @foreach (IGridColumn column in Model.Columns)
                            {
                                @await Html.PartialAsync("_GridCol", column)
                            }
                        </colgroup>
                        <tbody>
                            @await Html.PartialAsync("_GridBody", Model)
                        </tbody>
                    </table>
                </div>
                @if (hasTotals)
                {
                    <div id="@gridTableTotals" class="grid-table-body" style="@(Model.Width == null ? "" : "width:" + Model.Width + ";")">
                        <table class="table grid-table" style="table-layout:fixed;">
                            <colgroup>
                                @if (hasSubGrid)
                                {
                                    <col style="width:2em;" />
                                }
                                @if (requiredTotalsColumn)
                                {
                                    <col style="width:5em;" />
                                }
                                @foreach (IGridColumn column in Model.Columns)
                                {
                                    @await Html.PartialAsync("_GridCol", column)
                                }
                            </colgroup>
                            <tbody>
                                    @await Html.PartialAsync("_GridTotals", Model)
                            </tbody>
                        </table>
                    </div>
                }
                <script>
                    document.getElementById("@gridTableBody").onscroll = function () {
                        document.getElementById("@gridTableHead").scrollLeft = this.scrollLeft;
                        var elem = document.getElementById("@gridTableTotals");
                        if(elem)
                            elem.scrollLeft = this.scrollLeft;
                    };
                </script>
            }
            <div class="grid-footer">
                @if (Model.PagingType == PagingType.Pagination && Model.Pager != null)
                {
                    @await Html.PartialAsync(Model.Pager.TemplateName, Model.Pager, pagerViewData)
                }
                @if (Model.RenderOptions.ShowGridItemsCount)
                {
                    <div class="grid-itemscount">
                        <div class="grid-itemscount-label">@Model.RenderOptions.GridCountDisplayName:</div>
                        <div class="grid-itemscount-caption"><b>@Model.ItemsCount</b></div>
                    </div>
                }
            </div>
        </div>
    </div>
}
