using DropDownTreeEditorDefaultValue.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DropDownTreeEditorDefaultValue.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult ReadDropDownTree(int? Value)
        {
            var result = new List<ItemViewModel>()
            {
                new ItemViewModel() { Value = 1, EmployeeName = "One", HasChildren = true },
                new ItemViewModel() { Value = 2, EmployeeName = "Two", HasChildren = true },
                new ItemViewModel() { Value = 3, EmployeeName = "Three" },
                new ItemViewModel() { Value = 4, EmployeeName = "Four", ParentId = 1 },
                new ItemViewModel() { Value = 5, EmployeeName = "Five", ParentId = 1 },
                new ItemViewModel() { Value = 6, EmployeeName = "Six", ParentId = 2, HasChildren = true },
                new ItemViewModel() { Value = 7, EmployeeName = "Seven", ParentId = 6 }
            };

            if (Value != null)
            {
                result = result.Where(i => i.ParentId == Value).ToList();
            }
            else
            {
                result = result.Where(i => i.ParentId == null).ToList();
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
