using Gestor.Application.Helpers; using Gestor.Application.Model.Ajuda; using Gestor.Application.Servicos.Ajuda; using Gestor.Application.ViewModels.Generic; using Gestor.Model.API; using Gestor.Model.Domain.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using Gestor.Model.Helper; using Gestor.Model.Resources; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; namespace Gestor.Application.ViewModels.Drawer.Ajuda { public class AtendimentosViewModel : BaseViewModel { private readonly AjudaServico _ajudaServico; private List _status; private string _webbody; private Visibility _envioEmailVisibility = Visibility.Collapsed; private Visibility _atendimentoVisibility; private ObservableCollection _atendimentos; private Atendimento _selectedAtendimento; private string _nome; private string _ddd; private string _telefone; private string _email; private string _assunto; private string _idacesso; private string _corpo; private string _senhaacesso; private ObservableCollection _arquivosAnexados = new ObservableCollection(); private Gestor.Model.Domain.Common.ArquivoDigital _selectedAnexado = new Gestor.Model.Domain.Common.ArquivoDigital(); private string _head; public ObservableCollection ArquivosAnexados { get { return this._arquivosAnexados; } set { this._arquivosAnexados = value; base.OnPropertyChanged("ArquivosAnexados"); } } public string Assunto { get { return this._assunto; } set { this._assunto = value; base.OnPropertyChanged("Assunto"); } } public ObservableCollection Atendimentos { get { return this._atendimentos; } set { this._atendimentos = value; base.OnPropertyChanged("Atendimentos"); } } public Visibility AtendimentoVisibility { get { return this._atendimentoVisibility; } set { this._atendimentoVisibility = value; base.OnPropertyChanged("AtendimentoVisibility"); } } public string Corpo { get { return this._corpo; } set { this._corpo = value; base.OnPropertyChanged("Corpo"); } } public string Ddd { get { return this._ddd; } set { this._ddd = value; base.OnPropertyChanged("Ddd"); } } public string Email { get { return this._email; } set { this._email = value; base.OnPropertyChanged("Email"); } } public Visibility EnvioEmailVisibility { get { return this._envioEmailVisibility; } set { this._envioEmailVisibility = value; base.OnPropertyChanged("EnvioEmailVisibility"); } } public string Head { get { return this._head; } set { this._head = value; base.OnPropertyChanged("Head"); } } public Gestor.Model.API.HorarioAtendimento HorarioAtendimento { get; set; } public string IdAcesso { get { return this._idacesso; } set { this._idacesso = value; base.OnPropertyChanged("IdAcesso"); } } public string Nome { get { return this._nome; } set { this._nome = value; base.OnPropertyChanged("Nome"); } } public Gestor.Model.Domain.Common.ArquivoDigital SelectedAnexado { get { return this._selectedAnexado; } set { this._selectedAnexado = value; base.OnPropertyChanged("SelectedAnexado"); } } public Atendimento SelectedAtendimento { get { return this._selectedAtendimento; } set { this._selectedAtendimento = value; this.WorkOnSelectedAtendimento(value); base.OnPropertyChanged("SelectedAtendimento"); } } public string SenhaAcesso { get { return this._senhaacesso; } set { this._senhaacesso = value; base.OnPropertyChanged("SenhaAcesso"); } } public List Status { get { return this._status; } set { this._status = value; base.OnPropertyChanged("Status"); } } public string Telefone { get { return this._telefone; } set { this._telefone = value; base.OnPropertyChanged("Telefone"); } } public string WebBody { get { string str = this._webbody; if (str == null) { return null; } return str.ToUpper().Trim(); } set { this._webbody = value; base.OnPropertyChanged("WebBody"); } } public AtendimentosViewModel() { base.EnableButtons = true; this._ajudaServico = new AjudaServico(); this.LoadCombo(); this.InitialLoad(); } public async void Anexar() { List arquivoDigitals = await base.AddAttachments(new List(), new List()); if (arquivoDigitals != null) { arquivoDigitals.AddRange(this.ArquivosAnexados); this.ArquivosAnexados = new ObservableCollection(arquivoDigitals); } } public void Delete(Gestor.Model.Domain.Common.ArquivoDigital arquivo) { if (this.SelectedAnexado == null) { return; } Gestor.Model.Domain.Common.ArquivoDigital arquivoDigital = this.ArquivosAnexados.First((Gestor.Model.Domain.Common.ArquivoDigital x) => x.get_Descricao() == arquivo.get_Descricao()); this.ArquivosAnexados.Remove(arquivoDigital); this.ArquivosAnexados = new ObservableCollection(this.ArquivosAnexados); } public async Task>> Enviar() { List> keyValuePairs; object obj; try { List> keyValuePairs1 = this.Validate(); if (keyValuePairs1.Count <= 0) { CustomerData customerDatum = new CustomerData(); customerDatum.set_Corretora(Recursos.Empresa.get_Nome()); customerDatum.set_Name(this.Nome); customerDatum.set_AreaCode(this.Ddd); customerDatum.set_Number(this.Telefone); customerDatum.set_Email(this.Email); CustomerData customerDatum1 = customerDatum; object[] corpo = new object[] { this.Corpo, this.IdAcesso, this.SenhaAcesso, ApplicationHelper.Versao, Recursos.Usuario.get_Id(), Recursos.Usuario.get_Login(), JsonConvert.SerializeObject(customerDatum1) }; string str = string.Format("{0}

ID Acesso: {1}
Senha Acesso: {2}

VERSÃO DO SISTEMA: {3}
USUÁRIO SISTEMA:{4} {5}

{6}

", corpo); CustomerAttendance customerAttendance = new CustomerAttendance(); customerAttendance.set_CustomerData(customerDatum1); customerAttendance.set_Subject(this.Assunto); customerAttendance.set_Body(str); ObservableCollection arquivosAnexados = this.ArquivosAnexados; customerAttendance.set_Attachments(arquivosAnexados.Select((Gestor.Model.Domain.Common.ArquivoDigital x) => { Attachment attachment = new Attachment(); attachment.set_Description(x.get_Descricao()); attachment.set_Extension(x.get_Extensao()); attachment.set_File(x.get_Arquivo()); return attachment; }).ToList()); this.LimparComponentes(); this.SelecionaAtendimento(); if (await Connection.Post("Attendance/Send", customerAttendance) == null) { obj = null; } else { obj = new List>(); ((List>)obj).Add(new KeyValuePair("fail", "")); } keyValuePairs = (List>)obj; } else { keyValuePairs = keyValuePairs1; } } catch (Exception exception) { keyValuePairs = new List>() { new KeyValuePair("fail", "") }; } return keyValuePairs; } private async void HabilitarAtendimento(Gestor.Model.API.HorarioAtendimento horario) { bool id; string str; if (horario == null || horario.get_Autorizado()) { AtendimentosViewModel atendimentosViewModel = this; Usuario usuario = Recursos.Usuario; if (usuario != null) { id = usuario.get_Id() > (long)0; } else { id = false; } atendimentosViewModel.EnableButtons = id; } else { base.EnableButtons = false; if (horario.get_InicioDia().HasValue) { string[] strArrays = new string[] { " de ", horario.get_InicioDia().DiaDaSemana(), " à ", horario.get_FimDia().DiaDaSemana(), ", das ", horario.get_InicioHora(), "hs às ", horario.get_FimHora(), "hs" }; str = string.Concat(strArrays); } else { string[] inicioHora = new string[] { " das ", horario.get_InicioHora(), "hs às ", horario.get_FimHora(), "hs" }; str = string.Concat(inicioHora); } string str1 = str; string[] newLine = new string[] { "Prezado Cliente,", Environment.NewLine, Environment.NewLine, "Informamos Nosso horário de atendimento e recepção dos mesmos é ", str1, ".", Environment.NewLine, "Infelizmente não realizamos agendamento de data/hora de atendimento, por gentileza realize a solicitação dentro do horário comercial que retornaremos a você." }; await base.ShowMessage(string.Concat(newLine), "OK", "", false); } } private async void InitialLoad() { base.IsEnabled = false; this.HorarioAtendimento = await Connection.Get("Attendance", true, false); this.HabilitarAtendimento(this.HorarioAtendimento); base.IsEnabled = true; } public void LimparComponentes() { this.Nome = Recursos.Usuario.get_Nome(); this.Ddd = Recursos.Usuario.get_Prefixo(); this.Telefone = Recursos.Usuario.get_Telefone(); this.Email = Recursos.Usuario.get_Email(); this.Assunto = null; this.IdAcesso = null; this.SenhaAcesso = null; this.Corpo = string.Empty; this.ArquivosAnexados = new ObservableCollection(); } private void LoadCombo() { this.Status = new List() { "PENDENTES", "FINALIZADOS" }; } public void SelecionaAtendimento() { if (this.Atendimentos != null && this.Atendimentos.Count > 0) { this.SelectedAtendimento = this.Atendimentos[0]; } this.AtendimentoVisibility = Visibility.Visible; this.EnvioEmailVisibility = Visibility.Collapsed; } public List> Validate() { List> keyValuePairs = ValidationHelper.AddValue(); if (string.IsNullOrWhiteSpace(this.Nome)) { ValidationHelper.AddValue(keyValuePairs, "Nome", Messages.get_Obrigatorio(), true); } if (string.IsNullOrWhiteSpace(this.Ddd)) { ValidationHelper.AddValue(keyValuePairs, "Ddd", Messages.get_Obrigatorio(), true); } else if (!ValidationHelper.ValidacaoPrefixo(this.Ddd)) { ValidationHelper.AddValue(keyValuePairs, "Ddd", Messages.get_Invalido(), true); } if (string.IsNullOrWhiteSpace(this.Telefone)) { ValidationHelper.AddValue(keyValuePairs, "Telefone", Messages.get_Obrigatorio(), true); } else if (!ValidationHelper.ValidacaoTelefone(this.Telefone)) { ValidationHelper.AddValue(keyValuePairs, "Telefone", Messages.get_Invalido(), true); } if (string.IsNullOrWhiteSpace(this.Email)) { ValidationHelper.AddValue(keyValuePairs, "Email", Messages.get_Obrigatorio(), true); } else if (!ValidationHelper.ValidacaoEmail(this.Email)) { ValidationHelper.AddValue(keyValuePairs, "Email", Messages.get_Invalido(), true); } if (string.IsNullOrWhiteSpace(this.Corpo)) { ValidationHelper.AddValue(keyValuePairs, "Corpo", Messages.get_Obrigatorio(), true); } if (string.IsNullOrWhiteSpace(this.Assunto)) { ValidationHelper.AddValue(keyValuePairs, "Assunto", Messages.get_Obrigatorio(), true); } return keyValuePairs; } private async void WorkOnSelectedAtendimento(Atendimento value) { if (value != null) { this.WebBody = await this._ajudaServico.BuscarCorpoAtendimentos(value.IdAtendimento); } } public async void WorkOnSelectedStatus(string value) { this.Atendimentos = await this._ajudaServico.BuscarAtendimentos(value); this.SelecionaAtendimento(); } } }