@page "/cityname"

@using ServerManagement.StateStore

@inject NavigationManager NavigationManager
@inject ContainerStorage containerStorage

<h3>City Name</h3>
<br/>

@if (!string.IsNullOrWhiteSpace(errorMessage))
{
  <div class="alert alert-danger">
    @errorMessage
  </div>
}

@if (server != null)
{
  <FieldComponent Label="City name">
    <Control>
      <input type="text" @bind-value="server.City" class="form-control"></input>
    </Control>
  </FieldComponent>
  <br/>
  <button type="button" class="btn btn-primary" @onclick="GoNext">Next</button>
}

@code {
  private Server? server;
  private string? errorMessage;

  [SupplyParameterFromQuery]
  private string? ServerName { get; set; }

  protected override void OnInitialized()
  {
    base.OnInitialized();
  }

  protected override void OnAfterRender(bool firstRender)
  {
    if (firstRender)
    {
      this.server = containerStorage.GetServer();
      StateHasChanged();
    }
  }

  private void GoNext()
  {
    if (server != null)
    {
      if (string.IsNullOrWhiteSpace(server.City))
      {
        this.errorMessage = "City name is required.";
      }
      else
      {
        containerStorage.SetServer(server);
        NavigationManager.NavigateTo($"/serverstatus");
      }
    }
  }
}