@using [Owner].Module.[Module].Services
@using [Owner].Module.[Module].Models

@namespace [Owner].Module.[Module]
@inherits ModuleBase
@inject I[Module]Service [Module]Service
@inject NavigationManager NavigationManager
@inject IStringLocalizer<Index> Localizer

@if (_[Module]s == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <ActionLink Action="Add" Security="SecurityAccessLevel.Edit" Text="Add [Module]" ResourceKey="Add" />
    <br />
    <br />
    @if (@_[Module]s.Count != 0)
    {
        <Pager Items="@_[Module]s">
            <Header>
                <th style="width: 1px;">&nbsp;</th>
                <th style="width: 1px;">&nbsp;</th>
                <th>@Localizer["Name"]</th>
            </Header>
            <Row>
                <td><ActionLink Action="Edit" Parameters="@($"id=" + context.[Module]Id.ToString())" ResourceKey="Edit"  /></td>
                <td><ActionDialog Header="Delete [Module]" Message="Are You Sure You Wish To Delete This [Module]?" Action="Delete" Security="SecurityAccessLevel.Edit" Class="btn btn-danger" OnClick="@(async () => await Delete(context))" ResourceKey="Delete"  /></td>
                <td>@context.Name</td>
            </Row>
        </Pager>
    }
    else
    {
        <p>@Localizer["Message.DisplayNone"]</p>
    }
}

@code {
    public override List<Resource> Resources => new List<Resource>()
    {
        new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" },
        new Resource { ResourceType = ResourceType.Script, Url = ModulePath() + "Module.js" }
    };

    List<[Module]> _[Module]s;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            _[Module]s = await [Module]Service.Get[Module]sAsync(ModuleState.ModuleId);
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading [Module] {Error}", ex.Message);
            AddModuleMessage(Localizer["Message.LoadError"], MessageType.Error);
        }
    }

    private async Task Delete([Module] [Module])
    {
        try
        {
            await [Module]Service.Delete[Module]Async([Module].[Module]Id, ModuleState.ModuleId);
            await logger.LogInformation("[Module] Deleted {[Module]}", [Module]);
            _[Module]s = await [Module]Service.Get[Module]sAsync(ModuleState.ModuleId);
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Deleting [Module] {[Module]} {Error}", [Module], ex.Message);
            AddModuleMessage(Localizer["Message.DeleteError"], MessageType.Error);
        }
    }
}