using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Comal.Classes;
using InABox.Clients;
using InABox.Core;
using InABox.Wpf;
using InABox.WPF;
using Syncfusion.Windows.Tools.Controls;
namespace PRSDesktop
{
///
/// Interaction logic for NotificationForm.xaml
///
public partial class NotificationForm : ThemableWindow
{
private CoreTable Employees;
private CoreTable Jobs;
private CoreTable Teams;
public NotificationForm()
{
InitializeComponent();
ContentEdit.SetColor(Colors.LightYellow);
}
public string Subject
{
get => TitleEdit.Text;
set => TitleEdit.Text = value;
}
public string Description
{
get => ContentEdit.Text;
set => ContentEdit.Text = value;
}
public Guid Recipient { get; set; }
public Guid JobID { get; set; }
public void OnLoaded(object sender, RoutedEventArgs e)
{
new Client().Query(
LookupFactory.DefineFilter(),
LookupFactory.DefineColumns(),
LookupFactory.DefineSort(),
(table, exception) =>
{
Employees = table;
Dispatcher.Invoke(() => { ReloadEmployees(Guid.Empty); });
}
);
new Client().Query(
null,
null,
new SortOrder(x => x.TeamLink.Name),
(table, exception) =>
{
Teams = table;
Dispatcher.Invoke(() => { ReloadTeams(); });
}
);
new Client().Query(
LookupFactory.DefineFilter(),
LookupFactory.DefineColumns(),
LookupFactory.DefineSort(),
(table, exception) =>
{
Jobs = table;
Dispatcher.Invoke(() => { ReloadJobs(); });
}
);
}
private void ReloadTeams()
{
var lookups = new Dictionary
{
{ Guid.Empty, "" }
};
foreach (var row in Teams.Rows)
lookups[row.Get(x => x.TeamLink.ID)] = row.Get(x => x.TeamLink.Name);
TeamCombo.ItemsSource = null;
TeamCombo.ItemsSource = lookups;
}
private void ReloadEmployees(Guid teamid)
{
var lookups = new Dictionary();
foreach (var row in Employees.Rows)
{
var bInTeam = Teams == null || teamid == Guid.Empty || Teams.Rows.Any(r =>
r.Get(x => x.TeamLink.ID).Equals(teamid) &&
r.Get(x => x.EmployeeLink.ID).Equals(row.Get(x => x.ID)));
if (bInTeam)
lookups[row.Get(x => x.ID)] = row.Get(x => x.Name);
}
EmployeeChecks.ItemsSource = null;
EmployeeChecks.ItemsSource = lookups;
if (Recipient != Guid.Empty)
foreach (KeyValuePair pair in EmployeeChecks.Items)
if (pair.Key == Recipient)
{
EmployeeChecks.SelectedItem = pair;
EmployeeChecks.SelectedItems.Add(pair);
ReloadNames();
break;
}
}
private void ReloadJobs()
{
var lookups = new Dictionary { { Guid.Empty, "(No Job Selected)" } };
foreach (var row in Jobs.Rows)
lookups[row.Get(x => x.ID)] =
string.Format("{0}: {1}", row.Get(x => x.JobNumber), row.Get(x => x.Name));
JobDetails.ItemsSource = null;
JobDetails.ItemsSource = lookups;
foreach (KeyValuePair pair in JobDetails.Items)
if (pair.Key == JobID)
{
JobDetails.SelectedItem = pair;
break;
}
}
// private void CheckOKButton(object sender, EventArgs e)
// {
// OK.IsEnabled = (EmployeeChecks.SelectedItems.Count > 0) && !String.IsNullOrWhiteSpace(TitleEdit.Text) && !String.IsNullOrWhiteSpace(ContentEdit.Text);
// }
private void CheckOKButton(object sender, SelectionChangedEventArgs e)
{
CheckOK(sender);
ReloadNames();
}
// private void CheckOKButton(object sender, TextChangedEventArgs e)
// {
// OK.IsEnabled = (EmployeeChecks.SelectedItems.Count > 0) && !String.IsNullOrWhiteSpace(TitleEdit.Text) && !String.IsNullOrWhiteSpace(ContentEdit.Text);
// }
private void CheckOK(object sender)
{
OK.IsEnabled = EmployeeChecks.SelectedItems.Count > 0 && !string.IsNullOrWhiteSpace(TitleEdit.Text) &&
!string.IsNullOrWhiteSpace(ContentEdit.Text);
}
private void EmployeeChecks_ItemChecked(object sender, ItemCheckedEventArgs e)
{
OK.IsEnabled = EmployeeChecks.SelectedItems.Count > 0 && !string.IsNullOrWhiteSpace(TitleEdit.Text) &&
!string.IsNullOrWhiteSpace(ContentEdit.Text);
ReloadNames();
}
private void ReloadNames()
{
var names = new List();
foreach (KeyValuePair pair in EmployeeChecks.SelectedItems)
names.Add(pair.Value);
Recipients.Text = string.Join("; ", names);
}
private void OK_Click(object sender, RoutedEventArgs e)
{
using (new WaitCursor())
{
var me = new Client().Load(new Filter(x => x.UserID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
var updates = new List();
foreach (KeyValuePair item in EmployeeChecks.SelectedItems)
{
var notification = new Notification { Title = TitleEdit.Text, Description = ContentEdit.Text };
notification.Sender.ID = me != null ? me.ID : Guid.Empty;
notification.Employee.ID = item.Key;
if (JobDetails.SelectedItem != null)
{
var job = (KeyValuePair)JobDetails.SelectedItem;
notification.Job.ID = job.Key;
}
updates.Add(notification);
}
new Client().Save(updates, "Sent Notification");
}
DialogResult = true;
Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void TeamCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var pair = (KeyValuePair)TeamCombo.SelectedItem;
ReloadEmployees(pair.Key);
}
private void SelectAll_Checked(object sender, RoutedEventArgs e)
{
EmployeeChecks.SelectedItems.Clear();
foreach (var item in EmployeeChecks.Items)
EmployeeChecks.SelectedItems.Add(item);
}
private void SelectAll_Unchecked(object sender, RoutedEventArgs e)
{
EmployeeChecks.SelectedItems.Clear();
}
private void TitleEdit_LostFocus(object sender, RoutedEventArgs e)
{
}
private void LostFocus(object sender, RoutedEventArgs e)
{
CheckOK(sender);
}
}
}