using C1.Web.Mvc;
using MvcExplorer.Models;
using Microsoft.AspNetCore.Mvc;

namespace MvcExplorer.Controllers
{
    partial class TreeViewController : Controller
    {
        private readonly C1NWindEntities _db;

        public TreeViewController(C1NWindEntities db)
        {
            _db = db;
        }

        // GET: LazyLoad
        public ActionResult LazyLoading()
        {
            return View();
        }

        private static ActionResult C1TreeViewJson(Controller controller, object data)
        {
            return controller.Json(data, new Newtonsoft.Json.JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()
            });
        }

        public ActionResult Load()
        {
            return C1TreeViewJson(this, EmployeeEx.GetEmployees(null, _db.Employees));
        }

        public ActionResult LazyLoading_LoadAction([C1JsonRequest]TreeNode node)
        {
            var leaderID = (int?)node.DataItem["EmployeeID"];
            return C1TreeViewJson(this, EmployeeEx.GetEmployees(leaderID, _db.Employees));
        }
    }
}