summaryrefslogtreecommitdiff
path: root/Gestor.Application/ViewModels/Seguros/PerfilViewModel.cs
blob: dd3313a98c7dea7cf38a1810fc59f415c39c2ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Gestor.Application.Servicos.Generic;
using Gestor.Application.Servicos.Seguros;
using Gestor.Application.ViewModels.Generic;
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.Seguros
{
	public class PerfilViewModel : BaseViewModel
	{
		private readonly PerfilServico _servico;

		private readonly Controle _controle;

		public long CancelPerfil;

		private Perfil _selectedPerfil = new Perfil();

		private ObservableCollection<Perfil> _perfis;

		public ObservableCollection<Perfil> Perfis
		{
			get
			{
				return this._perfis;
			}
			set
			{
				this._perfis = value;
				base.OnPropertyChanged("Perfis");
			}
		}

		public Perfil SelectedPerfil
		{
			get
			{
				return this._selectedPerfil;
			}
			set
			{
				long? nullable;
				this._selectedPerfil = value;
				if (value != null && value.get_Id() > (long)0)
				{
					this.CancelPerfil = value.get_Id();
				}
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedPerfil");
			}
		}

		public PerfilViewModel(Controle controle)
		{
			this._servico = new PerfilServico();
			this._controle = controle;
			this.Seleciona(controle.get_Id());
		}

		public void CancelarAlteracao()
		{
			base.Loading(true);
			if (this.CancelPerfil > (long)0)
			{
				this.SelectedPerfil = this.Perfis.FirstOrDefault<Perfil>((Perfil x) => x.get_Id() == this.CancelPerfil);
			}
			base.Alterar(false);
			base.Loading(false);
		}

		public async Task Excluir()
		{
			if (this.SelectedPerfil != null && this.SelectedPerfil.get_Id() != 0)
			{
				if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false))
				{
					base.Loading(true);
					bool selectedPerfil = (object)this.SelectedPerfil == (object)this.Perfis.Last<Perfil>();
					if (await this._servico.Delete(this.SelectedPerfil))
					{
						if (selectedPerfil)
						{
							int num = this.Perfis.IndexOf(this.SelectedPerfil);
							if (num <= 0)
							{
								await this.SelecionaPerfis(this._controle.get_Id(), (long)0);
							}
							else
							{
								await this.SelecionaPerfis(this._controle.get_Id(), this.Perfis[num - 1].get_Id());
							}
						}
						else if (this.Perfis.Count > 0)
						{
							await this.SelecionaPerfis(this._controle.get_Id(), this.Perfis[this.Perfis.IndexOf(this.SelectedPerfil) + 1].get_Id());
						}
						base.Loading(false);
						base.ToggleSnackBar("PERFIL EXCLUÍDO COM SUCESSO", true);
					}
					else
					{
						base.Loading(false);
					}
				}
			}
		}

		public void Incluir()
		{
			Perfil perfil = new Perfil();
			perfil.set_Controle(this._controle);
			perfil.set_Cliente(this._controle.get_Cliente());
			perfil.set_UsoProfissional(new bool?(false));
			perfil.set_AntiFurto(new Antifurto?(0));
			perfil.set_Isencao(new bool?(false));
			perfil.set_SeguroVida(new bool?(false));
			perfil.set_EstenderCobertura(new bool?(false));
			this.SelectedPerfil = perfil;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedPerfil.Validate();
			if (keyValuePairs1.Count <= 0)
			{
				this.SelectedPerfil = await this._servico.Save(this.SelectedPerfil);
				if (this._servico.Sucesso)
				{
					await this.SelecionaPerfis(this._controle.get_Id(), this.SelectedPerfil.get_Id());
					base.ToggleSnackBar("PERFIL SALVO COM SUCESSO", true);
					base.Alterar(false);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			return keyValuePairs;
		}

		private async void Seleciona(long id)
		{
			base.Loading(true);
			await base.PermissaoTela(32);
			await this.SelecionaPerfis(id, (long)0);
			base.Loading(false);
		}

		private async Task SelecionaPerfis(long id, long perfilId = 0L)
		{
			Perfil perfil;
			base.Loading(true);
			this.Perfis = new ObservableCollection<Perfil>(await this._servico.BuscarPerfis(id));
			PerfilViewModel perfilViewModel = this;
			perfil = (perfilId == 0 ? this.Perfis.FirstOrDefault<Perfil>() : this.Perfis.First<Perfil>((Perfil x) => x.get_Id() == perfilId));
			perfilViewModel.SelectedPerfil = perfil;
			base.Loading(false);
		}
	}
}