@page "/admin/support"
@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Misc
@using Microsoft.EntityFrameworkCore
@using BlazorTable
@using Moonlight.App.Helpers
@using Moonlight.App.Services
@using Moonlight.App.Services.Sessions

@inject Repository<Ticket> TicketRepository
@inject SmartTranslateService SmartTranslateService
@inject IdentityService IdentityService

@attribute [PermissionRequired(nameof(Permissions.AdminSupport))]

<div class="row mb-5">
    <LazyLoader Load="LoadStatistics">
        <div class="col-12 col-lg-6 col-xl">
            <div class="mt-4 card">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Total Tickets</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(TotalTicketCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-0">
                                <i class="text-primary bx bx-purchase-tag bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <div class="mt-4 card">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Unassigned tickets</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(UnAssignedTicketCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-0">
                                <i class="text-primary bx bxs-bell-ring bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <div class="mt-4 card">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Pending tickets</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(PendingTicketCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-">
                                <i class="text-primary bx bx-hourglass bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-lg-6 col-xl">
            <div class="mt-4 card">
                <div class="card-body">
                    <div class="row align-items-center gx-0">
                        <div class="col">
                            <h6 class="text-uppercase text-muted mb-2">
                                <TL>Closed tickets</TL>
                            </h6>
                            <span class="h2 mb-0">
                                @(ClosedTicketCount)
                            </span>
                        </div>
                        <div class="col-auto">
                            <span class="h2 text-muted mb-">
                                <i class="text-primary bx bx-lock bx-lg"></i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </LazyLoader>
</div>

<div class="card">
    <div class="card-header">
        <span class="card-title">
            <TL>Ticket overview</TL>
        </span>
        <div class="card-toolbar">
            <div class="btn-group">
                <WButton Text="@(SmartTranslateService.Translate("Overview"))" CssClasses="btn-secondary" OnClick="() => UpdateFilter(0)" />
                <WButton Text="@(SmartTranslateService.Translate("Unassigned tickets"))" CssClasses="btn-secondary" OnClick="() => UpdateFilter(1)" />
                <WButton Text="@(SmartTranslateService.Translate("My tickets"))" CssClasses="btn-secondary" OnClick="() => UpdateFilter(2)" />
                <WButton Text="@(SmartTranslateService.Translate("All tickets"))" CssClasses="btn-secondary" OnClick="() => UpdateFilter(3)" />
            </div>
        </div>
    </div>
    <div class="card-body">
        <LazyLoader @ref="TicketLazyLoader" Load="LoadTickets">
            <div class="table-responsive">
                <Table TableItem="Ticket" Items="AllTickets" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Filterable="true" Sortable="true"/>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Assigned to"))" Field="@(x => x.AssignedTo)" Filterable="true" Sortable="true">
                        <Template>
                            <span>@(context.AssignedTo == null ? "None" : context.AssignedTo.FirstName + " " + context.AssignedTo.LastName)</span>
                        </Template>
                    </Column>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Ticket title"))" Field="@(x => x.IssueTopic)" Filterable="true" Sortable="false"/>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("User"))" Field="@(x => x.CreatedBy)" Filterable="true" Sortable="true">
                        <Template>
                            <span>@(context.CreatedBy.FirstName) @(context.CreatedBy.LastName)</span>
                        </Template>
                    </Column>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Created at"))" Field="@(x => x.CreatedAt)" Filterable="true" Sortable="true">
                        <Template>
                            <span>@(Formatter.FormatDate(context.CreatedAt))</span>
                        </Template>
                    </Column>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Priority"))" Field="@(x => x.Priority)" Filterable="true" Sortable="true">
                        <Template>
                            @switch (context.Priority)
                            {
                                case TicketPriority.Low:
                                    <span class="badge bg-success">@(context.Priority)</span>
                                    break;
                                case TicketPriority.Medium:
                                    <span class="badge bg-primary">@(context.Priority)</span>
                                    break;
                                case TicketPriority.High:
                                    <span class="badge bg-warning">@(context.Priority)</span>
                                    break;
                                case TicketPriority.Critical:
                                    <span class="badge bg-danger">@(context.Priority)</span>
                                    break;
                            }
                        </Template>
                    </Column>
                    <Column TableItem="Ticket" Title="@(SmartTranslateService.Translate("Status"))" Field="@(x => x.Status)" Filterable="true" Sortable="true">
                        <Template>
                            @switch (context.Status)
                            {
                                case TicketStatus.Closed:
                                    <span class="badge bg-danger">@(context.Status)</span>
                                    break;
                                case TicketStatus.Open:
                                    <span class="badge bg-success">@(context.Status)</span>
                                    break;
                                case TicketStatus.Pending:
                                    <span class="badge bg-warning">@(context.Status)</span>
                                    break;
                                case TicketStatus.WaitingForUser:
                                    <span class="badge bg-primary">@(context.Status)</span>
                                    break;
                            }
                        </Template>
                    </Column>
                    <Column TableItem="Ticket" Title="" Field="@(x => x.Id)" Filterable="false" Sortable="false">
                        <Template>
                            <a class="btn btn-sm btn-primary" href="/admin/support/view/@(context.Id)">
                                <TL>Open</TL>
                            </a>
                        </Template>
                    </Column>
                    <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                </Table>
            </div>
        </LazyLoader>
    </div>
</div>

@code
{
    private int TotalTicketCount;
    private int UnAssignedTicketCount;
    private int PendingTicketCount;
    private int ClosedTicketCount;

    private Ticket[] AllTickets;
    private int Filter = 0;

    private LazyLoader TicketLazyLoader;

    private Task LoadStatistics(LazyLoader _)
    {
        TotalTicketCount = TicketRepository
            .Get()
            .Count();

        UnAssignedTicketCount = TicketRepository
            .Get()
            .Include(x => x.AssignedTo)
            .Where(x => x.Status != TicketStatus.Closed)
            .Count(x => x.AssignedTo == null);

        PendingTicketCount = TicketRepository
            .Get()
            .Include(x => x.AssignedTo)
            .Where(x => x.AssignedTo != null)
            .Count(x => x.Status != TicketStatus.Closed);

        ClosedTicketCount = TicketRepository
            .Get()
            .Count(x => x.Status == TicketStatus.Closed);

        return Task.CompletedTask;
    }

    private Task LoadTickets(LazyLoader _)
    {
        switch (Filter)
        {
            default:
                AllTickets = TicketRepository
                    .Get()
                    .Include(x => x.CreatedBy)
                    .Include(x => x.AssignedTo)
                    .Where(x => x.Status != TicketStatus.Closed)
                    .ToArray();
                break;
            case 1:
                AllTickets = TicketRepository
                    .Get()
                    .Include(x => x.CreatedBy)
                    .Include(x => x.AssignedTo)
                    .Where(x => x.AssignedTo == null)
                    .Where(x => x.Status != TicketStatus.Closed)
                    .ToArray();
                break;
                case 2:
                    AllTickets = TicketRepository
                        .Get()
                        .Include(x => x.CreatedBy)
                        .Include(x => x.AssignedTo)
                        .Where(x => x.AssignedTo != null)
                        .Where(x => x.AssignedTo!.Id == IdentityService.User.Id)
                        .Where(x => x.Status != TicketStatus.Closed)
                        .ToArray();
                    break;
            case 3:
                AllTickets = TicketRepository
                    .Get()
                    .Include(x => x.CreatedBy)
                    .Include(x => x.AssignedTo)
                    .ToArray();
                break;
        }
        
        return Task.CompletedTask;
    }

    private async Task UpdateFilter(int filterId)
    {
        Filter = filterId;
        await TicketLazyLoader.Reload();
    }
}