using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using HeartTrack.Models;
using HeartTrack.Services;

namespace HeartTrack.Pages
{
    public partial class AddTicket
    {

        [Inject]
        public ILocalStorageService LocalStorage { get; set; }

        [Inject]
        public NavigationManager NavigationManager { get; set; }

        [Inject]
        public IWebHostEnvironment WebHostEnvironment { get; set; }

        /// <summary>
        /// The current item model
        /// </summary>
        public TicketModel ticketModel = new(){};

        private async void HandleValidSubmit()
        {
            // Get the current data
            var currentData = await LocalStorage.GetItemAsync<List<Ticket>>("data");

            // Simulate the Id
            ticketModel.Id = currentData.Max(s => s.Id) + 1;

            // Add the item to the current data
            currentData.Add(new Ticket
            {
                Id = ticketModel.Id,
                Username = ticketModel.Username,
                Nom = ticketModel.Nom,
                Prenom = ticketModel.Prenom,
                Contexte = ticketModel.Contexte,
                Description = ticketModel.Description/*,
                Urgence = ticketModel.Urgence*/
            });

            // Save the data
            await LocalStorage.SetItemAsync("data", currentData);

            NavigationManager.NavigateTo("tickets");
        }

    }
}
