summaryrefslogtreecommitdiff
path: root/Gestor.Application/ViewModels/Drawer/ExpedicaoViewModel.cs
blob: 2aff10f4f7f3ad92e34945962fe8d77c83bd4961 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
using Gestor.Application.Servicos.Generic;
using Gestor.Application.Servicos.Seguros;
using Gestor.Application.ViewModels.Generic;
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.Drawer
{
	public class ExpedicaoViewModel : BaseSegurosViewModel
	{
		private readonly ExpedicaoServico _servico;

		private readonly Documento _documento;

		private bool _carregando;

		private Expedicao _selectedExpedicao = new Expedicao();

		private ObservableCollection<Expedicao> _expedicoes = new ObservableCollection<Expedicao>();

		public bool Carregando
		{
			get
			{
				return this._carregando;
			}
			set
			{
				this._carregando = value;
				base.EnableMenu = !value;
				base.OnPropertyChanged("Carregando");
			}
		}

		public ObservableCollection<Expedicao> Expedicoes
		{
			get
			{
				return this._expedicoes;
			}
			set
			{
				this._expedicoes = value;
				base.OnPropertyChanged("Expedicoes");
			}
		}

		public Expedicao SelectedExpedicao
		{
			get
			{
				return this._selectedExpedicao;
			}
			set
			{
				long? nullable;
				this._selectedExpedicao = value;
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedExpedicao");
			}
		}

		public ExpedicaoViewModel(Documento documento)
		{
			this._servico = new ExpedicaoServico();
			this._documento = documento;
			this.Seleciona(documento);
		}

		public async void CancelarAlteracao()
		{
			Expedicao expedicao;
			this.Carregando = true;
			long id = this.SelectedExpedicao.get_Id();
			await this.Carregar(this.SelectedExpedicao.get_Apolice());
			ExpedicaoViewModel expedicaoViewModel = this;
			if (this.Expedicoes.Count <= 0 || id <= (long)0)
			{
				expedicao = (this.Expedicoes.Count <= 0 || id != 0 ? new Expedicao() : this.Expedicoes.First<Expedicao>());
			}
			else
			{
				expedicao = this.Expedicoes.FirstOrDefault<Expedicao>((Expedicao x) => x.get_Id() == id);
			}
			expedicaoViewModel.SelectedExpedicao = expedicao;
			base.Alterar(false);
			this.Carregando = false;
		}

		private async Task Carregar(Documento documento)
		{
			this.Expedicoes = new ObservableCollection<Expedicao>(await this._servico.BuscarExpedicoes(documento));
		}

		public async Task<bool> Excluir()
		{
			bool flag;
			Expedicao expedicao;
			if (this.SelectedExpedicao == null || this.SelectedExpedicao.get_Id() == 0)
			{
				flag = false;
			}
			else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false))
			{
				this.Carregando = true;
				if (await this._servico.Delete(this.SelectedExpedicao))
				{
					int num = this.Expedicoes.IndexOf(this.SelectedExpedicao);
					this.Expedicoes.Remove(this.SelectedExpedicao);
					if (this.Expedicoes.Count <= 0)
					{
						this.SelectedExpedicao = new Expedicao();
					}
					else
					{
						ExpedicaoViewModel expedicaoViewModel = this;
						expedicao = (num < this.Expedicoes.Count ? this.Expedicoes.ElementAt<Expedicao>(num) : this.Expedicoes.Last<Expedicao>());
						expedicaoViewModel.SelectedExpedicao = expedicao;
					}
					this.Carregando = false;
					flag = true;
				}
				else
				{
					this.Carregando = false;
					flag = false;
				}
			}
			else
			{
				flag = false;
			}
			return flag;
		}

		public void Incluir()
		{
			Expedicao expedicao = new Expedicao();
			expedicao.set_Apolice(this._documento);
			this.SelectedExpedicao = expedicao;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			this.SelectedExpedicao = await this._servico.Save(this.SelectedExpedicao);
			if (this._servico.Sucesso)
			{
				long id = this.SelectedExpedicao.get_Id();
				await this.Carregar(this.SelectedExpedicao.get_Apolice());
				this.SelectedExpedicao = this.Expedicoes.FirstOrDefault<Expedicao>((Expedicao x) => x.get_Id() == id);
				base.Alterar(false);
				keyValuePairs = null;
			}
			else
			{
				keyValuePairs = null;
			}
			return keyValuePairs;
		}

		private async void Seleciona(Documento documento)
		{
			this.Carregando = true;
			await base.PermissaoTela(46);
			await this.Carregar(documento);
			if (this.Expedicoes.Count <= 0)
			{
				this.SelectedExpedicao = new Expedicao();
				base.Alterar(false);
				base.EnableMenu = false;
			}
			else
			{
				this.SelectedExpedicao = this.Expedicoes.First<Expedicao>();
			}
			this.Carregando = false;
		}
	}
}