| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 | using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Media.Imaging;using Comal.Classes;using InABox.Clients;using InABox.Core;using InABox.DynamicGrid;using InABox.WPF;using sun.net.www;using Syncfusion.Windows.Controls.RichTextBoxAdv;using static ICSharpCode.AvalonEdit.Document.TextDocumentWeakEventManager;using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;namespace PRSDesktop;/// <summary>///     Interaction logic for NotificationPanel.xaml/// </summary>public partial class NotificationPanel : UserControl, IPanel<Notification>{    private readonly Button Archive;    private readonly Button AttachToJob;    private readonly Button CreateDelivery;    private readonly Button CreateRequi;    private readonly Button CreateSetout;    private readonly Button CreateTask;    private readonly List<Tuple<string, BitmapImage>> folders = new()    {        new("Inbox", PRSDesktop.Resources.download.AsBitmapImage()),        new("Sent", PRSDesktop.Resources.upload.AsBitmapImage()),        new("Archive", PRSDesktop.Resources.box.AsBitmapImage())    };    private readonly Button Forward;    private readonly Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid))        .FirstOrDefault();    private readonly DynamicDataGrid<Notification> Notifications;    private readonly Button Reopen;    //Button WriteNew = null;    private readonly Button Reply;    private readonly Button ViewEntity;    private readonly Button ViewForm;    private readonly Button ViewJob;    public NotificationPanel()    {        InitializeComponent();        Notifications = new DynamicDataGrid<Notification>();        Notifications.HiddenColumns.Add(x => x.Sender.ID);        Notifications.HiddenColumns.Add(x => x.Employee.ID);        Notifications.HiddenColumns.Add(x => x.Job.ID);        Notifications.HiddenColumns.Add(x => x.EntityType);        Notifications.HiddenColumns.Add(x => x.EntityID);        //Notifications.HiddenColumns.Add(x => x.Kanban.ID);        //Notifications.HiddenColumns.Add(x => x.Setout.ID);        //Notifications.HiddenColumns.Add(x => x.Requisition.ID);        //Notifications.HiddenColumns.Add(x => x.Delivery.ID);        //Notifications.HiddenColumns.Add(x => x.LeaveRequestLink.ID);        Notifications.HiddenColumns.Add(x => x.Closed);        Notifications.Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns,            DynamicGridOption.MultiSelect);        Notifications.SetValue(Grid.RowProperty, 0);        Notifications.SetValue(Grid.ColumnProperty, 1);        Notifications.Margin = new Thickness(0);        Notifications.OnReload += Notifications_OnReload;        Notifications.OnSelectItem += Notifications_OnSelectItem;        Notifications.OnValidate += Notifications_OnValidate;        Notifications.OnCreateItem += Notifications_OnCreateItem;        //WriteNew = CreateButton("Write New", null, WriteNewClick);        //WriteNew.Visibility = Visibility.Visible;        //WriteNew.Margin = new Thickness(WriteNew.Margin.Left, WriteNew.Margin.Top, 10.0F, WriteNew.Margin.Bottom);        Reply = CreateButton("Reply", null, ReplyClick);        Forward = CreateButton("Forward", null, ForwardClick);        ViewForm = CreateButton("View Form", null, ViewFormClick);        ViewEntity = CreateButton("View Item", null, ViewEntityClick);        AttachToJob = CreateButton("Attach To Job", null, AttachToJobClick);        ViewJob = CreateButton("View Job", null, ViewJobClick);        CreateTask = CreateButton("Create Task", null, CreateTaskClick);        CreateSetout = CreateButton("Create Setout", null, CreateSetoutClick);        CreateRequi = CreateButton("Create Requi", null, CreateRequiClick);        CreateDelivery = CreateButton("Create Delivery", null, CreateDeliveryClick);        Archive = CreateButton("Archive", null, ArchiveClick);        Archive.Margin = new Thickness(10.0F, Archive.Margin.Top, Archive.Margin.Right, Archive.Margin.Bottom);        Reopen = CreateButton("Reopen", null, ReopenClick);        Reopen.Margin = new Thickness(10.0F, Reopen.Margin.Top, Reopen.Margin.Right, Reopen.Margin.Bottom);        Layout.Children.Add(Notifications);        Folders.ItemsSource = folders;    }    private void Notifications_OnCreateItem(object sender, object item)    {        if (item is not Notification notification)            return;        notification.Sender.ID = me.ID;    }    private void Notifications_OnValidate(object sender, Notification[] items, List<string> errors)    {        foreach(var item in items)        {            if(!item.Sender.IsValid())            {                errors.Add("[Sender] may not be blank!");            }            if(!item.Employee.IsValid())            {                errors.Add("[Employee] may not be blank!");            }        }    }    public bool IsReady { get; set; }    public event DataModelUpdateEvent OnUpdateDataModel;    private Button CreateButton(string caption, BitmapImage? umage, Func<Button, CoreRow[], bool> action)    {        var result = Notifications.AddButton(caption, umage, action);        result.Width = 100.0F;        result.Visibility = Visibility.Collapsed;        return result;    }    private void Notifications_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)    {        var singlerow = e.Rows?.Length == 1;        //WriteNew.Visibility = (Folders.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;        var row = e.Rows?.FirstOrDefault();        if (row != null)        {            var hasjob = row.IsEntityLinkValid<Notification, JobLink>(x => x.Job);            var entitytype = row.Get<Notification, string>(x => x.EntityType);            NotificationUtils.MakeVisible(Reply, 0, 5, singlerow, Folders.SelectedIndex == 0 || Folders.SelectedIndex == 2);            NotificationUtils.MakeVisible(Forward, 5, 10, singlerow);            NotificationUtils.MakeVisible(ViewForm, 0, 5, singlerow, NotificationUtils.IsDigitalForm(entitytype));            NotificationUtils.MakeVisible(ViewEntity, 0, 10, singlerow, !string.IsNullOrWhiteSpace(entitytype));            ViewEntity.Content = string.Format("View {0}", NotificationUtils.GetEntityType(entitytype)?.GetCaption());            NotificationUtils.MakeVisible(AttachToJob, 0, 10, singlerow, !hasjob);            NotificationUtils.MakeVisible(ViewJob, 0, 10, singlerow, hasjob, !string.Equals(entitytype, typeof(Job).EntityName()));            var create = NotificationUtils.MakeVisible(CreateTask, 0, 5, singlerow, !string.Equals(entitytype, typeof(Kanban).EntityName()));            create = NotificationUtils.MakeVisible(CreateSetout, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Setout).EntityName())) ||                     create;            create =                NotificationUtils.MakeVisible(CreateRequi, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Requisition).EntityName())) ||                create;            create = NotificationUtils.MakeVisible(CreateDelivery, 0, 5, singlerow, !string.Equals(entitytype, typeof(Delivery).EntityName())) ||                     create;            NotificationUtils.MakeVisible(Archive, create ? 10 : 0, 5, Folders.SelectedIndex == 0);            NotificationUtils.MakeVisible(Reopen, create ? 10 : 0, 5, Folders.SelectedIndex == 2);            new Client<Notification>().Query(                new Filter<Notification>(x => x.ID).IsEqualTo(row.Get<Notification, Guid>(x => x.ID)),                new Columns<Notification>(x => x.Description),                null,                (table, error) =>                {                    var drow = table?.Rows.FirstOrDefault();                    var desc = drow?.Get<Notification, string>(x => x.Description)?.Replace("background:NoColor;", "") ?? "";                    var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));                    Dispatcher.Invoke(() =>                    {                        Editor.Load(ms, FormatType.Html);                        //cursor.Dispose();                    });                }            );        }        else        {            NotificationUtils.MakeVisible(Reply, 0, 0, false);            NotificationUtils.MakeVisible(Forward, 0, 0, false);            NotificationUtils.MakeVisible(ViewForm, 0, 0, false);            NotificationUtils.MakeVisible(ViewEntity, 0, 0, false);            NotificationUtils.MakeVisible(AttachToJob, 0, 0, false);            NotificationUtils.MakeVisible(ViewJob, 0, 0, false);            NotificationUtils.MakeVisible(CreateTask, 0, 0, false);            NotificationUtils.MakeVisible(CreateSetout, 0, 0, false);            NotificationUtils.MakeVisible(CreateRequi, 0, 0, false);            NotificationUtils.MakeVisible(CreateDelivery, 0, 0, false);            NotificationUtils.MakeVisible(Archive, 0, 0, false);            NotificationUtils.MakeVisible(Reopen, 0, 0, false);            var ms = new MemoryStream(Encoding.ASCII.GetBytes(""));            Editor.Load(ms, FormatType.Html);        }    }    public void AddNotification(Notification notification)    {        Dispatcher.Invoke(() =>        {            if (IsInbox)            {                //Notifications.AddRow(notification);                Notifications.Refresh(false, true);            }        });    }    private bool IsInbox => Folders.SelectedIndex == 0;    private bool IsOutbox => Folders.SelectedIndex == 1;    private bool IsArchive => Folders.SelectedIndex == 2;    private void Notifications_OnReload(object sender, Filters<Notification> criteria, Columns<Notification> columns,        ref SortOrder<Notification>? sortby)    {        var filter = new Filter<Notification>(x => x.ID).IsEqualTo(Guid.Empty);        if (me != null)        {            if (IsInbox)                filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsEqualTo(DateTime.MinValue);            else if (IsOutbox)                filter = new Filter<Notification>(x => x.Sender.ID).IsEqualTo(me.ID);            else if (IsArchive)                filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsNotEqualTo(DateTime.MinValue);        }        criteria.Add(filter);        sortby = new SortOrder<Notification>(x => x.Created, SortDirection.Descending);    }    private void Folders_SelectionChanged(object sender, SelectionChangedEventArgs e)    {        if (IsReady)            using (new WaitCursor())            {                Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;                Notifications.Refresh(true, true);            }    }    #region IPanel<T> stuff    public void CreateToolbarButtons(IPanelHost host)    {        // host.CreatePanelAction(new PanelAction() { Caption = "Send Notification", Image = PRSDesktop.Resources.email, OnExecute = SendNotificationClick });    }    public void Setup()    {        Folders.SelectedIndex = 0;        Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;        Notifications.Refresh(true, false);    }    public void Shutdown()    {    }    public void Refresh()    {        Notifications.Refresh(false, true);    }    public string SectionName => "Notifications";    public DataModel DataModel(Selection selection)    {        var ids = Notifications.ExtractValues(c => c.ID, selection).ToArray();        return new BaseDataModel<Notification>(new Filter<Notification>(x => x.ID).InList(ids));    }    public Dictionary<Type, CoreTable> DataEnvironment()    {        return new Dictionary<Type, CoreTable>        {            { typeof(Notification), Notifications.Data }        };    }    public Dictionary<string, object[]> Selected()    {        return new Dictionary<string, object[]>();    }    #endregion    #region Actions    private Guid[] SelectedIDs => Notifications.ExtractValues(x => x.ID, Selection.Selected).ToArray();    private bool ViewFormClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.ViewForm(SelectedIDs);    }    private bool ViewEntityClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.ViewEntity(SelectedIDs);    }    private bool WriteNewClick(Button sender, CoreRow[] rows)    {        var form = new NotificationForm();        return form.ShowDialog() == true;    }    private bool ReplyClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.ReplyOrForward(SelectedIDs, "RE:", true, Folders.SelectedIndex == 1);    }    private bool ForwardClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.ReplyOrForward(SelectedIDs, "FW:", false, Folders.SelectedIndex == 1);    }    private bool CreateTaskClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.CreateTask(SelectedIDs, me.ID);    }    private bool AttachToJobClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.AttachToJob(SelectedIDs);    }    private bool ViewJobClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.ViewJob(SelectedIDs);    }    private bool CreateSetoutClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.CreateSetout(SelectedIDs);    }    private bool CreateRequiClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.CreateRequi(SelectedIDs, me.ID);    }    private bool CreateDeliveryClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.CreateDelivery(SelectedIDs);    }    private bool ArchiveClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.Archive(SelectedIDs);    }    private bool ReopenClick(Button sender, CoreRow[] rows)    {        return NotificationUtils.Reopen(SelectedIDs);    }    public void Heartbeat(TimeSpan time)    {    }    #endregion}
 |