@page "/layers/{id:guid}"
@using DiunaBI.UI.Shared.Services
@using DiunaBI.Application.DTOModels
@using MudBlazor
@inject LayerService LayerService
@inject NavigationManager NavigationManager

<MudCard>
    <MudCardHeader>
        <CardHeaderContent>
            <MudText Typo="Typo.h5">Layer Details</MudText>
        </CardHeaderContent>
        <CardHeaderActions>
            <MudButton Variant="Variant.Text" OnClick="Export">Export</MudButton>
            @if (layer != null && layer.Type == LayerType.Administration)
            {
                <MudButton Variant="Variant.Text" Href="@($"/layers/edit/{layer.Id}/duplicate")">Duplicate</MudButton>
                <MudButton Variant="Variant.Text" Href="@($"/layers/edit/{layer.Id}")">Edit</MudButton>
            }
            @if (layer != null && layer.Type == LayerType.Processed)
            {
                <MudButton Variant="Variant.Text" OnClick="ProcessLayer">Process Layer</MudButton>
            }
        </CardHeaderActions>
    </MudCardHeader>
    <MudCardContent>
        @if (isLoading)
        {
            <MudProgressLinear Color="Color.Primary" Indeterminate="true" />
        }
        else if (layer == null)
        {
            <MudAlert Severity="Severity.Error">Layer not found</MudAlert>
        }
        else
        {
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="layer.Name"
                                  Label="Name"
                                  Variant="Variant.Outlined"
                                  ReadOnly="true"
                                  FullWidth="true"/>
                </MudItem>
                <MudItem xs="12" md="6">
                    @if (layer.IsCancelled)
                    {
                        <MudAlert Severity="Severity.Warning" Dense="true">
                            This layer is cancelled. Will not be used in any further processing.
                        </MudAlert>
                    }
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField Value="@layer.CreatedAt.ToString("g")"
                                  Label="Created"
                                  Variant="Variant.Outlined"
                                  ReadOnly="true"
                                  FullWidth="true"
                                  Adornment="Adornment.End"
                                  AdornmentText="@(layer.CreatedBy?.Username ?? "")"/>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField Value="@layer.ModifiedAt.ToString("g")"
                                  Label="Modified"
                                  Variant="Variant.Outlined"
                                  ReadOnly="true"
                                  FullWidth="true"
                                  Adornment="Adornment.End"
                                  AdornmentText="@(layer.ModifiedBy?.Username ?? "")"/>
                </MudItem>
            </MudGrid>

            <MudDivider Class="my-4"/>

            <MudTable Items="@records"
                      Dense="true"
                      Striped="true"
                      FixedHeader="true"
                      FixedFooter="true"
                      Height="600px">
                <HeaderContent>
                    <MudTh>Code</MudTh>
                    @foreach (var column in displayedColumns)
                    {
                        <MudTh>@column</MudTh>
                    }
                </HeaderContent>
                <RowTemplate>
                    <MudTd DataLabel="Code">@context.Code</MudTd>
                    @foreach (var column in displayedColumns)
                    {
                        <MudTd DataLabel="@column">@GetRecordValue(context, column)</MudTd>
                    }
                </RowTemplate>
                <FooterContent>
                    <MudTd><b>Value1 sum</b></MudTd>
                    @foreach (var column in displayedColumns)
                    {
                        @if (column == "Value1")
                        {
                            <MudTd><b>@valueSum.ToString("N2")</b></MudTd>
                        }
                        else
                        {
                            <MudTd></MudTd>
                        }
                    }
                </FooterContent>
            </MudTable>
        }
    </MudCardContent>
</MudCard>
