public IActionResult ProductList(
            int? pageNumber, 
            int? pageSize,
            string query = "")
        {
            var itemsPerPage = pageSize.HasValue ? pageSize.Value : DefaultPageSize;
            var currentPageNum = pageNumber.HasValue ? pageNumber.Value  : 1;
            var offset = (itemsPerPage * currentPageNum) - itemsPerPage;
            var model = new ProductListViewModel();

            var filtered = this.allProducts.Where(p =>
                p.Category.StartsWith(query)
                );

            model.Products = filtered
                .Skip(offset)
                .Take(itemsPerPage)
                .ToList();

            model.Paging.CurrentPage = currentPageNum;
            model.Paging.ItemsPerPage = itemsPerPage;
            model.Paging.TotalItems = filtered.ToList().Count;
            model.Query = query; //TODO: sanitize

            return View(model);


        }
       // [Route("paging/{page?}")]
        public IActionResult Index(int? page)
        {
            
            var currentPageNum = page.HasValue ? page.Value : 1;
            var offset = (DefaultPageSize * currentPageNum) - DefaultPageSize;
            var model = new ProductListViewModel();
            model.Products = this.allProducts
                .Skip(offset)
                .Take(DefaultPageSize)
                .ToList();

            model.Paging.CurrentPage = currentPageNum;
            model.Paging.ItemsPerPage = DefaultPageSize;
            model.Paging.TotalItems = allProducts.Count;

            
            return View(model);

            
        }
        public IActionResult ProductList(
            int? pageNumber, 
            int? pageSize,
            string query = "")
        {
            int currentPageIndex = pageNumber.HasValue ? pageNumber.Value - 1 : 0;
            int itemsPerPage = pageSize.HasValue ? pageSize.Value : DefaultPageSize;

            var model = new ProductListViewModel();

            model.Products = this.allProducts.Where(p => 
            p.Category.StartsWith(query)
            ).ToPagedList(currentPageIndex, itemsPerPage);

            model.Paging.CurrentPage = pageNumber.HasValue ? pageNumber.Value : 1;
            model.Paging.ItemsPerPage = itemsPerPage;
            model.Paging.TotalItems = model.Products.TotalItemCount;
            model.Query = query; //TODO: sanitize

            return View(model);


        }
        public IActionResult BlogPagingDemo(
            int? pageNumber,
            string query = "")
        { 
            int itemsPerPage = 1;

            var currentPageNum = pageNumber.HasValue ? pageNumber.Value : 1;
            var offset = (itemsPerPage * currentPageNum) - itemsPerPage;

            var filtered = allProducts.Where(p =>
                p.Category.StartsWith(query)
            )
            .OrderByDescending(p => p.CreatedUtc)
            .ToList();
            
            var model = new ProductListViewModel();

            model.Products = filtered
            .Skip(offset)
            .Take(itemsPerPage)
            .ToList();

            model.Paging.CurrentPage = currentPageNum;
            model.Paging.ItemsPerPage = itemsPerPage;
            model.Paging.TotalItems = filtered.Count;
            //model.Paging.UseReverseIncrement = true;
            model.Query = query; //TODO: sanitize

            return View(model);


        }
        //[Route("paging/ajaxpage/{page?}")]
        public IActionResult AjaxPage(int? page)
        {
            ViewBag.Title = "Browse all products";
            
            var currentPageNum = page.HasValue ? page.Value : 1;
            var offset = (DefaultPageSize * currentPageNum) - DefaultPageSize;

            var model = new ProductListViewModel();

            model.Products = this.allProducts
                .Skip(offset)
                .Take(DefaultPageSize)
                .ToList();

            model.Paging.CurrentPage = currentPageNum;
            model.Paging.ItemsPerPage = DefaultPageSize;
            model.Paging.TotalItems = allProducts.Count;
            
            return PartialView("_ProductGrid", model);
        }
        public IActionResult IndexAjax()
        {
            
            var model = new ProductListViewModel();

            model.Products = this.allProducts
                .Take(DefaultPageSize)
                .ToList();
            

            model.Paging.CurrentPage = 1;
            model.Paging.ItemsPerPage = DefaultPageSize;
            model.Paging.TotalItems = allProducts.Count;

            if (HttpContext.Request.IsAjaxRequest())
            {
                return PartialView("_PagingModal", model);
            }
            return View(model);
        }
        public IActionResult ViewByCategory(string categoryName, int? page)
        {
            categoryName = categoryName ?? this.allCategories[0]; 
            var currentPageNum = page.HasValue ? page.Value : 1;
            var offset = (DefaultPageSize * currentPageNum) - DefaultPageSize;

            var filtered = this.allProducts.Where(p =>
                p.Category.Equals(categoryName)
                ).ToList();

            var model = new ProductListViewModel();

            model.Products = filtered
                .Skip(offset)
                .Take(DefaultPageSize)
                .ToList();

            model.Paging.CurrentPage = currentPageNum;
            model.Paging.ItemsPerPage = DefaultPageSize;
            model.Paging.TotalItems = filtered.Count;

            ViewBag.CategoryName = new SelectList(this.allCategories, categoryName);
            ViewBag.CategoryDisplayName = categoryName;

            return View("ProductsByCategory", model);
        }