using Kendo.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TelerikAspNetCoreApp543.Models;

namespace TelerikAspNetCoreApp543.Controllers
{
    public class GridController : Controller
    {
        private IEnumerable<OrderViewModel> GetData()
        {
            var data = Enumerable.Range(1, 50).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i * 10,
                OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                ShipName = "ShipName " + i%5,
                ShipCity = "ShipCity " + i
            }).ToList();
            return data;
        }
        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
        {
            var result = GetData();
            if (!request.Filters.Any())
            {
                var firstFilter = new FilterDescriptor("OrderID", FilterOperator.IsEqualTo, 22);
                var secondFilter = new FilterDescriptor("ShipName", FilterOperator.Contains, 3);
                var compositeFilter = new CompositeFilterDescriptor();
                compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
                compositeFilter.FilterDescriptors.Add(firstFilter);
                compositeFilter.FilterDescriptors.Add(secondFilter);
                request.Filters.Add(compositeFilter);
            }

            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }
    }
}
