using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Sarand.Core.Bus; using Sarand.PatientMgr.Core.Bus; using Sarand.Win.Forms.Utils; using Sarand.Core.Base; using Sarand.PatientMgr.Core.App; using Sarand.PatientMgr.Win.Forms.Maint; using Sarand.PatientMgr.Win.Forms.Controls; using Sarand.PatientMgr.Win.Forms.TabPages; using Sarand.PatientMgr.Win.Forms.Filters; using Sarand.PatientMgr.Win.Forms.Base; namespace Sarand.PatientMgr.Win.Forms.Panels { //======================================================================= public partial class SpmPanel_Departments : SpmPanel_Items { //------------------------------------------------------------------- // Configuration. public enum PanelMode { Search, Maintain, } public new class Config : SpmPanel_Items.Config { public Config (PanelMode mode) { Mode = mode; // Defaults based on mode. DisplayFilter = false; // Mode == PanelMode.Maintain || Mode == PanelMode.Search; DisplayTabs = Mode == PanelMode.Maintain; DisplayActionButtons = Mode == PanelMode.Maintain; DisplayNavigationButtons = Mode == PanelMode.Search; } public PanelMode Mode { get; } = PanelMode.Maintain; public SpmForm SearchForm { get; set; } // Display options. public bool DisplayFilter { get; set; } = false; public bool DisplayContextMenu { get; set; } = true; public bool DisplayActionButtons { get; set; } = false; public bool DisplayNavigationButtons { get; set; } = false; public bool DisplayTabs { get; set; } = false; } //------------------------------------------------------------------- // Data members. private Config config = new Config(PanelMode.Search); private readonly List tabPages = new List(); //------------------------------------------------------------------- // Construction. private SpmPanel_Departments () : this(new Config(PanelMode.Search)) { } public SpmPanel_Departments (Config config) : base(config) { this.config = config; InitializeComponent(); } protected override void Dispose (bool disposing) { if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); } //------------------------------------------------------------------- // Properties. protected override String DefaultMessageTitle { get { return "Department"; } } public override int ItemTotal { get { return listViewDepartment.ItemTotal; } } public override int ItemCount { get { return listViewDepartment.ItemCount; } } public override int SelectedItemCount { get { return listViewDepartment.SelectedItemCount; } } public SysId SelectedItemId { get { return listViewDepartment.SelectedItemId; } } public List SelectedItemIds { get { return listViewDepartment.SelectedItemIds; } } public SpmDepartment SelectedDepartment { get { return listViewDepartment.SelectedDepartment; } } //------------------------------------------------------------------- // Configure. public void SetConfig (Config config) { base.SetConfig(config); this.config = config; ConfigurePanel(); } //------------------------------------------------------------------- // I n i t i a l i s a t i o n //------------------------------------------------------------------- protected override void OnCreateControl () { base.OnCreateControl(); if (this.IsDesignerHosted()) return; ConfigurePanel(); } private void ConfigurePanel () { if (!Created || this.IsDesignerHosted()) return; // Configure filter. if (config.DisplayFilter) { SpmItemListFilter_Department.Config configFilter = new SpmItemListFilter_Department.Config(); configFilter.Internals = Internals; listFilterDepartment.SetConfig(configFilter); } listFilterDepartment.Visible = config.DisplayFilter; // Configure list. SpmItemListView_Department.Config configList = new SpmItemListView_Department.Config(); configList.Internals = Internals; configList.Mode = SpmItemListView_Department.ListMode.General; listViewDepartment.SetConfig(configList); // Tabs. tabControl.TabPages.Clear(); tabControl.Visible = config.DisplayTabs; RefreshTabs(); // Configure buttons. buttonCreate.Visible = config.DisplayActionButtons && SecurityProfile.Admin.CanManageConfig(); buttonUpdate.Visible = config.DisplayActionButtons; buttonRemove.Visible = config.DisplayActionButtons && buttonCreate.Visible; buttonOk.Visible = config.DisplayNavigationButtons && config.Mode == PanelMode.Search; buttonCancel.Visible = config.DisplayNavigationButtons; // Complete form initialisation. RefreshDataItems(); SetControlActivity(); PositionControls(); } //------------------------------------------------------------------- // Initialise tabs. private void InitTabs () { String fn = GetType().Name + ".InitTabs"; tabControl.TabPages.Clear(); tabPages.Clear(); if (!config.DisplayTabs) { tabPages.Clear(); tabControl.TabPages.Clear(); tabControl.Visible = false; return; } // Selected Department. SpmDepartment department = SelectedDepartment; // Theatres. List panelTypes_Theatres = new List(); if (department?.HasFacilityType(Spm.FacilityType.Theatre) ?? false) panelTypes_Theatres.Add(SpmTabPage_Department.PanelType.Theatres); if (panelTypes_Theatres.Count > 0) { SpmTabPage_Department.Config configTabPage = new SpmTabPage_Department.Config(); configTabPage.Internals = Internals; configTabPage.ReadOnly = ReadOnly; configTabPage.PanelTypes.AddRange(panelTypes_Theatres); SpmTabPage_Department tabPage = new SpmTabPage_Department(configTabPage) { Text = "Theatres" }; tabPages.Add(tabPage); } // Clinics. List panelTypes_Clinics = new List(); if (department?.HasFacilityType(Spm.FacilityType.Clinic) ?? false) panelTypes_Clinics.Add(SpmTabPage_Department.PanelType.Clinics); if (panelTypes_Clinics.Count > 0) { SpmTabPage_Department.Config configTabPage = new SpmTabPage_Department.Config(); configTabPage.Internals = Internals; configTabPage.ReadOnly = ReadOnly; configTabPage.PanelTypes.AddRange(panelTypes_Clinics); SpmTabPage_Department tabPage = new SpmTabPage_Department(configTabPage) { Text = "Clinics" }; tabPages.Add(tabPage); } // Wards. List panelTypes_Wards = new List(); if (department?.HasFacilityType(Spm.FacilityType.Ward) ?? false) panelTypes_Wards.Add(SpmTabPage_Department.PanelType.Wards); if (panelTypes_Wards.Count > 0) { SpmTabPage_Department.Config configTabPage = new SpmTabPage_Department.Config(); configTabPage.Internals = Internals; configTabPage.ReadOnly = ReadOnly; configTabPage.PanelTypes.AddRange(panelTypes_Wards); SpmTabPage_Department tabPage = new SpmTabPage_Department(configTabPage) { Text = "Wards" }; tabPages.Add(tabPage); } // Updates. List panelTypes_Updates = new List(); panelTypes_Updates.Add(SpmTabPage_Department.PanelType.Updates); if (panelTypes_Updates.Count > 0) { SpmTabPage_Department.Config configTabPage = new SpmTabPage_Department.Config(); configTabPage.Internals = Internals; configTabPage.ReadOnly = ReadOnly; configTabPage.PanelTypes.AddRange(panelTypes_Updates); SpmTabPage_Department tabPage = new SpmTabPage_Department(configTabPage) { Text = "Updates" }; tabPages.Add(tabPage); } // Populate tab pages. tabControl.Visible = tabPages.Count > 0; foreach (var tabPage in tabPages) tabControl.TabPages.Add(tabPage); } //------------------------------------------------------------------- // P a n e l I n t e r f a c e //------------------------------------------------------------------- private String previouslySelectedTabPageText = ""; private void RefreshTabs () { if (tabControl.SelectedTab != null) previouslySelectedTabPageText = tabControl.SelectedTab.Text; // Reinitialise tabs whenever the selection changes in case the set of tab pages displayed needs to be changed. InitTabs(); // Select previous tab page if still present. if (!StringUtil.IsEmpty(previouslySelectedTabPageText)) { foreach (TabPage tabPage in tabControl.TabPages) { if (tabPage.Text.Equals(previouslySelectedTabPageText)) { tabControl.SelectTab(tabPage); break; } } } RefreshTabPages(); SetControlActivity(); PositionControls(); } private void RefreshTabPages () { // Selected Department - populated when needed. SpmDepartment department = null; // Refresh tabs. if (tabControl.Visible) { if (department == null) department = SelectedDepartment; SpmDepartment.Data departmentData = department != null ? department.GetData() : new SpmDepartment.Data(); tabPages.PopulateToControls(departmentData); } } //------------------------------------------------------------------- // F i l t e r //------------------------------------------------------------------- public bool ParamsAreValid (SpmDepartment.Params prmDepartment) { return true; } public SpmDepartment.Params GetParams () { SpmDepartment.Params prmDepartment = new SpmDepartment.Params(); // Sort columns. prmDepartment.SortColumns.AddRange(listViewDepartment.GetSortColumns()); return prmDepartment; } //------------------------------------------------------------------- // A c t i o n s //------------------------------------------------------------------- // Refresh. public override void RefreshDataItems () { SpmDepartment.Params prmDepartment = GetParams(); if (!ParamsAreValid(prmDepartment)) prmDepartment = null; listViewDepartment.RefreshDepartments(prmDepartment); } public override void RefreshDataItem (SysId id) { listViewDepartment.RefreshDepartment(id); } public override void RemoveDataItem (SysId id) { listViewDepartment.RemoveItemById(id); } //------------------------------------------------------------------- // Create item. public override bool CanCreateItem () { if (ReadOnly) return false; if (!SecurityProfile.Admin.CanManageConfig()) return false; return true; } public override void CreateItem () { if (!CanCreateItem()) return; if (config.Mode == PanelMode.Search) return; // Create new Department. SpmForm_Department.Config configDepartment = new SpmForm_Department.Config(); using (var form = new SpmForm_Department(configDepartment)) { if (form.ShowDialog() == DialogResult.OK) { RefreshDataItem(form.NewItemId); InvokeItemCreatedEvent(Spm.DataItemType.Department, form.NewItemId); } } } //------------------------------------------------------------------- // View/Update item. public override bool CanViewItem () { return true; } public override bool CanUpdateItem () { if (ReadOnly) return false; if (!SecurityProfile.Admin.CanManageConfig()) return false; return true; } public override void UpdateItem (bool updateItem = true) { if (config.Mode == PanelMode.Search) return; SpmDepartment department = SelectedDepartment; if (SelectedItemCount != 1) return; if (!CanUpdateItem() && !CanViewItem()) return; if (!CanUpdateItem()) updateItem = false; if (department == null) { MessageConsole.ShowMessageWarning("Failed to retrieve selected item"); return; } SpmForm_Department.Config configDepartment = new SpmForm_Department.Config(); configDepartment.DepartmentToUpdate = department; configDepartment.ReadOnly = (config.ReadOnly || !updateItem); using (SpmForm_Department formDepartment = new SpmForm_Department(configDepartment)) { // Perform the update. if (formDepartment.ShowDialog() == DialogResult.OK) { // Department has been updated. RefreshDataItem(department.Id); listViewDepartment.RefreshDepartment(department.Id); InvokeItemUpdatedEvent(Spm.DataItemType.Department, department.Id); } } } //------------------------------------------------------------------- // Delete item. public override bool CanDeleteItem () { if (ReadOnly) return false; if (!SecurityProfile.Admin.CanManageConfig()) return false; return true; } public override void DeleteItem () { if (!CanDeleteItem()) return; if (config.Mode == PanelMode.Search) return; if (SelectedItemCount != 1) return; SpmDepartment department = SelectedDepartment; if (department == null) { MessageConsole.ShowMessageWarning("Failed to retrieve item for deletion."); return; } if (!DepartmentMgr.DepartmentCanBeDeleted(department)) { MessageConsole.ShowMessageError("Unable to remove department - " + DepartmentMgr.ErrorStateMessage + "."); return; } if (MessageConsole.ShowMessageConfirm("Are you sure that this department should be removed from the system?")) { if (DepartmentMgr.DeleteDepartment(department, DbUpdateInfo)) { int selectIdx = listViewDepartment.SelectedIndex; listViewDepartment.RemoveItemById(department.Id.ToString()); if (selectIdx >= listViewDepartment.ItemCount - 1) selectIdx--; listViewDepartment.SelectedIndices.Clear(); listViewDepartment.SelectedIndices.Add(selectIdx); } else MessageConsole.ShowMessageError("Failed to remove department - " + DepartmentMgr.ErrorStateMessage + "."); } } //------------------------------------------------------------------- // Import data. public override bool CanImportData () { return false; } public override void ImportData () { if (!CanImportData()) return; } //------------------------------------------------------------------- // Export data. public override bool CanExportData () { return false; } public override void ExportData () { } //------------------------------------------------------------------- // C o n t e x t M e n u //------------------------------------------------------------------- private SpmContextMenuStrip contextMenuStrip = new SpmContextMenuStrip(); private void RefreshContextMenu () { contextMenuStrip.Name = "Department"; contextMenuStrip.Items.Clear(); if (CanCreateItem()) contextMenuStrip.Items.Add("Create", null, CreateItem); if (SysId.IsValid(listViewDepartment.SelectedItemId)) { SpmDepartment department = SelectedDepartment; if (department != null) { if (CanUpdateItem() || CanDeleteItem()) { if (contextMenuStrip.Items.Count > 0) contextMenuStrip.Items.Add(new ToolStripSeparator()); contextMenuStrip.Items.Add(CanUpdateItem() ? "Update" : "View", null, UpdateItem); if (CanDeleteItem()) contextMenuStrip.Items.Add("Remove", null, DeleteItem); } } } if (CanExportData() || CanImportData()) { if (contextMenuStrip.Items.Count > 0) contextMenuStrip.Items.Add(new ToolStripSeparator()); if (CanImportData()) contextMenuStrip.Items.Add("Import", null, ImportData); if (CanExportData()) contextMenuStrip.Items.Add("Export", null, ExportData); } } //------------------------------------------------------------------- // C o n t r o l A c t i v i t y //------------------------------------------------------------------- protected override void SetControlActivity () { SetControlsReadOnly(); bool itemSelected = SysId.IsValid(SelectedItemId); listViewDepartment.Enabled = Enabled; buttonCreate.Enabled = Enabled; buttonUpdate.Enabled = Enabled && itemSelected; buttonRemove.Enabled = Enabled && itemSelected; buttonOk.Enabled = Enabled; buttonCancel.Enabled = Enabled; } protected override void SetControlsReadOnly () { } //------------------------------------------------------------------- // P o s i t i o n C o n t r o l s //------------------------------------------------------------------- protected override void PositionControls () { if (this.IsDesignerHosted() || !Created) return; Rectangle clientRect = ClientRectangle; //--------------------------------------------------------------- // Filter panel. if (listFilterDepartment.Visible) { listFilterDepartment.PositionAtTopLeftOf(this); listFilterDepartment.SetFullWidthOf(this); listFilterDepartment.ResizeToBottomControl(); listViewDepartment.PositionBelow(listFilterDepartment); } else { listViewDepartment.PositionAtTopLeftOf(this); } //--------------------------------------------------------------- // List view. int buttonLeft = clientRect.Width - buttonCreate.Width - buttonCreate.Margin.Right - Padding.Right; listViewDepartment.Left = listViewDepartment.Margin.Left; listViewDepartment.Width = buttonLeft - buttonCreate.Margin.Left - listViewDepartment.Left - listViewDepartment.Margin.Right; if (!(buttonCreate.Visible || buttonUpdate.Visible || buttonRemove.Visible || buttonOk.Visible || buttonCancel.Visible)) listViewDepartment.Width = clientRect.Width - Padding.Right - listViewDepartment.Left - listViewDepartment.Margin.Right; //--------------------------------------------------------------- // Tab control. if (tabControl.Visible) { tabControl.Left = listViewDepartment.Left; tabControl.Width = clientRect.Width - (tabControl.Left * 2); listViewDepartment.Height = (int)((clientRect.Height - listViewDepartment.Top) * 0.40); tabControl.Top = listViewDepartment.Bottom + listViewDepartment.Margin.Bottom + tabControl.Margin.Top; tabControl.Height = clientRect.Height - tabControl.Top - tabControl.Margin.Bottom - Padding.Bottom; } else { listViewDepartment.Height = clientRect.Height - listViewDepartment.Top - listViewDepartment.Margin.Bottom - Padding.Bottom; } //--------------------------------------------------------------- // Create button. int buttonTop = listViewDepartment.Top + listViewDepartment.HeaderHeight; buttonCreate.Top = buttonTop; buttonCreate.Left = buttonLeft; if (buttonCreate.Visible) buttonTop += buttonCreate.Height + buttonCreate.Margin.Bottom; // Update button. buttonUpdate.Top = buttonTop; buttonUpdate.Left = buttonLeft; if (buttonUpdate.Visible) buttonTop += buttonUpdate.Height + buttonUpdate.Margin.Bottom; // Remove button. buttonRemove.Top = buttonTop; buttonRemove.Left = buttonLeft; if (buttonRemove.Visible) buttonTop += buttonRemove.Height + buttonRemove.Margin.Bottom; //--------------------------------------------------------------- // Navigation buttons. // Cancel button. buttonCancel.Top = clientRect.Bottom - Padding.Bottom - buttonCancel.Margin.Bottom - buttonCancel.Height; buttonCancel.Left = buttonLeft; // OK button. buttonOk.Top = buttonCancel.Top - buttonOk.Height - buttonCancel.Padding.Top - buttonCancel.Margin.Bottom; buttonOk.Left = buttonLeft; if (buttonOk.Visible) buttonTop += buttonOk.Height + buttonOk.Margin.Bottom; //--------------------------------------------------------------- } //------------------------------------------------------------------- // P a n e l E v e n t s //------------------------------------------------------------------- // Filter. private void listFilterDepartment_FilterChanged (object sender, EventArgs e) { RefreshDataItems(); SetControlActivity(); } //------------------------------------------------------------------- // Item list. private void listViewDepartment_ColumnClick (object sender, ColumnClickEventArgs e) { RefreshDataItems(); } private void listViewDepartment_ItemCountChanged (object sender, Sarand.Win.Forms.Controls.SacsListView.ItemCountChangedEventArgs e) { InvokeItemCountChangedEvent(e); } private void listViewDepartment_SelectedIndexChanged (object sender, EventArgs e) { RefreshTabs(); InvokeItemSelectionChangedEvent(e); SetControlActivity(); } private void listViewDepartment_DoubleClick (object sender, EventArgs e) { if (config.Mode == PanelMode.Search) { //if (config.SearchForm != null) config.SearchForm.OnOK(); } else UpdateItem(); SetControlActivity(); } private void listViewDepartment_MouseUp (object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Right: // Refresh the context menu and display at pointer position. RefreshContextMenu(); contextMenuStrip.Show(listViewDepartment, new Point(e.X, e.Y), ToolStripDropDownDirection.Default); break; } SetControlActivity(); } //------------------------------------------------------------------- // Actions. private void buttonCreate_Click (object sender, EventArgs e) { CreateItem(); SetControlActivity(); } private void buttonUpdate_Click (object sender, EventArgs e) { UpdateItem(); SetControlActivity(); } private void buttonRemove_Click (object sender, EventArgs e) { DeleteItem(); SetControlActivity(); } private void buttonOk_Click (object sender, EventArgs e) { if (config.SearchForm != null) { //config.SearchForm.OnOK(); return; } SetControlActivity(); } private void buttonCancel_Click (object sender, EventArgs e) { if (config.SearchForm != null) { //config.SearchForm.OnCancel(); return; } SetControlActivity(); } //------------------------------------------------------------------- } //======================================================================= }