using FSI.Prj.Mgt.Data;
using FSI.Prj.Mgt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using X.PagedList;

namespace FSI.Prj.Mgt.Controllers
{
    public class OrganizationController : Controller
    {
        private readonly ApplicationDbContext _context;

        public OrganizationController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {

            var organizations = _context.Organizations.ToList();

            var org = organizations.Where(x => x.ParentId == 0 || x.ParentId == null).FirstOrDefault();

            SetChildren(org, organizations);

            return View(org);
        }

        private void SetChildren(Organization model, List<Organization> organizations)
        {
            var childs = organizations.Where(x => x.ParentId == model.Id).ToList();

            foreach (var item in childs)
            {
                SetChildren(item, organizations);
                model.Childs.Add(item);
            }
        }


    }
}