/// <summary>
        /// Shows the edit.
        /// </summary>
        /// <param name="attributeId">The attribute id.</param>
        protected void ShowEdit(int?exclusionId)
        {
            ScheduleCategoryExclusion exclusion = null;

            if (exclusionId.HasValue)
            {
                exclusion = new ScheduleCategoryExclusionService(new RockContext()).Get(exclusionId.Value);
            }

            if (exclusion != null)
            {
                tbTitle.Text            = exclusion.Title;
                drpExclusion.LowerValue = exclusion.StartDate;
                drpExclusion.UpperValue = exclusion.EndDate;
            }
            else
            {
                tbTitle.Text = string.Empty;
                drpExclusion.DelimitedValues = string.Empty;
            }

            hfIdValue.Value = exclusionId.ToString();

            modalDetails.Show();
        }
        /// <summary>
        /// Handles the Delete event of the rGrid 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 rGrid_Delete(object sender, RowEventArgs e)
        {
            var rockContext = new RockContext();
            var service     = new ScheduleCategoryExclusionService(rockContext);

            var exclusion = service.Get(e.RowKeyId);

            if (exclusion != null)
            {
                string errorMessage = string.Empty;
                if (service.CanDelete(exclusion, out errorMessage))
                {
                    int categoryId = exclusion.CategoryId;

                    service.Delete(exclusion);
                    rockContext.SaveChanges();

                    CategoryCache.Flush(categoryId);
                    Rock.CheckIn.KioskDevice.FlushAll();
                }
                else
                {
                    nbMessage.Text    = errorMessage;
                    nbMessage.Visible = true;
                }
            }

            BindGrid();
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var rockContext = new RockContext();
            var service     = new ScheduleCategoryExclusionService(rockContext);

            rGrid.DataSource = service.Queryable().AsNoTracking()
                               .Where(e => e.CategoryId == _categoryId.Value)
                               .OrderByDescending(e => e.StartDate)
                               .ToList();
            rGrid.DataBind();
        }
        /// <summary>
        /// Handles the SaveClick event of the modalDetails 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 modalDetails_SaveClick( object sender, EventArgs e )
        {
            int? exclusionId = hfIdValue.ValueAsInt();

            var rockContext = new RockContext();
            var service = new ScheduleCategoryExclusionService( rockContext );
            ScheduleCategoryExclusion exclusion = null;

            if ( exclusionId.HasValue )
            {
                exclusion = service.Get( exclusionId.Value );
            }

            if ( exclusion == null )
            {
                exclusion = new ScheduleCategoryExclusion();
                service.Add( exclusion );
            }

            exclusion.CategoryId = _categoryId.Value;
            exclusion.Title = tbTitle.Text;
            if ( drpExclusion.LowerValue.HasValue )
            {
                exclusion.StartDate = drpExclusion.LowerValue.Value.Date;
            }
            if ( drpExclusion.UpperValue.HasValue )
            {
                exclusion.EndDate = drpExclusion.UpperValue.Value.Date.AddDays( 1 ).AddSeconds( -1 );
            }

            if ( exclusion.IsValid )
            {
                rockContext.SaveChanges();

                CategoryCache.Flush( exclusion.CategoryId );
                Rock.CheckIn.KioskDevice.FlushAll();

                hfIdValue.Value = string.Empty;
                modalDetails.Hide();

                BindGrid();
            }
        }
        /// <summary>
        /// Handles the SaveClick event of the modalDetails 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 modalDetails_SaveClick(object sender, EventArgs e)
        {
            int?exclusionId = hfIdValue.ValueAsInt();

            var rockContext = new RockContext();
            var service     = new ScheduleCategoryExclusionService(rockContext);
            ScheduleCategoryExclusion exclusion = null;

            if (exclusionId.HasValue)
            {
                exclusion = service.Get(exclusionId.Value);
            }

            if (exclusion == null)
            {
                exclusion = new ScheduleCategoryExclusion();
                service.Add(exclusion);
            }

            exclusion.CategoryId = _categoryId.Value;
            exclusion.Title      = tbTitle.Text;
            if (drpExclusion.LowerValue.HasValue)
            {
                exclusion.StartDate = drpExclusion.LowerValue.Value.Date;
            }
            if (drpExclusion.UpperValue.HasValue)
            {
                exclusion.EndDate = drpExclusion.UpperValue.Value.Date.AddDays(1).AddSeconds(-1);
            }

            if (exclusion.IsValid)
            {
                rockContext.SaveChanges();

                CategoryCache.Flush(exclusion.CategoryId);
                Rock.CheckIn.KioskDevice.FlushAll();

                hfIdValue.Value = string.Empty;
                modalDetails.Hide();

                BindGrid();
            }
        }
 /// <summary>
 /// Binds the grid.
 /// </summary>
 private void BindGrid()
 {
     var rockContext = new RockContext();
     var service = new ScheduleCategoryExclusionService( rockContext );
     rGrid.DataSource = service.Queryable().AsNoTracking()
         .Where( e => e.CategoryId == _categoryId.Value )
         .OrderByDescending( e => e.StartDate )
         .ToList();
     rGrid.DataBind();
 }
        /// <summary>
        /// Shows the edit.
        /// </summary>
        /// <param name="attributeId">The attribute id.</param>
        protected void ShowEdit( int? exclusionId )
        {
            ScheduleCategoryExclusion exclusion = null;
            if ( exclusionId.HasValue )
            {
                exclusion = new ScheduleCategoryExclusionService( new RockContext() ).Get( exclusionId.Value );
            }

            if ( exclusion != null )
            {
                tbTitle.Text = exclusion.Title;
                drpExclusion.LowerValue = exclusion.StartDate;
                drpExclusion.UpperValue = exclusion.EndDate;
            }
            else
            {
                tbTitle.Text = string.Empty;
                drpExclusion.DelimitedValues = string.Empty;
            }

            hfIdValue.Value = exclusionId.ToString();

            modalDetails.Show();
        }
        /// <summary>
        /// Handles the Delete event of the rGrid 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 rGrid_Delete( object sender, RowEventArgs e )
        {
            var rockContext = new RockContext();
            var service = new ScheduleCategoryExclusionService( rockContext );

            var exclusion = service.Get( e.RowKeyId );
            if ( exclusion != null )
            {
                string errorMessage = string.Empty;
                if ( service.CanDelete( exclusion, out errorMessage ) )
                {
                    int categoryId = exclusion.CategoryId;

                    service.Delete( exclusion );
                    rockContext.SaveChanges();

                    CategoryCache.Flush( categoryId );
                    Rock.CheckIn.KioskDevice.FlushAll();
                }
                else
                {
                    nbMessage.Text = errorMessage;
                    nbMessage.Visible = true;
                }
            }

            BindGrid();
        }