@page "/admin/websites/servers/edit/{Id:int}"

@using Moonlight.App.Models.Forms
@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities

@inject PleskServerRepository PleskServerRepository
@inject NavigationManager NavigationManager

<OnlyAdmin>
    <LazyLoader Load="Load">
        @if (PleskServer == null)
            {
                <div class="d-flex justify-content-center flex-center">
                    <div class="card">
                        <img src="/assets/media/svg/nodata.svg" class="card-img-top w-50 mx-auto pt-5" alt="Not found image"/>
                        <div class="card-body text-center">
                            <h1 class="card-title">
                                <TL>Plesk server not found</TL>
                            </h1>
                            <p class="card-text fs-4">
                                <TL>A plesk server with that id cannot be found</TL>
                            </p>
                        </div>
                    </div>
                </div>
            }
            else
            {
                <div class="card card-body p-10">
                    <SmartForm Model="Model" OnValidSubmit="OnValidSubmit">
                        <label class="form-label">
                            <TL>Name</TL>
                        </label>
                        <div class="input-group mb-5">
                            <InputText @bind-Value="Model.Name" class="form-control"></InputText>
                        </div>
                        <label class="form-label">
                            <TL>Api Url</TL>
                        </label>
                        <div class="input-group mb-5">
                            <InputText @bind-Value="Model.ApiUrl" class="form-control"></InputText>
                        </div>
                        <label class="form-label">
                            <TL>Api Key</TL>
                        </label>
                        <div class="input-group mb-5">
                            <InputText @bind-Value="Model.ApiKey" class="blur-unless-hover form-control"></InputText>
                        </div>
                        <div>
                            <button type="submit" class="btn btn-primary float-end">
                                <TL>Save</TL>
                            </button>
                        </div>
                    </SmartForm>
                </div>
            }
    </LazyLoader>
</OnlyAdmin>

@code
{
    [Parameter]
    public int Id { get; set; }

    private PleskServer? PleskServer;

    private PleskServerDataModel Model = new();

    private Task OnValidSubmit()
    {
        PleskServer!.Name = Model.Name;
        PleskServer.ApiUrl = Model.ApiUrl;
        PleskServer.ApiKey = Model.ApiKey;
        
        PleskServerRepository.Update(PleskServer);
        
        NavigationManager.NavigateTo("/admin/websites/servers");
        
        return Task.CompletedTask;
    }

    private Task Load(LazyLoader arg)
    {
        PleskServer = PleskServerRepository
            .Get()
            .FirstOrDefault(x => x.Id == Id);

        if (PleskServer != null)
        {
            Model.Name = PleskServer.Name;
            Model.ApiUrl = PleskServer.ApiUrl;
            Model.ApiKey = PleskServer.ApiKey;
        }
        
        return Task.CompletedTask;
    }
}
