@namespace Connected.Components
@using Connected.Utilities;
@inherits TableBase
@typeparam T
<div @attributes="CustomAttributes" class="@Classname">
@if (Items != null || ServerData != null)
{
    <CascadingValue Value="TableContext" IsFixed="true">
        @if (ToolBarContent != null)
        {
            <ToolBar Class="table-toolbar">
                @ToolBarContent
            </ToolBar>
        }
        @if (Context.SortLabels.Any())
        {
            <div Class="table-smalldevices-sortselect">
                <CascadingValue Name="SubscribeToParentForm" Value="false" IsFixed>
                    <Select Value="@Context.CurrentSortLabel" Label="@SortLabel" ValueChanged="@(v => Context.SetSortFunc(v, override_direction_none: true))" T="TableSortLabel<T>">
                        @foreach (var label in Context.SortLabels)
                        {
                            <SelectItem Value="@label">@label.ChildContent</SelectItem>
                        }
                    </Select>
                </CascadingValue>
            </div>
        }
        <div class="table-container" style="@TableStyle @(GetHorizontalScrollbarStyle())">
            <table class="table-root">
                @if (ColGroup != null)
                {
                    <colgroup>
                        @ColGroup
                    </colgroup>
                }
                @if (HeaderContent != null || Columns != null)
                {
                    <thead class="@HeadClassname">
                        @if (CustomHeader)
                        {
                            @HeaderContent
                        }
                        else
                        {
                            <THeadRow IsCheckable="MultiSelection" IsExpandable="@(GroupBy?.Expandable ?? false)">
                                @if (HeaderContent != null)
                                {
                                    @HeaderContent
                                }
                                
                                @if (Columns != null)
                                {
                                    <CascadingValue Value="ColumnBase.Rendermode.Header" Name="Mode">
                                        @Columns(Def)
                                    </CascadingValue>
                                }
                            </THeadRow>
                        }
                    </thead>
                }
                <tbody class="table-body">
                    @if (Loading)
                    {
                        <tr>
                            <td colspan="1000" class="@(CurrentPageItems.Count() > 0 || LoadingContent != null ? "table-loading" : "")">
                                <ProgressLinear Color="@LoadingProgressColor" Class="table-loading-progress" Indeterminate="true" />
                            </td>
                        </tr>
                    }
                    @if(GroupBy != null)
                    {
                        @foreach (var group in GroupItemsPage)
                        {
                            <TableGroupRow GroupDefinition="GroupBy" Items="group" IsCheckable="MultiSelection" 
                                              HeaderClass="@GroupHeaderClass" HeaderStyle="@GroupHeaderStyle" 
                                              FooterClass="@GroupFooterClass" FooterStyle="@GroupFooterStyle"
                                              HeaderTemplate="@GroupHeaderTemplate" FooterTemplate="@GroupFooterTemplate" T="T"  />
                        }
                    }
                    else
                    {
                        @if (CurrentPageItems != null && CurrentPageItems.Count() > 0)
                        {
                            var rowIndex = 0;
                            <Virtualize IsEnabled="@Virtualize" Items="@CurrentPageItems?.ToList()" Context="item">
                               @{ var rowClass = new CssBuilder(RowClass).AddClass(RowClassFunc?.Invoke(item, rowIndex)).Build(); }
                               @{ var rowStyle = new StyleBuilder().AddStyle(RowStyle).AddStyle(RowStyleFunc?.Invoke(item, rowIndex)).Build(); }
                               <Tr Class="@rowClass" Style="@rowStyle" Item="item" @key="item" IsCheckable="MultiSelection" IsEditable="IsEditable"
                                      IsChecked="@(Context.Selection.Contains(item))"
                                      IsCheckedChanged="((value) => { var x = item; OnRowCheckboxChanged(value, x); })">

                                   @if ((!ReadOnly) && IsEditable && object.Equals(_editingItem, item))
                                   {
                                        <CascadingValue Value="@Validator" IsFixed="true">
                                           @if(RowEditingTemplate != null)
										    {
											    @RowEditingTemplate(item)
										    }
										    @if (Columns != null)
										    {
											    <CascadingValue Value="ColumnBase.Rendermode.Edit" Name="Mode">
												    @Columns(item)
											    </CascadingValue>
										    }
                                        </CascadingValue>
                                   }
                                   else
                                   {
                                       if (RowTemplate != null)
										{
											@RowTemplate(item)
										}
										@if (Columns != null)
										{
											<CascadingValue Value="ColumnBase.Rendermode.Item" Name="Mode">
												@Columns(item)
											</CascadingValue>
										}
                                   }
                               </Tr>
                               @if (ChildRowContent != null)
                               {
                                   @ChildRowContent(item)
                               }   
                               @{ rowIndex++; }
                            </Virtualize>
                        }
                    }
                    @if(!(CurrentPageItems != null && CurrentPageItems.Count() > 0)
                        && (Loading ? LoadingContent != null : NoRecordsContent != null)
                    )
                    {
                        <tr>
                            <th colspan="1000" class="table-empty-row">
                                <div Class="my-3">
                                    @if(Loading)
                                    {
                                        @LoadingContent
                                    }
                                    else
                                    {
                                        @NoRecordsContent
                                    }
                                </div>
                            </th>
                        </tr>
                    }
                </tbody>
                @if (FooterContent != null || Columns != null)
                {
                    <tfoot class="@FootClassname">
                        @if (CustomFooter)
                        {
                            @FooterContent
                        }
                        else
                        {
                            <TFootRow IsCheckable="MultiSelection" IsExpandable="@(GroupBy?.Expandable ?? false)">
                                @if (@FooterContent != null)
                                {
                                    @FooterContent
                                }
                                @if (Columns != null)
                                {
                                    <CascadingValue Value="ColumnBase.Rendermode.Footer" Name="Mode">
                                        @Columns(Def)
                                    </CascadingValue>
                                }
                            </TFootRow>
                        }
                    </tfoot>
                }
            </table>
        </div>
        @if (PagerContent != null)
        {
            <div class="table-pagination">
                @PagerContent
            </div>
        }
    </CascadingValue>
}
</div>
@code
{
    // moved to code to avoid repeatedly code on markup block
    internal RenderFragment RenderRows(IEnumerable<T> source, string customClass = null, bool isExpandable = false)
    {
        var rowIndex = 0;

         RenderFragment rootNode =
            @<text>
                <Virtualize IsEnabled="@Virtualize" Items="@source?.ToList()" Context="item" ChildContent="@child()">              
                </Virtualize>
            </text>;

         RenderFragment<T> child() => item =>
             @<text>
             @{
                var rowClass = new CssBuilder(RowClass).AddClass(RowClassFunc?.Invoke(item, rowIndex)).AddClass(customClass, ! string.IsNullOrEmpty("table-row-group-indented-1")).Build();
                var rowStyle = new StyleBuilder().AddStyle(RowStyle).AddStyle(RowStyleFunc?.Invoke(item, rowIndex)).Build();
             } 
             <Tr Class="@rowClass" Style="@rowStyle" Item="item" @key="item" IsCheckable="MultiSelection" IsEditable="IsEditable" IsExpandable="isExpandable"
                        IsCheckedChanged="((value) => { var x = item; OnRowCheckboxChanged(value, x); })">

                    @if ((!ReadOnly) && IsEditable && object.Equals(_editingItem, item))
                    {
                        <CascadingValue Value="@Validator" IsFixed="true">
                            @if(RowEditingTemplate != null)
							{
								@RowEditingTemplate(item)
							}
							@if (Columns != null)
							{
								<CascadingValue Value="ColumnBase.Rendermode.Edit" Name="Mode">
									@Columns(item)
								</CascadingValue>
							}
                        </CascadingValue>
                    }
                    else
                    {
                        if (RowTemplate != null)
						{
							@RowTemplate(item)
						}
						@if (Columns != null)
						{
							<CascadingValue Value="ColumnBase.Rendermode.Item" Name="Mode">
								@Columns(item)
							</CascadingValue>
						}
                    }
                </Tr>
                @if (ChildRowContent != null)
                {
                    @ChildRowContent(item)
                }
                @{rowIndex++;}
        </text>
    ;


        return rootNode;
    }
}