@namespace Oqtane.Modules.Admin.Logs
@inherits ModuleBase
@inject ILogService LogService
@inject IStringLocalizer<Index> Localizer 

@if (_logs == null)
{
    <p><em>@Localizer["Loading..."]</em></p>
}
else
{
    <table class="table table-borderless">
        <tr>
            <td>
                <Label For="level" HelpText="Select the log level for event log items" ResourceKey="Level">Level: </Label><br /><br />
                <select id="level" class="form-control" @onchange="(e => LevelChanged(e))">
                    <option value="-">&lt;@Localizer["All Levels"]&gt;</option>
                    <option value="Trace">@Localizer["Trace"]</option>
                    <option value="Debug">@Localizer["Debug"]</option>
                    <option value="Information">@Localizer["Information"]</option>
                    <option value="Warning">@Localizer["Warning"]</option>
                    <option value="Error">@Localizer["Error"]</option>
                    <option value="Critical">@Localizer["Critical"]</option>
                </select>
            </td>
            <td>
                <Label For="function" HelpText="Select the function for event log items" ResourceKey="Function">Function: </Label><br /><br />
                <select id="function" class="form-control" @onchange="(e => FunctionChanged(e))">
                    <option value="-">&lt;@Localizer["All Functions"]&gt;</option>
                    <option value="Create">@Localizer["Create"]</option>
                    <option value="Read">@Localizer["Read"]</option>
                    <option value="Update">@Localizer["Update"]</option>
                    <option value="Delete">@Localizer["Delete"]</option>
                    <option value="Security">@Localizer["Security"]</option>
                    <option value="Other">@Localizer["Other"]</option>
                </select>
            </td>
            <td>
                <Label For="rows" HelpText="Select the maximum number of event log items to review. Please note that if you choose more than 10 items the information will be split into pages." ResourceKey="Rows">Maximum Items: </Label><br /><br />
                <select id="rows" class="form-control" @onchange="(e => RowsChanged(e))">
                    <option value="10">10</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
            </td>
        </tr>
    </table>

    @if (_logs.Any())
    {
        <Pager Items="@_logs">
            <Header>
                <th style="width: 1px;">&nbsp;</th>
                <th>@Localizer["Date"]</th>
                <th>@Localizer["Level"]</th>
                <th>@Localizer["Feature"]</th>
                <th>@Localizer["Function"]</th>
            </Header>
            <Row>
                <td class="@GetClass(context.Function)"><ActionLink Action="Detail" Parameters="@($"id=" + context.LogId.ToString())" ResourceKey="LogDetails" /></td>
                <td class="@GetClass(context.Function)">@context.LogDate</td>
                <td class="@GetClass(context.Function)">@context.Level</td>
                <td class="@GetClass(context.Function)">@context.Feature</td>
                <td class="@GetClass(context.Function)">@context.Function</td>
            </Row>
        </Pager>
    }
    else
    {
        <p><em>@Localizer["No Logs Match The Criteria Specified"]</em></p>
    }
}

@code {
    private string _level = "-";
    private string _function = "-";
    private string _rows = "10";
    private List<Log> _logs;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            await GetLogs();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Logs {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Loading Logs"], MessageType.Error);
        }
    }

    private async void LevelChanged(ChangeEventArgs e)
    {
        try
        {
            _level = (string)e.Value;
            await GetLogs();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Logs {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Loading Logs"], MessageType.Error);
        }
    }

    private async void FunctionChanged(ChangeEventArgs e)
    {
        try
        {
            _function = (string)e.Value;
            await GetLogs();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Logs {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Loading Logs"], MessageType.Error);
        }
    }


    private async void RowsChanged(ChangeEventArgs e)
    {
        try
        {
            _rows = (string)e.Value;
            await GetLogs();
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Loading Logs {Error}", ex.Message);
            AddModuleMessage(Localizer["Error Loading Logs"], MessageType.Error);
        }
    }

    private async Task GetLogs()
    {
        _logs = await LogService.GetLogsAsync(PageState.Site.SiteId, ((_level == "-") ? string.Empty : _level), ((_function == "-") ? string.Empty : _function), int.Parse(_rows));
    }

    private string GetClass(string function)
    {
        string classname = string.Empty;
        switch (function)
        {
            case "Create":
                classname = "table-success";
                break;
            case "Read":
                classname = "table-primary";
                break;
            case "Update":
                classname = "table-warning";
                break;
            case "Delete":
                classname = "table-danger";
                break;
            case "Security":
                classname = "table-secondary";
                break;
            default:
                classname = string.Empty;
                break;
        }
        return classname;
    }
}
