@page "/rent"
@using Models
@using Services
@inject IRentService RentService
@inject NavigationManager NavigationManager

<PageTitle>Rent</PageTitle>

<h1>Rent</h1>

<form @onsubmit="RentCar">
    <InputSelect @bind-Value="_vehicleId">
        @foreach (Vehicle v in _vehicles)
        {
            <option value=@v.Id>@v.Id - @v.Brand - @v.Model</option>
        }
    </InputSelect>

    <InputSelect @bind-Value="_customerId">
        @foreach (Customer c in _customers)
        {
            <option value=@c.Id>@c.Name</option>
        }
    </InputSelect>

    <InputDate @bind-Value="_from"/>

    <InputDate @bind-Value="_thru"/>

    <button type="submit">Submit</button>
</form>

@code {
    private List<Vehicle> _vehicles = new List<Vehicle>();
    private List<Customer> _customers = new List<Customer>();

    private long _vehicleId;
    private long _customerId;
    private DateTime _from = DateTime.Now;
    private DateTime _thru = DateTime.Now;

    protected override void OnInitialized()
    {
        _vehicles = RentService.GetAvailableCars();
        _customers = RentService.GetCustomers();
    }

    private Task RentCar()
    {
        RentService.RentCar(_customerId, _vehicleId, _from, _thru);
        Console.WriteLine("Rented Car");
        NavigationManager.NavigateTo("/");
        return Task.CompletedTask;
    }
}
