@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inherits DbContextPage

<RadzenCard Variant="Variant.Outlined" class="rz-my-4">
    <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center">
        <RadzenCheckBox @bind-Value=@allowCompositeDataCells Name="CheckBox1" TValue="bool" Change=@(args => grid.Reload())/>
        <RadzenLabel Text="Allow composite data cells" Component="CheckBox1" />
    </RadzenStack>
    <RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center" Style="margin-top:20px">
        <div>Filter Mode:</div>
        <RadzenDropDown @bind-Value="@filterMode" TextProperty="Text" ValueProperty="Value"
                         Data="@(Enum.GetValues(typeof(FilterMode)).Cast<FilterMode>().Select(t => new { Text = $"{t}", Value = t }))" Size="ButtonSize.Small" />
    </RadzenStack>
</RadzenCard>

<RadzenDataGrid @ref="grid" AllowGrouping=true AllowCompositeDataCells="@allowCompositeDataCells" AllowSorting="true" AllowFiltering="true" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterMode="@filterMode" PageSize="5" AllowPaging="true"
                Data="@employees" ColumnWidth="300px" LogicalFilterOperator="LogicalFilterOperator.Or" Style="height:420px" HideGroupedColumn="false" >
    <Columns>
        <RadzenDataGridColumn Property="@nameof(Employee.EmployeeID)" Filterable="false" Title="ID" Frozen="true" Width="80px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn Title="Photo" Frozen="true" Sortable="false" Filterable="false" Width="80px" TextAlign="TextAlign.Center" >
            <Template Context="data">
                <RadzenImage Path="@data.Photo" class="rz-gravatar" AlternateText="@(data.FirstName + " " + data.LastName)" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Title="Title" Property="@nameof(Employee.TitleOfCourtesy)" Filterable=@allowCompositeDataCells Sortable=@allowCompositeDataCells Width="820px">
            <Columns>
                <RadzenDataGridColumn Property="@nameof(Employee.FirstName)" Title="FirstName" Width="480px" Filterable=@allowCompositeDataCells Sortable=@allowCompositeDataCells>
                    <Columns>
                        <RadzenDataGridColumn Property="@nameof(Employee.Title)" Title="Job Title" Width="200px" />
                        <RadzenDataGridColumn Property="@nameof(Employee.BirthDate)" Title="Birth Date" FormatString="{0:d}" Width="140px" />
                        <RadzenDataGridColumn Property="@nameof(Employee.HireDate)" Title="Hire Date" FormatString="{0:d}" Width="140px" />
                    </Columns>
                </RadzenDataGridColumn>
                <RadzenDataGridColumn Property="@nameof(Employee.LastName)" Title="LastName" Width="200px" Filterable=@allowCompositeDataCells Sortable=@allowCompositeDataCells>
                    <Columns>
                        <RadzenDataGridColumn Property="@nameof(Employee.Address)" Title="Address" Width="200px" />
                    </Columns>
                </RadzenDataGridColumn>
                <RadzenDataGridColumn Property="@nameof(Employee.City)" Title="City" Width="140px" />
            </Columns>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="@nameof(Employee.Region)" Title="Region" Width="140px" />
        <RadzenDataGridColumn Property="@nameof(Employee.PostalCode)" Title="Postal Code" Width="140px" />
        <RadzenDataGridColumn Property="@nameof(Employee.Country)" Title="Country" Width="140px" />
        <RadzenDataGridColumn Property="@nameof(Employee.HomePhone)" Title="Home Phone" Width="140px" />
        <RadzenDataGridColumn Property="@nameof(Employee.Extension)" Title="Extension" Width="140px" />
        <RadzenDataGridColumn Property="@nameof(Employee.Notes)" Title="Notes" Width="300px" />
    </Columns>
</RadzenDataGrid>

@code {
    FilterMode filterMode = FilterMode.Advanced;
    bool allowCompositeDataCells = false;
    RadzenDataGrid<Employee> grid;
    IEnumerable<Employee> employees;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        employees = dbContext.Employees;
    }
}