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

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

@inject Repository<CloudPanel> CloudPanelRepository
@inject NavigationManager NavigationManager

<OnlyAdmin>
    <LazyLoader Load="Load">
        @if (CloudPanel == 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>Cloud panel not found</TL>
                        </h1>
                        <p class="card-text fs-4">
                            <TL>A cloud panel 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>Host</TL>
                    </label>
                    <div class="input-group mb-5">
                        <InputText @bind-Value="Model.Host" 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 CloudPanel? CloudPanel;

    private CloudPanelDataModel Model = new();

    private Task OnValidSubmit()
    {
    // Apply changes by mapping values using the override feature
        CloudPanel = Mapper.Map(CloudPanel!, Model);

        CloudPanelRepository.Update(CloudPanel);

        NavigationManager.NavigateTo("/admin/webspaces/servers");

        return Task.CompletedTask;
    }

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

        if (CloudPanel != null)
        {
            Model = Mapper.Map<CloudPanelDataModel>(CloudPanel);
        }

        return Task.CompletedTask;
    }
}