using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using Gestor.Application.Servicos.Financeiro; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Financeiro; using Gestor.Model.Domain.Generic; namespace Gestor.Application.ViewModels.Financeiro; public class PlanosViewModel : BaseFinanceiroViewModel { private readonly PlanosServico _planosServico; private ObservableCollection _plano = new ObservableCollection(); private ObservableCollection _planosFiltrados = new ObservableCollection(); private bool _isExpanded; private Planos _selectedPlanos; private Plano _selectedPlano; private bool _ativo; private long _ultimoId; public List Planos { get; set; } public ObservableCollection Plano { get { return _plano; } set { _plano = value; OnPropertyChanged("Plano"); } } public ObservableCollection PlanosFiltrados { get { return _planosFiltrados; } set { _planosFiltrados = value; IsExpanded = value != null && value.Count > 0; OnPropertyChanged("PlanosFiltrados"); } } public bool IsExpanded { get { return _isExpanded; } set { _isExpanded = value; OnPropertyChanged("IsExpanded"); } } public Planos SelectedPlanos { get { return _selectedPlanos; } set { _selectedPlanos = value; Planos obj = value; SelectedPlano = ((((obj != null) ? obj.Plano : null) != null) ? ((IEnumerable)Plano).FirstOrDefault((Func)((Plano x) => ((DomainBase)x).Id == ((DomainBase)value.Plano).Id)) : null); Planos obj2 = value; if (obj2 != null && ((DomainBase)obj2).Id > 0) { _ultimoId = ((DomainBase)value).Id; } Planos obj3 = value; VerificarEnables((obj3 != null) ? new long?(((DomainBase)obj3).Id) : null); OnPropertyChanged("SelectedPlanos"); } } public Plano SelectedPlano { get { return _selectedPlano; } set { _selectedPlano = value; OnPropertyChanged("SelectedPlano"); } } public bool Ativo { get { return _ativo; } set { _ativo = value; OnPropertyChanged("Ativo"); } } public PlanosViewModel() { _planosServico = new PlanosServico(); base.EnableMenu = true; Seleciona(); } private async void Seleciona() { Loading(isLoading: true); await PermissaoTela((TipoTela)28); await SelecionaPlanos(); await SelecionaPlanos(PlanosFiltrados.FirstOrDefault()); Loading(isLoading: false); } private async Task SelecionaPlanos() { Planos = (await new BaseServico().BuscarPlanosAsync()).OrderByDescending((Planos x) => x.Ativo).ThenBy(delegate(Planos x) { Plano plano = x.Plano; return (plano == null) ? null : plano.Descricao; }).ThenBy((Planos x) => x.Descricao) .ToList(); PlanosFiltrados = new ObservableCollection(Planos); } public async Task SelecionaPlanos(Planos planos) { if (planos == null) { Alterar(alterar: false); base.EnableMenu = false; base.EnableAlterar = false; return; } Loading(isLoading: true); ObservableCollection source = await _planosServico.BuscarPlanos(); DomainBase.Copy(PlanosFiltrados.First((Planos x) => ((DomainBase)x).Id == ((DomainBase)planos).Id), source.First((Planos x) => ((DomainBase)x).Id == ((DomainBase)planos).Id)); if (Plano == null) { Plano = new ObservableCollection((from x in await new BaseServico().BuscarPlanoAsync() orderby x.Ativo descending, x.Descricao select x).ToList()); } SelectedPlanos = PlanosFiltrados.First((Planos x) => ((DomainBase)x).Id == ((DomainBase)planos).Id); Loading(isLoading: false); } public void Incluir() { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown Planos selectedPlanos = new Planos { Plano = SelectedPlano }; SelectedPlanos = selectedPlanos; Alterar(alterar: true); } public async Task>> Validate() { List> errors = new List>(); bool valida = true; (await new BaseServico().BuscarPlanosAsync())?.ForEach(delegate(Planos x) { if (((DomainBase)x).Id != ((DomainBase)SelectedPlanos).Id && x.Descricao == SelectedPlanos.Descricao) { valida = false; } }); if (!valida) { errors.Add(new KeyValuePair("Descricao", "UM SUBNÍVEL COM ESSE NOME JÁ EXISTE.")); } return errors; } public async Task>> Salvar() { SelectedPlanos.Plano = SelectedPlano; List> errorMessages = SelectedPlanos.Validate(); List> list = errorMessages; list.AddRange(await Validate()); if (errorMessages.Count > 0) { return errorMessages; } SelectedPlanos.Plano = SelectedPlano; SelectedPlanos = await _planosServico.Save(SelectedPlanos); if (!_planosServico.Sucesso) { return null; } await SelecionaPlanos(); await SelecionaPlanos(PlanosFiltrados.First((Planos x) => ((DomainBase)x).Id == ((DomainBase)SelectedPlanos).Id)); Alterar(alterar: false); ToggleSnackBar("SUBNÍVEL SALVO COM SUCESSO"); return null; } public async void CancelarAlteracao() { Loading(isLoading: true); Alterar(alterar: false); if (((DomainBase)SelectedPlanos).Id > 0) { await SelecionaPlanos(); } await SelecionaPlanos(((IEnumerable)PlanosFiltrados).FirstOrDefault((Func)((Planos x) => ((DomainBase)x).Id == _ultimoId))); Loading(isLoading: false); } public async Task> Filtrar(string value) { return await Task.Run(() => FiltrarPlanos(value)); } public ObservableCollection FiltrarPlanos(string filter) { PlanosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(Planos) : new ObservableCollection(from x in Planos where ValidationHelper.RemoveDiacritics(x.Descricao.Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby Ativo descending, x.Descricao select x)); return PlanosFiltrados; } }