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

@inject TicketClientService TicketClientService
@inject Repository<Ticket> TicketRepository
@inject SmartTranslateService SmartTranslateService
@inject IdentityService IdentityService
@inject Repository<WebSpace> WebSpaceRepository
@inject Repository<Domain> DomainRepository
@inject Repository<Server> ServerRepository
@inject NavigationManager NavigationManager

<div class="row">
    <div class="col">
        <div class="card">
            <div class="card-header">
                <span class="card-title">
                    <TL>Create a new ticket</TL>
                </span>
            </div>
            <div class="card-body">
                <LazyLoader Load="LoadTicketCreate">
                    <SmartForm Model="Model" OnValidSubmit="OnValidSubmit">
                        <div class="mb-3">
                            <InputText @bind-Value="Model.IssueTopic"
                                       placeholder="@(SmartTranslateService.Translate("Enter a title for your ticket"))"
                                       class="form-control">
                            </InputText>
                        </div>
                        <div class="mb-3">
                            <InputTextArea @bind-Value="Model.IssueDescription"
                                           placeholder="@(SmartTranslateService.Translate("Describe the issue you are experiencing"))"
                                           class="form-control">
                            </InputTextArea>
                        </div>
                        <div class="mb-3">
                            <InputTextArea @bind-Value="Model.IssueTries"
                                           placeholder="@(SmartTranslateService.Translate("Describe what you have tried to solve this issue"))"
                                           class="form-control">
                            </InputTextArea>
                        </div>
                        <div class="mb-3">
                            <select @bind="Model.Subject" class="form-select">
                                @foreach (var subject in (TicketSubject[])Enum.GetValues(typeof(TicketSubject)))
                                {
                                    if (Model.Subject == subject)
                                    {
                                        <option value="@(subject)" selected="">@(subject)</option>
                                    }
                                    else
                                    {
                                        <option value="@(subject)">@(subject)</option>
                                    }
                                }
                            </select>
                        </div>
                        <div class="mb-3">
                            @if (Model.Subject == TicketSubject.Domain)
                            {
                                <select @bind="Model.SubjectId" class="form-select">
                                    @foreach (var domain in Domains)
                                    {
                                        if (Model.SubjectId == domain.Id)
                                        {
                                            <option value="@(domain.Id)" selected="">@(domain.Name).@(domain.SharedDomain.Name)</option>
                                        }
                                        else
                                        {
                                            <option value="@(domain.Id)">@(domain.Name).@(domain.SharedDomain.Name)</option>
                                        }
                                    }
                                </select>
                            }
                            else if (Model.Subject == TicketSubject.Server)
                            {
                                <select @bind="Model.SubjectId" class="form-select">
                                    @foreach (var server in Servers)
                                    {
                                        if (Model.SubjectId == server.Id)
                                        {
                                            <option value="@(server.Id)" selected="">@(server.Name)</option>
                                        }
                                        else
                                        {
                                            <option value="@(server.Id)">@(server.Name)</option>
                                        }
                                    }
                                </select>
                            }
                            else if (Model.Subject == TicketSubject.Webspace)
                            {
                                <select @bind="Model.SubjectId" class="form-select">
                                    @foreach (var webSpace in WebSpaces)
                                    {
                                        if (Model.SubjectId == webSpace.Id)
                                        {
                                            <option value="@(webSpace.Id)" selected="">@(webSpace.Domain)</option>
                                        }
                                        else
                                        {
                                            <option value="@(webSpace.Id)">@(webSpace.Domain)</option>
                                        }
                                    }
                                </select>
                            }
                        </div>
                        <div class="text-end">
                            <button class="btn btn-primary" type="submit">
                                <TL>Create ticket</TL>
                            </button>
                        </div>
                    </SmartForm>
                </LazyLoader>
            </div>
        </div>
    </div>
    <div class="col">
        <div class="card">
            <div class="card-header">
                <span class="card-title">
                    <TL>Your tickets</TL>
                </span>
            </div>
            <div class="card-body">
                <LazyLoader Load="LoadTickets">
                    <div class="table-responsive">
                        <Table TableItem="Ticket" Items="Tickets" 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("Ticket title"))" Field="@(x => x.IssueTopic)" Filterable="true" Sortable="false">
                                <Template>
                                    <a href="/support/view/@(context.Id)">@(context.IssueTopic)</a>
                                </Template>
                            </Column>
                            <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("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>
                            <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                        </Table>
                    </div>
                </LazyLoader>
            </div>
        </div>
    </div>
</div>

@code
{
    private Ticket[] Tickets;
    private CreateTicketDataModel Model = new();
    
    private Server[] Servers;
    private WebSpace[] WebSpaces;
    private Domain[] Domains;

    private Task LoadTickets(LazyLoader _)
    {
        Tickets = TicketRepository
            .Get()
            .Include(x => x.CreatedBy)
            .Include(x => x.AssignedTo)
            .Where(x => x.Status != TicketStatus.Closed)
            .Where(x => x.CreatedBy.Id == IdentityService.User.Id)
            .ToArray();

        return Task.CompletedTask;
    }
    
    private Task LoadTicketCreate(LazyLoader _)
    {
        Servers = ServerRepository
            .Get()
            .Include(x => x.Owner)
            .Where(x => x.Owner.Id == IdentityService.User.Id)
            .ToArray();

        WebSpaces = WebSpaceRepository
            .Get()
            .Include(x => x.Owner)
            .Where(x => x.Owner.Id == IdentityService.User.Id)
            .ToArray();

        Domains = DomainRepository
            .Get()
            .Include(x => x.SharedDomain)
            .Include(x => x.Owner)
            .Where(x => x.Owner.Id == IdentityService.User.Id)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task OnValidSubmit()
    {
        var ticket = await TicketClientService.Create(
            Model.IssueTopic,
            Model.IssueDescription,
            Model.IssueTries,
            Model.Subject,
            Model.SubjectId
        );

        Model = new();

        NavigationManager.NavigateTo("/support/view/" + ticket.Id);
    }
}