/// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="benevolenceTypeId">The benevolence type identifier.</param>
        private void ShowDetail(int benevolenceTypeId)
        {
            pnlDetails.Visible = true;
            var             rockContext     = new RockContext();
            BenevolenceType benevolenceType = null;

            if (benevolenceTypeId != 0)
            {
                benevolenceType   = new BenevolenceTypeService(new RockContext()).Get(benevolenceTypeIdPageParameter);
                lActionTitle.Text = ActionTitle.Edit(BenevolenceType.FriendlyTypeName).FormatAsHtmlTitle();
                pdAuditDetails.SetEntity(benevolenceType, ResolveRockUrl("~"));
            }

            if (benevolenceType == null)
            {
                benevolenceType = new BenevolenceType
                {
                    Id                   = 0,
                    IsActive             = true,
                    ShowFinancialResults = true
                };

                lActionTitle.Text = ActionTitle.Add(BenevolenceType.FriendlyTypeName).FormatAsHtmlTitle();

                // hide the panel drawer that show created and last modified dates
                pdAuditDetails.Visible = false;
            }

            tbName.Text                    = benevolenceType.Name;
            tbDescription.Text             = benevolenceType.Description;
            ceLavaTemplate.Text            = benevolenceType.RequestLavaTemplate;
            cbShowFinancialResults.Checked = benevolenceType.ShowFinancialResults;
            cbIsActive.Checked             = benevolenceType.IsActive;

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;

            if (!IsUserAuthorized(Authorization.EDIT))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(BenevolenceType.FriendlyTypeName);
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View(BenevolenceType.FriendlyTypeName);
                btnCancel.Text    = "Close";
            }

            tbName.ReadOnly         = readOnly;
            tbDescription.ReadOnly  = readOnly;
            ceLavaTemplate.ReadOnly = readOnly;
            cbIsActive.Enabled      = !readOnly;

            SetHighlightLabelVisibility(benevolenceType, readOnly);

            btnSave.Visible = !readOnly;
        }
        /// <summary>
        /// Handles the Delete event of the gBenevolenceType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gBenevolenceType_Delete(object sender, RowEventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                rockContext.WrapTransaction(action: () =>
                {
                    var benevolenceWorkflowService = new BenevolenceWorkflowService(rockContext);
                    var benevolenceTypeService     = new BenevolenceTypeService(rockContext);
                    var authService = new AuthService(rockContext);
                    BenevolenceType benevolenceType = benevolenceTypeService.Get(e.RowKeyId);

                    if (benevolenceType != null)
                    {
                        // Do not allow deletions if the person is not authorized
                        if (!benevolenceType.IsAuthorized(Authorization.ADMINISTRATE, this.CurrentPerson))
                        {
                            mdGridWarning.Show("You are not authorized to delete this Benevolence type.", ModalAlertType.Information);
                            return;
                        }

                        // var benevolenceRequests = new Service<BenevolenceRequest>( rockContext ).Queryable().All( a => a.BenevolenceTypeId == BenevolenceType.Id );
                        var benevolenceRequests       = benevolenceType.BenevolenceRequests.ToList();
                        var benevolenceRequestService = new BenevolenceRequestService(rockContext);

                        string errorMessageBenevolenceRequest = string.Empty;

                        foreach (var benvolenceRequest in benevolenceRequests)
                        {
                            if (!benevolenceRequestService.CanDelete(benvolenceRequest, out errorMessageBenevolenceRequest))
                            {
                                mdGridWarning.Show(errorMessageBenevolenceRequest, ModalAlertType.Information);
                                return;
                            }

                            benevolenceRequestService.Delete(benvolenceRequest);
                        }

                        // Save deleting the benevolence requests for the benevolence type id
                        rockContext.SaveChanges();

                        string errorMessageBenevolenceType;
                        if (!benevolenceTypeService.CanDelete(benevolenceType, out errorMessageBenevolenceType))
                        {
                            mdGridWarning.Show(errorMessageBenevolenceType, ModalAlertType.Information);
                            return;
                        }

                        benevolenceTypeService.Delete(benevolenceType);
                        rockContext.SaveChanges();

                        // ToDo: benevolenceWorkflowService.RemoveCachedTriggers();
                    }
                });
            }

            BindGrid();
        }
        /// <summary>
        /// Sets the highlight label visibility.
        /// </summary>
        /// <param name="benevolenceType">The benevolence type.</param>
        private void SetHighlightLabelVisibility(BenevolenceType benevolenceType, bool readOnly)
        {
            if (readOnly)
            {
                // if we are just showing readonly detail of the group, we don't have to worry about the highlight labels changing while editing on the client
                hlInactive.Visible = !benevolenceType.IsActive;
            }
            else
            {
                // in edit mode, the labels will have javascript handle if/when they are shown
                hlInactive.Visible = true;
            }

            if (benevolenceType.IsActive)
            {
                hlInactive.Style[HtmlTextWriterStyle.Display] = "none";
            }
        }
        /// <summary>
        /// Initializes the workflow grid.
        /// </summary>
        /// <param name="benevolenceTypeId">The benevolence type identifier.</param>
        private void InitializeWorkflowGrid(int benevolenceTypeId)
        {
            BenevolenceType benevolenceType = null;
            var             rockContext     = new RockContext();

            if (!benevolenceTypeId.Equals(0))
            {
                benevolenceType = new BenevolenceTypeService(rockContext).Get(benevolenceTypeId);
            }

            if (benevolenceType == null)
            {
                benevolenceType = new BenevolenceType {
                    Id = 0
                };
            }

            WorkflowStateModel = new List <BenevolenceWorkflow>();
            foreach (var benevolenceWorkflow in benevolenceType.BenevolenceWorkflows)
            {
                var modelState = new BenevolenceWorkflow
                {
                    Id   = benevolenceWorkflow.Id,
                    Guid = benevolenceWorkflow.Guid,
                    BenevolenceTypeId = benevolenceTypeId,
                    QualifierValue    = benevolenceWorkflow.QualifierValue,
                    TriggerType       = benevolenceWorkflow.TriggerType,
                    WorkflowTypeId    = benevolenceWorkflow.WorkflowTypeId,
                    WorkflowType      = benevolenceWorkflow.WorkflowType
                };

                WorkflowStateModel.Add(modelState);
            }

            gBenevolenceTypeWorkflows.DataKeyNames      = new string[] { "Guid" };
            gBenevolenceTypeWorkflows.Actions.ShowAdd   = true;
            gBenevolenceTypeWorkflows.Actions.AddClick += gBenevolenceTypeWorkflows_Add;
            gBenevolenceTypeWorkflows.GridRebind       += gBenevolenceTypeWorkflows_GridRebind;
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate();
            BenevolenceType benevolenceType = null;

            using (var rockContext = new RockContext())
            {
                var benevolenceService         = new BenevolenceTypeService(rockContext);
                var benevolenceWorkflowService = new BenevolenceWorkflowService(rockContext);

                var benevolenceTypeId = benevolenceTypeIdPageParameter;

                if (benevolenceTypeId != 0)
                {
                    benevolenceType = benevolenceService.Get(benevolenceTypeId);
                }

                if (benevolenceType == null)
                {
                    // Check for existing
                    var existingBenevolence = benevolenceService.Queryable()
                                              .Where(d => d.Name == tbName.Text)
                                              .FirstOrDefault();

                    if (existingBenevolence != null)
                    {
                        nbDuplicateDevice.Text    = $"A benevolence type already exists with the name '{existingBenevolence.Name}'. Please use a different benevolence type name.";
                        nbDuplicateDevice.Visible = true;
                    }
                    else
                    {
                        benevolenceType = new BenevolenceType();
                        benevolenceService.Add(benevolenceType);
                    }
                }

                if (benevolenceType != null)
                {
                    benevolenceType.Name                 = tbName.Text;
                    benevolenceType.Description          = tbDescription.Text;
                    benevolenceType.RequestLavaTemplate  = ceLavaTemplate.Text;
                    benevolenceType.ShowFinancialResults = cbShowFinancialResults.Checked;
                    benevolenceType.IsActive             = cbIsActive.Checked;

                    // remove any workflows that were removed in the UI
                    var uiWorkflows = WorkflowStateModel.Select(l => l.Guid);

                    foreach (var benevolenceWorkflow in benevolenceType.BenevolenceWorkflows.Where(l => !uiWorkflows.Contains(l.Guid)).ToList())
                    {
                        benevolenceType.BenevolenceWorkflows.Remove(benevolenceWorkflow);
                        benevolenceWorkflowService.Delete(benevolenceWorkflow);
                    }

                    // Add or Update workflows from the UI
                    foreach (var workflowStateModel in WorkflowStateModel)
                    {
                        BenevolenceWorkflow benevolenceWorkflow = benevolenceType.BenevolenceWorkflows
                                                                  .Where(b => !workflowStateModel.Guid.Equals(Guid.Empty) && b.Guid == workflowStateModel.Guid).FirstOrDefault();

                        if (benevolenceWorkflow == null)
                        {
                            benevolenceWorkflow = new BenevolenceWorkflow
                            {
                                BenevolenceTypeId = benevolenceTypeId
                            };
                            benevolenceType.BenevolenceWorkflows.Add(benevolenceWorkflow);
                        }

                        // Set the properties on the state model
                        benevolenceWorkflow.CopyPropertiesFrom(workflowStateModel);
                    }

                    if (!benevolenceType.IsValid)
                    {
                        // Controls will render the error messages
                        return;
                    }

                    rockContext.WrapTransaction(() =>
                    {
                        rockContext.SaveChanges();
                    });

                    BenevolenceWorkflowService.RemoveCachedTriggers();
                    NavigateToParentPage();
                }
            }
        }