using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Common; using Gestor.Model.Domain.Generic; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace Gestor.Application.ViewModels.Ferramentas { public class SocioViewModel : BaseSegurosViewModel { private readonly SocioServico _servico; public Socio CancelSocio; private ObservableCollection _empresas = new ObservableCollection(); private Empresa _selectedEmpresa; private Socio _selectedSocio = new Socio(); private ObservableCollection _sociosFiltradas = new ObservableCollection(); private bool _isExpanded; private bool _isLoading; public ObservableCollection Empresas { get { return this._empresas; } set { this._empresas = value; base.OnPropertyChanged("Empresas"); } } public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public bool IsLoading { get { return this._isLoading; } set { this._isLoading = value; base.OnPropertyChanged("IsLoading"); } } public Empresa SelectedEmpresa { get { return this._selectedEmpresa; } set { this._selectedEmpresa = value; this.WorkOnSelectedEmpresa(value); base.OnPropertyChanged("SelectedEmpresa"); } } public Socio SelectedSocio { get { return this._selectedSocio; } set { long? nullable; this._selectedSocio = value; this.WorkOnSelectedSocio(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedSocio"); } } public List Socios { get; set; } public ObservableCollection SociosFiltradas { get { return this._sociosFiltradas; } set { this._sociosFiltradas = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("SociosFiltradas"); } } public SocioViewModel() { this._servico = new SocioServico(); base.EnableMenu = true; this.Seleciona(); } public void CancelarAlteracao() { if (this.CancelSocio == null || !this.Socios.Any((Socio x) => x.get_Id() == this.CancelSocio.get_Id())) { this.Incluir(); } else { DomainBase.Copy(this.Socios.First((Socio x) => x.get_Id() == this.CancelSocio.get_Id()), this.CancelSocio); if (this.SociosFiltradas.Count <= 0 || !this.SociosFiltradas.Any((Socio x) => x.get_Id() == this.CancelSocio.get_Id())) { this.SociosFiltradas.Add(this.CancelSocio); } else { DomainBase.Copy(this.SociosFiltradas.First((Socio x) => x.get_Id() == this.CancelSocio.get_Id()), this.CancelSocio); } this.SociosFiltradas = new ObservableCollection(this.SociosFiltradas); this.SelectedSocio = this.SociosFiltradas.First((Socio x) => x.get_Id() == this.CancelSocio.get_Id()); } base.Alterar(false); } public async void Excluir() { Socio socio; if (this.SelectedSocio != null && this.SelectedSocio.get_Id() != 0) { if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { base.Loading(true); this.IsLoading = true; if (await this._servico.Delete(this.SelectedSocio)) { string str = string.Concat("EXCLUIU SÓCIO \"", this.SelectedSocio.get_Nome(), "\""); long id = this.SelectedSocio.get_Id(); TipoTela? nullable = new TipoTela?(19); object[] objArray = new object[] { this.SelectedSocio.get_Id(), this.SelectedSocio.get_Prefixo(), this.SelectedSocio.get_Telefone(), this.SelectedSocio.get_Email() }; base.RegistrarAcao(str, id, nullable, string.Format("ID: {0}\nTELEFONE: ({1}) {2}\nE-MAIL: {3}", objArray)); int num = this.SociosFiltradas.IndexOf(this.SelectedSocio); this.Socios.Remove(this.SelectedSocio); this.SociosFiltradas.Remove(this.SelectedSocio); this.SociosFiltradas = new ObservableCollection(this.SociosFiltradas); if (this.SociosFiltradas.Count <= 0) { this.SelectedSocio = new Socio(); base.Alterar(false); } else { SocioViewModel socioViewModel = this; socio = (num < this.SociosFiltradas.Count ? this.SociosFiltradas.ElementAt(num) : this.SociosFiltradas.Last()); socioViewModel.SelectedSocio = socio; } base.Loading(false); this.IsLoading = false; base.ToggleSnackBar("SÓCIO EXCLUÍDO COM SUCESSO", true); } else { base.Loading(false); this.IsLoading = false; } } } } internal async Task> Filtrar(string value) { List socios = await Task.Run>(() => this.FiltrarSocio(value)); return socios; } public List FiltrarSocio(string filter) { this.SociosFiltradas = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Socios) : new ObservableCollection( from x in this.Socios where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.get_Nome() select x)); return this.SociosFiltradas.ToList(); } public void Incluir() { Socio socio = new Socio(); socio.set_IdEmpresa(this.SelectedEmpresa.get_Id()); this.SelectedSocio = socio; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; string str; string str1; List> keyValuePairs1 = this.SelectedSocio.Validate(); if (keyValuePairs1.Count <= 0) { str = (this.SelectedSocio.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Socio socio = await this._servico.Save(this.SelectedSocio); if (this._servico.Sucesso) { string str2 = string.Concat(str1, " SÓCIO \"", socio.get_Nome(), "\""); long id = socio.get_Id(); TipoTela? nullable = new TipoTela?(19); object[] objArray = new object[] { socio.get_Id(), socio.get_Prefixo(), socio.get_Telefone(), socio.get_Email() }; base.RegistrarAcao(str2, id, nullable, string.Format("ID: {0}\nTELEFONE: ({1}) {2}\nE-MAIL: {3}", objArray)); if (!this.Socios.Any((Socio x) => x.get_Id() == socio.get_Id())) { this.Socios.Add(socio); } else { DomainBase.Copy(this.Socios.First((Socio x) => x.get_Id() == socio.get_Id()), socio); } if (!this.SociosFiltradas.Any((Socio x) => x.get_Id() == socio.get_Id())) { this.SociosFiltradas.Add(socio); } else { DomainBase.Copy(this.SociosFiltradas.First((Socio x) => x.get_Id() == socio.get_Id()), socio); } this.SociosFiltradas = new ObservableCollection(this.SociosFiltradas); this.WorkOnSelectedSocio(socio, false); base.Alterar(false); base.ToggleSnackBar("SÓCIO SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } str1 = null; return keyValuePairs; } private async void Seleciona() { base.Loading(true); await base.PermissaoTela(19); this.Empresas = new ObservableCollection(await (new EmpresaServico()).BuscarEmpresas()); base.Loading(false); this.SelectedEmpresa = this.Empresas.FirstOrDefault(); } private async Task SelecionaSocios(long? id) { this.Socios = null; this.SociosFiltradas = null; if (id.HasValue) { long? nullable = id; long num = (long)0; if (!(nullable.GetValueOrDefault() == num & nullable.HasValue)) { base.Loading(true); List socios = await (new BaseServico()).BuscarSociosAsync(id.Value); SocioViewModel list = this; List socios1 = socios; list.Socios = ( from x in socios1 orderby x.get_Nome() select x).ToList(); this.SociosFiltradas = new ObservableCollection(this.Socios); this.SelectedSocio = this.SociosFiltradas.FirstOrDefault(); base.Loading(false); return; } } } private async void WorkOnSelectedEmpresa(Empresa value) { if (value != null && value.get_Id() != 0) { await this.SelecionaSocios(new long?(value.get_Id())); } } private void WorkOnSelectedSocio(Socio value, bool registrar = true) { long? nullable; this.CancelSocio = (value == null || value.get_Id() == 0 ? this.CancelSocio : (Socio)value.Clone()); if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 19) { return; } if (registrar) { base.RegistrarAcao(string.Concat("ACESSOU SÓCIO \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(19), string.Format("ID: {0}\nTELEFONE: ({1}) {2}\nE-MAIL: {3}", new object[] { value.get_Id(), value.get_Prefixo(), value.get_Telefone(), value.get_Email() })); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 19; Socio selectedSocio = this.SelectedSocio; if (selectedSocio != null) { nullable = new long?(selectedSocio.get_Id()); } else { nullable = null; } long? nullable1 = nullable; long id = value.get_Id(); if (nullable1.GetValueOrDefault() != id | !nullable1.HasValue) { this.SelectedSocio = this.SociosFiltradas.FirstOrDefault((Socio x) => x.get_Id() == value.get_Id()); } } } }