using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ProjetTheAlone.Classes; using ProjetTheAlone.Gestion; using System.Collections.ObjectModel; using System.IO; using ProjetTheAlone.Config; namespace ProjetTheAlone.ViewModel { public class VM_Plat : BasePropriete { #region Données Écran private string chConnexion = Settings2.Default.schCon /*@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\Hugues\Haute Ecole de la ville de Liège\Adrien VAN DAMME - ProjetTheAlone\ProjetTheAlone\ProjetThe.mdf';Integrated Security = True"*/; private int nAjout; private C_T_plat.TypePlat_E typeplat=0; private bool _ActiverUneFiche; public bool ActiverUneFiche { get { return _ActiverUneFiche; } set { AssignerChamp(ref _ActiverUneFiche, value, System.Reflection.MethodBase.GetCurrentMethod().Name); ActiverBcpFiche = !ActiverUneFiche; } } private bool _ActiverBcpFiche; public bool ActiverBcpFiche { get { return _ActiverBcpFiche; } set { AssignerChamp(ref _ActiverBcpFiche, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } private C_T_plat _PlatSelectionnee; public C_T_plat PlatSelectionnee { get { return _PlatSelectionnee; } set { AssignerChamp(ref _PlatSelectionnee, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } #endregion #region Données extérieures private VM_UnPlat _UnPlat; public VM_UnPlat UnPlat { get { return _UnPlat; } set { AssignerChamp(ref _UnPlat, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } private ObservableCollection _BcpPlats = new ObservableCollection(); public ObservableCollection BcpPlats { get { return _BcpPlats; } set { _BcpPlats = value; } } #endregion public VM_Plat() { UnPlat = new VM_UnPlat(); UnPlat.ID = 0; UnPlat.Nom = "Nom du plat"; BcpPlats = ChargerPersonnes(chConnexion); ActiverUneFiche = false; Typeplat = C_T_plat.TypePlat_E.Soupe; cConfirmer = new BaseCommande(Confirmer); cAnnuler = new BaseCommande(Annuler); cAjouter = new BaseCommande(Ajouter); cModifier = new BaseCommande(Modifier); cSupprimer = new BaseCommande(Supprimer); } #region Commandes public BaseCommande cConfirmer { get; set; } public BaseCommande cAnnuler { get; set; } public BaseCommande cAjouter { get; set; } public BaseCommande cModifier { get; set; } public BaseCommande cSupprimer { get; set; } public C_T_plat.TypePlat_E Typeplat { get => typeplat; set =>AssignerChamp(ref typeplat, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } #endregion private ObservableCollection ChargerPersonnes(string chConn) { ObservableCollection rep = new ObservableCollection(); List lTmp = new G_T_plat(chConn).Lire("P_nom"); foreach (C_T_plat Tmp in lTmp) rep.Add(Tmp); return rep; } public void Confirmer() { if (nAjout == -1) { UnPlat.ID = new G_T_plat(chConnexion).Ajouter(UnPlat.Nom, UnPlat.Img,(int)UnPlat.TypePlat); BcpPlats.Add(new C_T_plat(UnPlat.ID, UnPlat.Nom, UnPlat.Img, (int)UnPlat.TypePlat)); } else { new G_T_plat(chConnexion).Modifier(UnPlat.ID, UnPlat.Nom, UnPlat.Img, (int)UnPlat.TypePlat); BcpPlats[nAjout] = new C_T_plat(UnPlat.ID, UnPlat.Nom, UnPlat.Img, (int)UnPlat.TypePlat); } ActiverUneFiche = false; } public void Annuler() { ActiverUneFiche = false; } public void Ajouter() { UnPlat = new VM_UnPlat(); nAjout = -1; ActiverUneFiche = true; } public void Modifier() { if (PlatSelectionnee != null) { C_T_plat Tmp = new G_T_plat(chConnexion).Lire_ID(PlatSelectionnee.ID_plat); UnPlat = new VM_UnPlat(); UnPlat.ID = Tmp.ID_plat; UnPlat.Nom = Tmp.P_nom; UnPlat.Img = Tmp.P_img; nAjout = BcpPlats.IndexOf(PlatSelectionnee); ActiverUneFiche = true; } } public void Supprimer() { if (PlatSelectionnee != null) { new G_T_plat(chConnexion).Supprimer(PlatSelectionnee.ID_plat); BcpPlats.Remove(PlatSelectionnee); } } } public class VM_UnPlat : BasePropriete { private int _ID; Classes.C_T_plat.TypePlat_E _TypePlat; private string _Nom; private byte?[] _Img; public int ID { get { return _ID; } set { AssignerChamp(ref _ID, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } public string Nom { get { return _Nom; } set { AssignerChamp(ref _Nom, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } public byte?[] Img { get { return _Img; } set { AssignerChamp (ref _Img, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } public Classes.C_T_plat.TypePlat_E TypePlat { get { return _TypePlat; } set { AssignerChamp(ref _TypePlat, value, System.Reflection.MethodBase.GetCurrentMethod().Name); } } } }