@inherits ComponentBase
@using Insight.Agent.Enums;

<TableContainer T="AgentLogEntity" 
    @ref="Container" 
    @bind-Search="Search" 
    Title="@Title" 
    Breadcrumbs="@Breadcrumbs" 
    Data="LoadDataAsync" 
    OnFilter="()=>Filter = true" 
    Filtered="Filtered">
    <Header>
        <MudTh>
            <MudTableSortLabel SortLabel="Timestamp" T="AgentLogEntity">
                Timestamp
            </MudTableSortLabel>
        </MudTh>
        <MudTh>
            <MudTableSortLabel SortLabel="Category" T="AgentLogEntity">
                Category
            </MudTableSortLabel>
        </MudTh>
        <MudTh>
            <MudTableSortLabel SortLabel="Status" T="AgentLogEntity">
                Status
            </MudTableSortLabel>
        </MudTh>
        <MudTh Style="width: 400px;">
            <MudTableSortLabel SortLabel="Message" T="AgentLogEntity">
                Message
            </MudTableSortLabel>
        </MudTh>
    </Header>
    <RowTemplate>
        <MudTd DataLabel="Timestamp">
            @if (context?.Timestamp is not null)
            {
                @context?.Timestamp.Value.ToLocalTime().ToString("dd.MM.yyyy HH:mm:ss")
                //@context?.Timestamp
            }
        </MudTd>
        <MudTd DataLabel="Category">
            @context?.Category
        </MudTd>
        <MudTd DataLabel="Status">
            @{
                var color = Color.Inherit;

                if (context?.Status is not null)
                {
                    color = Enum.Parse<Insight.Agent.Enums.StatusEnum>(context.Status, true) switch
                    {
                        Insight.Agent.Enums.StatusEnum.Information => Color.Success,
                        Insight.Agent.Enums.StatusEnum.Warning => Color.Warning,
                        Insight.Agent.Enums.StatusEnum.Error => Color.Error,
                        _ => Color.Inherit
                    };
                }

                <MudText Color="color" Typo="Typo.inherit">
                    @context?.Status
                </MudText>
            }
        </MudTd>
        <MudTd DataLabel="Message">
            @context?.Message
        </MudTd>
    </RowTemplate>
</TableContainer>

<ActionDialog @bind-Visible="Filter">
    <Content>
        <MudDateRangePicker @ref="_picker" @bind-DateRange="FilterDate" Label="Date" PickerVariant="PickerVariant.Static">
                    <PickerActions>
                        <MudButton OnClick="@(() => _picker?.Clear())">
                            Clear
                        </MudButton>
                        <MudButton OnClick="@(() => _picker?.Close())" Color="Color.Primary">
                            Ok
                        </MudButton>
                    </PickerActions>
        </MudDateRangePicker>
        <MudSelect @bind-Value="FilterCategory" Label="Category">
                    <MudSelectItem T="CategoryEnum?" Value="null">
                        None
                    </MudSelectItem>
                    @foreach (CategoryEnum category in Enum.GetValues(typeof(CategoryEnum)))
                    {
                        <MudSelectItem T="CategoryEnum?" Value="@category">
                            @category
                        </MudSelectItem>
                    }
        </MudSelect>
        <MudSelect @bind-Value="FilterStatus" Label="State">
            <MudSelectItem T="StatusEnum?" Value="null">
                None
            </MudSelectItem>
            @foreach (StatusEnum state in Enum.GetValues(typeof(StatusEnum)))
            {
                <MudSelectItem T="StatusEnum?" Value="@state">
                    @state
                </MudSelectItem>
            }
        </MudSelect>
    </Content>
    <Actions>
        <MudButton OnClick="OnFilterReset">
            Reset
        </MudButton>
        <MudButton OnClick="()=>Filter = false" Color="Color.Primary">
            Done
        </MudButton>
    </Actions>
</ActionDialog>