@namespace Oqtane.Modules.Admin.Logs
@inherits ModuleBase
@inject ILogService LogService

@if (_logs == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table table-borderless">
        <tr>
            <td>
                <label>Level: </label>
                <select class="form-control" @onchange="(e => LevelChanged(e))">
                    <option value="-">&lt;All Levels&gt;</option>
                    <option value="Trace">Trace</option>
                    <option value="Debug">Debug</option>
                    <option value="Information">Information</option>
                    <option value="Warning">Warning</option>
                    <option value="Error">Error</option>
                    <option value="Critical">Critical</option>
                </select>
            </td>
            <td>
                <label>Function: </label>
                <select class="form-control" @onchange="(e => FunctionChanged(e))">
                    <option value="-">&lt;All Functions&gt;</option>
                    <option value="Create">Create</option>
                    <option value="Read">Read</option>
                    <option value="Update">Update</option>
                    <option value="Delete">Delete</option>
                    <option value="Security">Security</option>
                    <option value="Other">Other</option>
                </select>
            </td>
            <td>
                <label>Rows: </label>
                <select 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>Date</th>
                <th>Level</th>
                <th>Feature</th>
                <th>Function</th>
            </Header>
            <Row>
                <td class="@GetClass(context.Function)"><ActionLink Action="Detail" Parameters="@($"id=" + context.LogId.ToString())" /></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>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("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("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("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("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;
    }
}
