using Mediatek86.Controller;
using projet.modele;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace projet.View
{
    public partial class ModifierAbsence : Form
    {
        private ModifierAbsenceController controller;
        Absence absence;
        Personnel personnel;
        /// <summary>
        /// Méthode d'initialisation du formulaire de modification d'absence.
        /// </summary>
        public ModifierAbsence(Absence absence, Personnel personnel)
        {
            InitializeComponent();
            Init();
            this.absence = absence;
            this.personnel = personnel;
            RemplirDefaut();
        }

        public void Init()
        {
            controller = new ModifierAbsenceController();
            RemplirComboBoxMotif();
        }

        public void RemplirDefaut()
        {
            textBoxDateDebut.Text = absence.DateDebut.ToString("dd-MM-yyyy");
            textBoxDateFin.Text = absence.DateFin.HasValue ? absence.DateFin.Value.ToString("dd-MM-yyyy") : string.Empty;
            int motifId = controller.GetMotifIdByLibelle(absence.IdMotif);
            Motif motif = controller.GetMotifById(motifId);
            //comboBoxMotif.SelectedItem = motif;
        }

        private void btnRetour_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Close();
            GestionAbsence gestionAbsence = new GestionAbsence(personnel);
            gestionAbsence.ShowDialog();
        }

        private void RemplirComboBoxMotif()
        {
            List<Motif> motifs = controller.GetAllMotifs();
            comboBoxMotif.Items.Clear();
            foreach (Motif motif in motifs)
            {
                comboBoxMotif.Items.Add(motif);
            }
        }

        private void buttonModifier_Click(object sender, EventArgs e)
        {
            string NomMotif = comboBoxMotif.SelectedItem != null ? ((Motif)comboBoxMotif.SelectedItem).Libelle : string.Empty;
            Absence newAbsence = new Absence(
                personnel.IdPersonnel,
                DateTime.ParseExact(textBoxDateDebut.Text, "dd-MM-yyyy", null),
                string.IsNullOrEmpty(textBoxDateFin.Text) ? (DateTime?)null : DateTime.ParseExact(textBoxDateFin.Text, "dd-MM-yyyy", null),
                Convert.ToString(controller.GetMotifIdByLibelle(NomMotif))
            );
            controller.ModifierAbsence(newAbsence, personnel);


            this.Hide();
            this.Close();
            GestionAbsence gestionAbsence = new GestionAbsence(personnel);
            gestionAbsence.ShowDialog();
        }
    }
}
