using Gestor.Application.Helpers; using Gestor.Application.Servicos; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; 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 CadastroParceiroViewModel : BaseSegurosViewModel { private readonly ParceiroServico _servico; private Parceiro _selectedParceiro; private ObservableCollection _parceirosFiltrados = new ObservableCollection(); private bool _isExpanded; public Parceiro CancelParceiro; private Item _selectedItem = new Item(); public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public List Parceiros { get; set; } public ObservableCollection ParceirosFiltrados { get { return this._parceirosFiltrados; } set { this._parceirosFiltrados = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("ParceirosFiltrados"); } } public Item SelectedItem { get { return this._selectedItem; } set { this._selectedItem = value; base.OnPropertyChanged("SelectedItem"); } } public Parceiro SelectedParceiro { get { return this._selectedParceiro; } set { long? nullable; this._selectedParceiro = value; this.WorkOnSelectedParceiro(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedParceiro"); } } public CadastroParceiroViewModel(Parceiro parceiro) { this._servico = new ParceiroServico(); base.EnableMenu = true; this.Seleciona(parceiro); } public void CancelarAlteracao() { if (this.CancelParceiro == null || !this.Parceiros.Any((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id())) { this.Incluir(); } else { DomainBase.Copy(this.Parceiros.First((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()), this.CancelParceiro); if (this.ParceirosFiltrados.Count <= 0 || !this.ParceirosFiltrados.Any((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id())) { this.ParceirosFiltrados.Add(this.CancelParceiro); } else { DomainBase.Copy(this.ParceirosFiltrados.First((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()), this.CancelParceiro); } this.ParceirosFiltrados = new ObservableCollection(this.ParceirosFiltrados); this.SelecionarParceiro(this.ParceirosFiltrados.First((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id())); } base.Alterar(false); } public async void Excluir() { string complemento; Parceiro parceiro; if (this.SelectedParceiro != null && this.SelectedParceiro.get_Id() != 0) { if (await (new BaseServico()).ParceiroUtilizado(this.SelectedParceiro.get_Id())) { await base.ShowMessage("PARCEIRO NÃO PODE SER EXCLUÍDO POIS ESTÁ SENDO UTILIZADO.", "OK", "", false); } else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { base.Loading(true); if (await this._servico.Delete(this.SelectedParceiro)) { CadastroParceiroViewModel cadastroParceiroViewModel = this; string str = string.Concat("EXCLUIU O PARCEIRO \"", this.SelectedParceiro.get_Nome(), "\""); long id = this.SelectedParceiro.get_Id(); TipoTela? nullable = new TipoTela?(22); string[] cgccpf = new string[] { "DOCUMENTO: ", this.SelectedParceiro.get_Cgccpf(), "\nTELEFONE 1: (", this.SelectedParceiro.get_Ddd1(), ") ", this.SelectedParceiro.get_Telefone1(), "\nTELEFONE 2: (", this.SelectedParceiro.get_Ddd2(), ") ", this.SelectedParceiro.get_Telefone2(), "\nTELEFONE 3: (", this.SelectedParceiro.get_Ddd3(), ") ", this.SelectedParceiro.get_Telefone3(), "\nE-MAIL: ", this.SelectedParceiro.get_Email(), "\n\nENDEREÇO: ", this.SelectedParceiro.get_Endereco(), ", ", this.SelectedParceiro.get_Numero(), ", ", null, null, null, null, null, null, null, null, null }; if (string.IsNullOrWhiteSpace(this.SelectedParceiro.get_Complemento())) { complemento = "-"; } else { complemento = this.SelectedParceiro.get_Complemento(); if (complemento == null) { complemento = ""; } } cgccpf[21] = complemento; cgccpf[22] = ", "; cgccpf[23] = this.SelectedParceiro.get_Bairro(); cgccpf[24] = ", "; cgccpf[25] = this.SelectedParceiro.get_Cidade(); cgccpf[26] = "/"; cgccpf[27] = this.SelectedParceiro.get_Uf(); cgccpf[28] = " - "; cgccpf[29] = this.SelectedParceiro.get_Cep(); cadastroParceiroViewModel.RegistrarAcao(str, id, nullable, string.Concat(cgccpf)); int num = this.ParceirosFiltrados.IndexOf(this.SelectedParceiro); this.Parceiros.Remove(this.SelectedParceiro); this.ParceirosFiltrados.Remove(this.SelectedParceiro); this.ParceirosFiltrados = new ObservableCollection(this.ParceirosFiltrados); if (this.ParceirosFiltrados.Count <= 0) { this.Incluir(); base.Alterar(false); base.EnableMenu = false; } else { CadastroParceiroViewModel cadastroParceiroViewModel1 = this; parceiro = (num < this.ParceirosFiltrados.Count ? this.ParceirosFiltrados.ElementAt(num) : this.ParceirosFiltrados.Last()); cadastroParceiroViewModel1.SelecionarParceiro(parceiro); } base.Loading(false); base.ToggleSnackBar("PARCEIRO EXCLUÍDO COM SUCESSO", true); } else { base.Loading(false); } } } } internal async Task> Filtrar(string value) { List parceiros = await Task.Run>(() => this.FiltrarParceiro(value)); return parceiros; } public List FiltrarParceiro(string filter) { this.ParceirosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Parceiros) : new ObservableCollection( from x in this.Parceiros where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) select x)); return this.ParceirosFiltrados.ToList(); } public void Incluir() { this.SelectedParceiro = new Parceiro(); base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; string str; string complemento; string str1; List> keyValuePairs1 = this.SelectedParceiro.Validate(); if (keyValuePairs1.Count <= 0) { str = (this.SelectedParceiro.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Parceiro parceiro = await this._servico.Save(this.SelectedParceiro); if (this._servico.Sucesso) { CadastroParceiroViewModel cadastroParceiroViewModel = this; string str2 = string.Concat(str1, " PARCEIRO \"", parceiro.get_Nome(), "\""); long id = parceiro.get_Id(); TipoTela? nullable = new TipoTela?(22); string[] cgccpf = new string[] { "DOCUMENTO: ", parceiro.get_Cgccpf(), "\nTELEFONE 1: (", parceiro.get_Ddd1(), ") ", parceiro.get_Telefone1(), "\nTELEFONE 2: (", parceiro.get_Ddd2(), ") ", parceiro.get_Telefone2(), "\nTELEFONE 3: (", parceiro.get_Ddd3(), ") ", parceiro.get_Telefone3(), "\nE-MAIL: ", parceiro.get_Email(), "\n\nENDEREÇO: ", parceiro.get_Endereco(), ", ", parceiro.get_Numero(), ", ", null, null, null, null, null, null, null, null, null }; if (string.IsNullOrWhiteSpace(parceiro.get_Complemento())) { complemento = "-"; } else { complemento = parceiro.get_Complemento(); if (complemento == null) { complemento = ""; } } cgccpf[21] = complemento; cgccpf[22] = ", "; cgccpf[23] = parceiro.get_Bairro(); cgccpf[24] = ", "; cgccpf[25] = parceiro.get_Cidade(); cgccpf[26] = "/"; cgccpf[27] = parceiro.get_Uf(); cgccpf[28] = " - "; cgccpf[29] = parceiro.get_Cep(); cadastroParceiroViewModel.RegistrarAcao(str2, id, nullable, string.Concat(cgccpf)); if (!this.Parceiros.Any((Parceiro x) => x.get_Id() == parceiro.get_Id())) { this.Parceiros.Add(parceiro); } else { DomainBase.Copy(this.Parceiros.First((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro); } if (!this.ParceirosFiltrados.Any((Parceiro x) => x.get_Id() == parceiro.get_Id())) { this.ParceirosFiltrados.Add(parceiro); } else { DomainBase.Copy(this.ParceirosFiltrados.First((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro); } this.ParceirosFiltrados = new ObservableCollection(this.ParceirosFiltrados); Recursos.Parceiros = this.Parceiros; this.WorkOnSelectedParceiro(parceiro, false); base.Alterar(false); base.ToggleSnackBar("PARCEIRO SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } str1 = null; return keyValuePairs; } private async void Seleciona(Parceiro parceiro) { base.Loading(true); await base.PermissaoTela(22); await this.SelecionaParceiro(parceiro); base.Loading(false); } private async Task SelecionaParceiro(Parceiro parceiro = null) { base.Loading(true); List parceiros = await (new BaseServico()).BuscarParceirosAsync(); CadastroParceiroViewModel list = this; List parceiros1 = parceiros; list.Parceiros = ( from x in parceiros1 orderby x.get_Nome() select x).ToList(); this.ParceirosFiltrados = new ObservableCollection(this.Parceiros); CadastroParceiroViewModel cadastroParceiroViewModel = this; Parceiro parceiro1 = parceiro; if (parceiro1 == null) { parceiro1 = this.ParceirosFiltrados.FirstOrDefault(); } cadastroParceiroViewModel.SelecionarParceiro(parceiro1); base.Loading(false); } public async void SelecionarParceiro(Parceiro parceiro) { if (parceiro != null) { Parceiro parceiro1 = await this._servico.BuscarParceiro(parceiro.get_Id()); DomainBase.Copy(this.ParceirosFiltrados.First((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro1); this.SelectedParceiro = this.ParceirosFiltrados.First((Parceiro x) => x.get_Id() == parceiro.get_Id()); } else { this.SelectedParceiro = null; } } private async void WorkOnSelectedParceiro(Parceiro value, bool registrar = true) { Parceiro parceiro; long? nullable; string complemento; CadastroParceiroViewModel cadastroParceiroViewModel = this; parceiro = (value == null || value.get_Id() == 0 ? this.CancelParceiro : (Parceiro)value.Clone()); cadastroParceiroViewModel.CancelParceiro = parceiro; if (value != null && value.get_Id() != 0 && (this.LastAccessId != value.get_Id() || this.LastAccessTela != 22)) { if (registrar) { CadastroParceiroViewModel cadastroParceiroViewModel1 = this; string str = string.Concat("ACESSOU PARCEIRO \"", value.get_Nome(), "\""); long id = value.get_Id(); TipoTela? nullable1 = new TipoTela?(22); string[] cgccpf = new string[] { "DOCUMENTO: ", value.get_Cgccpf(), "\nTELEFONE 1: (", value.get_Ddd1(), ") ", value.get_Telefone1(), "\nTELEFONE 2: (", value.get_Ddd2(), ") ", value.get_Telefone2(), "\nTELEFONE 3: (", value.get_Ddd3(), ") ", value.get_Telefone3(), "\nE-MAIL: ", value.get_Email(), "\n\nENDEREÇO: ", value.get_Endereco(), ", ", value.get_Numero(), ", ", null, null, null, null, null, null, null, null, null }; if (string.IsNullOrWhiteSpace(value.get_Complemento())) { complemento = "-"; } else { complemento = value.get_Complemento(); if (complemento == null) { complemento = ""; } } cgccpf[21] = complemento; cgccpf[22] = ", "; cgccpf[23] = value.get_Bairro(); cgccpf[24] = ", "; cgccpf[25] = value.get_Cidade(); cgccpf[26] = "/"; cgccpf[27] = value.get_Uf(); cgccpf[28] = " - "; cgccpf[29] = value.get_Cep(); cadastroParceiroViewModel1.RegistrarAcao(str, id, nullable1, string.Concat(cgccpf)); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 22; Parceiro selectedParceiro = this.SelectedParceiro; if (selectedParceiro != null) { nullable = new long?(selectedParceiro.get_Id()); } else { nullable = null; } long? nullable2 = nullable; long num = value.get_Id(); if (nullable2.GetValueOrDefault() != num | !nullable2.HasValue) { Parceiro parceiro1 = await this._servico.BuscarParceiro(value.get_Id()); DomainBase.Copy(this.SelectedParceiro, parceiro1); Parceiro selectedParceiro1 = this.SelectedParceiro; if (selectedParceiro1 != null) { selectedParceiro1.Initialize(); } else { } this.Initialized = true; } } } } }