using MauiAppStock.Models;
using MauiAppStock.Data;

namespace MauiAppStock.Views
{
    public partial class EditAppareilPage : ContentPage
    {
        private Appareil _appareil;
        public EditAppareilPage(Appareil appareil)
        {
            InitializeComponent();
            _appareil = appareil;
            NomEntry.Text = appareil.Nom;
            DescriptionEditor.Text = appareil.Description;
        }

        private async void OnSaveClicked(object sender, EventArgs e)
        {
            _appareil.Nom = NomEntry.Text;
            _appareil.Description = DescriptionEditor.Text;
            await Database.UpdateAppareilAsync(_appareil);
            await Navigation.PopAsync();
        }

        private async void OnDeleteClicked(object sender, EventArgs e)
        {
            bool confirm = await DisplayAlert("Confirmation", "Voulez-vous vraiment supprimer cet appareil ?", "Oui", "Non");
            if (confirm)
            {
                await Database.DeleteAppareilAsync(_appareil);
                await Navigation.PopAsync();
            }
        }
    }
}