using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using TelerikAspNetCoreApp10.Models;

namespace TelerikAspNetCoreApp10.Controllers
{
    public class GridController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult _Child_Grid(int employeeID)
        {
            return PartialView(employeeID);
        }

        public virtual List<ExtendedEmployeeViewModel> GetAllParents()
        {
            List<ExtendedEmployeeViewModel> results = Enumerable.Range(1, 2)
                .Select(i => new ExtendedEmployeeViewModel
                {
                    EmployeeID = i,
                    Address = "Address" + i,
                    Title = "Title" + i,
                    City = "City" + i,
                    Country = "Country" + i,
                    EmployeeName = "EmployeeName" + i,
                    FirstName = "FirstName" + i,
                    HomePhone = "Phone" + i,
                    LastName = "LastName" + i,
                    Notes = "Notes" + i,
                    ReportsTo = i % 2 == 0 ? 1 : 2
                })
                .ToList();

            return results;
        }
        public virtual List<OrderViewModel> GetAllChildren()
        {
            List<OrderViewModel> results = Enumerable.Range(1, 10)
                .Select(i => new OrderViewModel
                {
                    CustomerID = i.ToString(),
                    OrderDate = DateTime.Now,
                    ShippedDate = DateTime.Now,
                    OrderID = i,
                    EmployeeID = i % 2 == 0 ? 1 : 2,
                    ContactName = $"ContactName {i}",
                    Freight = i * 1.25M,
                    ShipAddress = "Address" + i,
                    ShipCity = "ShipCity" + i,
                    ShipName = "ShipName" + i,
                    ShipCountry = "ShipCountry" + i
                })
                .ToList();
            return results;
        }
        public ActionResult Parent_Read([DataSourceRequest] DataSourceRequest request)
        {
            var result = GetAllParents();

            var dsResult = result.ToDataSourceResult(request);
            return Json(dsResult);
        }
        public ActionResult Children_Read([DataSourceRequest] DataSourceRequest request, int employeeID)
        {
            var result = GetAllChildren()
                .Where(order => order.EmployeeID == employeeID)
                .ToList();

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