summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.ViewModels.Ferramentas/TipoTarefaViewModel.cs
blob: bf0176b630e3c78ab9328381dedce5777f028e2b (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Application.Servicos.Ferramentas;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Common;
using Gestor.Model.Domain.Ferramentas;
using Gestor.Model.Domain.Generic;

namespace Gestor.Application.ViewModels.Ferramentas;

public class TipoTarefaViewModel : BaseSegurosViewModel
{
	private readonly TipoTarefaServico _servico;

	private readonly TarefaServico _servicoTarefa;

	public TipoDeTarefa CancelTipoTarefa;

	private ObservableCollection<TipoDeTarefa> _tiposTarefaFiltrados = new ObservableCollection<TipoDeTarefa>();

	private bool _isExpanded;

	private TipoDeTarefa _selectedTipoTarefa;

	public List<TipoDeTarefa> TiposTarefa { get; set; }

	public ObservableCollection<TipoDeTarefa> TiposTarefaFiltrados
	{
		get
		{
			return _tiposTarefaFiltrados;
		}
		set
		{
			_tiposTarefaFiltrados = value;
			IsExpanded = value != null && value.Count > 0;
			OnPropertyChanged("TiposTarefaFiltrados");
		}
	}

	public bool IsExpanded
	{
		get
		{
			return _isExpanded;
		}
		set
		{
			_isExpanded = value;
			OnPropertyChanged("IsExpanded");
		}
	}

	public TipoDeTarefa SelectedTipoTarefa
	{
		get
		{
			return _selectedTipoTarefa;
		}
		set
		{
			_selectedTipoTarefa = value;
			WorkOnSelectedTipoTarefa(value);
			VerificarEnables((value != null) ? new long?(((DomainBase)value).Id) : null);
			OnPropertyChanged("SelectedTipoTarefa");
		}
	}

	public TipoTarefaViewModel()
	{
		_servico = new TipoTarefaServico();
		_servicoTarefa = new TarefaServico();
		base.EnableMenu = true;
		Seleciona();
	}

	private async void Seleciona()
	{
		Loading(isLoading: true);
		await PermissaoTela((TipoTela)52);
		await SelecionaTipoTarefa();
		Loading(isLoading: false);
	}

	private async Task SelecionaTipoTarefa()
	{
		Loading(isLoading: true);
		TiposTarefa = (from x in await _servico.BuscarTarefas()
			where !x.Excluido
			orderby x.Ativo descending, x.Nome
			select x).ToList();
		TiposTarefaFiltrados = new ObservableCollection<TipoDeTarefa>(TiposTarefa);
		SelectedTipoTarefa = TiposTarefaFiltrados.FirstOrDefault();
		Loading(isLoading: false);
	}

	public async Task<List<TipoDeTarefa>> Filtrar(string value)
	{
		return await Task.Run(() => FiltrarTipoTarefa(value));
	}

	public List<TipoDeTarefa> FiltrarTipoTarefa(string filter)
	{
		TiposTarefaFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<TipoDeTarefa>(TiposTarefa) : new ObservableCollection<TipoDeTarefa>(from x in TiposTarefa
			where ValidationHelper.RemoveDiacritics(x.Nome.Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
			orderby x.Ativo descending, x.Nome
			select x));
		SelectedTipoTarefa = TiposTarefaFiltrados.FirstOrDefault();
		return TiposTarefaFiltrados.ToList();
	}

	public void Incluir()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Expected O, but got Unknown
		SelectedTipoTarefa = new TipoDeTarefa
		{
			Ativo = true,
			Excluido = false
		};
		Alterar(alterar: true);
	}

	public async Task<List<KeyValuePair<string, string>>> Salvar()
	{
		List<KeyValuePair<string, string>> list = SelectedTipoTarefa.Validate();
		list.AddRange(Validate());
		if (list.Count > 0)
		{
			return list;
		}
		string acao = ((((DomainBase)SelectedTipoTarefa).Id == 0L) ? "INCLUIU" : "ALTEROU");
		TipoDeTarefa value = await _servico.Save(SelectedTipoTarefa);
		if (!_servico.Sucesso)
		{
			return null;
		}
		RegistrarAcao(acao + " TIPO DE TAREFA \"" + value.Nome + "\"", ((DomainBase)value).Id, (TipoTela)52, string.Format("ID: {0}\nATIVO: {1}\nDESCRIÇÃO: \"{2}\"", ((DomainBase)value).Id, value.Ativo ? "SIM" : "NÃO", value.Descricao));
		if (TiposTarefa.Any((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)value).Id))
		{
			DomainBase.Copy<TipoDeTarefa, TipoDeTarefa>(TiposTarefa.First((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)value).Id), value);
		}
		else
		{
			TiposTarefa.Add(value);
		}
		if (TiposTarefaFiltrados.Any((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)value).Id))
		{
			DomainBase.Copy<TipoDeTarefa, TipoDeTarefa>(TiposTarefaFiltrados.First((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)value).Id), value);
		}
		else
		{
			TiposTarefaFiltrados.Add(value);
		}
		TiposTarefaFiltrados = new ObservableCollection<TipoDeTarefa>(TiposTarefaFiltrados);
		WorkOnSelectedTipoTarefa(value, registrar: false);
		Recursos.TiposTarefa = TiposTarefa;
		Alterar(alterar: false);
		ToggleSnackBar("TIPO DE TAREFA SALVO COM SUCESSO");
		return null;
	}

	public List<KeyValuePair<string, string>> Validate()
	{
		List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
		if (string.IsNullOrWhiteSpace(SelectedTipoTarefa.Nome))
		{
			return list;
		}
		if (TiposTarefa.Any((TipoDeTarefa x) => x.Nome.Contains(SelectedTipoTarefa.Nome) && ((DomainBase)x).Id != ((DomainBase)SelectedTipoTarefa).Id))
		{
			list.Add(new KeyValuePair<string, string>("Nome", "UM TIPO DE TAREFA COM O MESMO NOME JÁ EXISTE."));
		}
		return list;
	}

	public void CancelarAlteracao()
	{
		if (CancelTipoTarefa != null && TiposTarefa.Any((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)CancelTipoTarefa).Id))
		{
			DomainBase.Copy<TipoDeTarefa, TipoDeTarefa>(TiposTarefa.First((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)CancelTipoTarefa).Id), CancelTipoTarefa);
			if (TiposTarefaFiltrados.Count > 0 && TiposTarefaFiltrados.Any((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)CancelTipoTarefa).Id))
			{
				DomainBase.Copy<TipoDeTarefa, TipoDeTarefa>(TiposTarefaFiltrados.First((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)CancelTipoTarefa).Id), CancelTipoTarefa);
			}
			else
			{
				TiposTarefaFiltrados.Add(CancelTipoTarefa);
			}
			TiposTarefaFiltrados = new ObservableCollection<TipoDeTarefa>(TiposTarefaFiltrados);
			SelectedTipoTarefa = TiposTarefaFiltrados.First((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)CancelTipoTarefa).Id);
		}
		else
		{
			Incluir();
		}
		Alterar(alterar: false);
	}

	public async void Excluir()
	{
		if (SelectedTipoTarefa == null || ((DomainBase)SelectedTipoTarefa).Id == 0L)
		{
			return;
		}
		if ((await _servicoTarefa.BuscarTarefasPorTipo(SelectedTipoTarefa)).Count > 0)
		{
			await ShowMessage("O TIPO " + SelectedTipoTarefa.Nome + ", NÃO PODE SER EXCLUÍDO, POIS ESTÁ CADASTRADO EM UMA TAREFA");
		}
		else if (await ShowMessage("DESEJA REALMENTE EXCLUIR O TIPO DE TAREFA " + SelectedTipoTarefa.Nome + "?", "SIM", "NÃO"))
		{
			Loading(isLoading: true);
			SelectedTipoTarefa.Excluido = true;
			SelectedTipoTarefa.Ativo = false;
			await _servico.Save(SelectedTipoTarefa);
			if (!_servico.Sucesso)
			{
				Loading(isLoading: false);
				return;
			}
			RegistrarAcao("EXCLUIU TIPO DE TAREFA \"" + SelectedTipoTarefa.Nome + "\"", ((DomainBase)SelectedTipoTarefa).Id, (TipoTela)52, string.Format("ID: {0}\nATIVO: {1}\nDESCRIÇÃO: \"{2}\"", ((DomainBase)SelectedTipoTarefa).Id, SelectedTipoTarefa.Ativo ? "SIM" : "NÃO", SelectedTipoTarefa.Descricao));
			TiposTarefa.Remove(SelectedTipoTarefa);
			TiposTarefaFiltrados.Remove(SelectedTipoTarefa);
			Recursos.TiposTarefa.Remove(SelectedTipoTarefa);
			SelectedTipoTarefa = TiposTarefaFiltrados.FirstOrDefault();
			await SelecionaTipoTarefa();
			Loading(isLoading: false);
			ToggleSnackBar("TIPO DE TAREFA EXCLUÍDO COM SUCESSO");
		}
	}

	private void WorkOnSelectedTipoTarefa(TipoDeTarefa value, bool registrar = true)
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Invalid comparison between Unknown and I4
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		CancelTipoTarefa = (TipoDeTarefa)((value == null || ((DomainBase)value).Id == 0L) ? ((object)CancelTipoTarefa) : ((object)(TipoDeTarefa)((DomainBase)value).Clone()));
		if (value == null || ((DomainBase)value).Id == 0L || (LastAccessId == ((DomainBase)value).Id && (int)LastAccessTela == 52))
		{
			return;
		}
		if (registrar)
		{
			RegistrarAcao("ACESSOU TIPO DE TAREFA \"" + value.Nome + "\"", ((DomainBase)value).Id, (TipoTela)52, string.Format("ID: {0}\nATIVO: {1}\nDESCRIÇÃO: \"{2}\"", ((DomainBase)value).Id, value.Ativo ? "SIM" : "NÃO", value.Descricao));
		}
		LastAccessId = ((DomainBase)value).Id;
		LastAccessTela = (TipoTela)52;
		TipoDeTarefa selectedTipoTarefa = SelectedTipoTarefa;
		if (((selectedTipoTarefa != null) ? new long?(((DomainBase)selectedTipoTarefa).Id) : null) != ((DomainBase)value).Id)
		{
			SelectedTipoTarefa = ((IEnumerable<TipoDeTarefa>)TiposTarefaFiltrados).FirstOrDefault((Func<TipoDeTarefa, bool>)((TipoDeTarefa x) => ((DomainBase)x).Id == ((DomainBase)value).Id));
		}
	}
}