@page "/fetchdata"

<PageTitle>Weather forecast</PageTitle>

@using BlazorServerApp.Data
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (_forecasts == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in _forecasts)
            {
                <tr>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>

    <button class="btn btn-primary" @onclick="AddNewForecast">
        Add New Forecast
    </button>

    if (_showPopup)
    {
        <div class="modal" style="display:block" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h3 class="modal-title">Add Forecast</h3>
                        <button type="button" class="btn-close" @onclick="ClosePopup"/>
                    </div>
                    <div class="modal-body">
                        <EditForm Model="_addForecast" OnValidSubmit="SaveForecast">
                            @* Validation 기능 on *@
                            <DataAnnotationsValidator/>
                            @* 에러 메세지 출력 기능 on *@
                            <ValidationSummary/>

                            <label for="TemperatureC">Temperacure(℃)</label>
                            <InputNumber class="form-control" placeholder="Temperacure(℃)" @bind-Value="_addForecast.TemperatureC" />
                            <label for="Summary">Summary</label>
                            <InputText class="form-control" placeholder="Summary" @bind-Value="_addForecast.Summary" />
                            <br />
                            <button class="btn btn-primary" type="submit">Save</button>
                        </EditForm>

                        @* <label for="TemperatureC">Temperacure(℃)</label>
                        <input class="form-control" type="text" placeholder="Temperacure(℃)" @bind="_addForecast.TemperatureC"/>
                        <label for="Summary">Summary</label>
                        <input class="form-control" type="text" placeholder="Summary" @bind="_addForecast.Summary" />
                        <br/>
                        <button class="btn btn-primary" @onclick="SaveForecast">Save</button> *@
                    </div>
                </div>
            </div>
        </div>
    }
}

@code {
    private List<WeatherForecast>? _forecasts;

    private bool _showPopup = false;
    private WeatherForecast _addForecast;

    protected override async Task OnInitializedAsync()
    {
        _forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
    }

    private void AddNewForecast()
    {
        _showPopup = true;
        _addForecast = new WeatherForecast();
    }

    private void ClosePopup()
    {
        _showPopup = false;
    }

    private void SaveForecast()
    {
        _addForecast.Date = DateTime.Now;
        _forecasts.Add(_addForecast);
        _showPopup = false;
    }
}
