using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using Assinador.Infrastructure.Helpers; using Gestor.Application.Helpers; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; namespace Gestor.Application.ViewModels.Drawer; public class MetaSeguradoraViewModel : BaseSegurosViewModel { private readonly MetaSeguradoraServico _servico; private readonly Seguradora _selectedSeguradora; private MetaSeguradora _selectedMetaSeguradora = new MetaSeguradora(); private ObservableCollection _metasSeguradora = new ObservableCollection(); private bool _carregando; public MetaSeguradora CancelMetaSeguradora; public MetaSeguradora SelectedMetaSeguradora { get { return _selectedMetaSeguradora; } set { _selectedMetaSeguradora = value; CancelMetaSeguradora = ((value != null && ((DomainBase)value).Id > 0) ? value : CancelMetaSeguradora); VerificarEnables((value != null) ? new long?(((DomainBase)value).Id) : null); OnPropertyChanged("SelectedMetaSeguradora"); } } public ObservableCollection MetasSeguradora { get { return _metasSeguradora; } set { _metasSeguradora = value; OnPropertyChanged("MetasSeguradora"); } } public bool Carregando { get { return _carregando; } set { _carregando = value; base.IsEnabled = !value; base.EnableMenu = !value; OnPropertyChanged("Carregando"); } } public MetaSeguradoraViewModel(Seguradora seguradora) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown _servico = new MetaSeguradoraServico(); Seleciona(seguradora); _selectedSeguradora = seguradora; } private async void Seleciona(Seguradora seguradora) { Loading(isLoading: true); await PermissaoTela((TipoTela)31); await SelecionaMetasSeguradora(seguradora); Loading(isLoading: false); } public void SelecionaMetaSeguradora(MetaSeguradora metaSeguradora) { SelectedMetaSeguradora = metaSeguradora; } private async Task SelecionaMetasSeguradora(Seguradora seguradora) { Carregando = true; await CarregarMetas(seguradora); if (MetasSeguradora.Count > 0) { SelecionaMetaSeguradora(MetasSeguradora.First()); } else { SelectedMetaSeguradora = new MetaSeguradora(); Alterar(alterar: false); base.EnableMenu = false; base.EnableAlterar = false; base.EnableExcluir = false; } Carregando = false; } public void Incluir() { //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Expected O, but got Unknown DateTime date = Funcoes.GetNetworkTime().Date; SelectedMetaSeguradora = new MetaSeguradora { Ano = date.Year, Mes = (Mes)date.Month }; Alterar(alterar: true); } public async Task>> Salvar() { SelectedMetaSeguradora.Seguradora = _selectedSeguradora; string acao = ((((DomainBase)SelectedMetaSeguradora).Id == 0L) ? "INCLUIU" : "ALTEROU"); MetaSeguradora value = await _servico.Save(SelectedMetaSeguradora); if (!_servico.Sucesso) { return null; } RegistrarAcao(acao + " META DA SEGURADORA \"" + value.Seguradora.Nome + "\"", ((DomainBase)value).Id, (TipoTela)31, $"ID: {((DomainBase)value).Id}\nVALOR: {value.Valor:C}\nMÊS: {Functions.GetDescription((Enum)(object)value.Mes)}\nANO: {value.Ano}"); if (MetasSeguradora.Any((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)value).Id)) { DomainBase.Copy(MetasSeguradora.First((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)value).Id), value); } else { MetasSeguradora.Add(value); } MetasSeguradora = new ObservableCollection(MetasSeguradora); SelectedMetaSeguradora = MetasSeguradora.First((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)value).Id); Alterar(alterar: false); return null; } public async Task Excluir() { if (SelectedMetaSeguradora == null || ((DomainBase)SelectedMetaSeguradora).Id == 0L) { return false; } if (!(await ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO"))) { return false; } Carregando = true; if (!(await _servico.Delete(SelectedMetaSeguradora))) { Carregando = false; Alterar(alterar: false); return false; } MetaSeguradoraViewModel metaSeguradoraViewModel = this; Seguradora seguradora = SelectedMetaSeguradora.Seguradora; metaSeguradoraViewModel.RegistrarAcao("EXCLUIU META DA SEGURADORA \"" + ((seguradora != null) ? seguradora.Nome : null) + "\"", ((DomainBase)SelectedMetaSeguradora).Id, (TipoTela)31, $"ID: {((DomainBase)SelectedMetaSeguradora).Id}\nVALOR: {SelectedMetaSeguradora.Valor:C}\nMÊS: {Functions.GetDescription((Enum)(object)SelectedMetaSeguradora.Mes)}\nANO: {SelectedMetaSeguradora.Ano}"); int num = MetasSeguradora.IndexOf(SelectedMetaSeguradora); MetasSeguradora.Remove(SelectedMetaSeguradora); MetasSeguradora.Remove(SelectedMetaSeguradora); MetasSeguradora = new ObservableCollection(MetasSeguradora); if (MetasSeguradora.Count > 0) { SelectedMetaSeguradora = ((num < MetasSeguradora.Count) ? MetasSeguradora.ElementAt(num) : MetasSeguradora.Last()); } else { SelectedMetaSeguradora = new MetaSeguradora(); Alterar(alterar: false); base.EnableMenu = false; base.EnableAlterar = false; base.EnableExcluir = false; } Carregando = false; return true; } public void CancelarAlteracao() { Carregando = true; if (CancelMetaSeguradora != null && MetasSeguradora.Any((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelMetaSeguradora).Id)) { DomainBase.Copy(MetasSeguradora.First((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelMetaSeguradora).Id), CancelMetaSeguradora); SelectedMetaSeguradora = MetasSeguradora.First((MetaSeguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelMetaSeguradora).Id); } Alterar(alterar: false); Carregando = false; } private async Task CarregarMetas(Seguradora seguradora) { MetasSeguradora = new ObservableCollection(from x in await new BaseServico().BuscarMetaSeguradoraAsync(seguradora) orderby x.Mes, x.Valor select x); } }