using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using MvcApp_Kendo.Models;

namespace MvcApp_Kendo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var titles = new ReckonPayrollEntities().Titles
                .Select(e => new Titles
                                 {
                                     TitleId = e.Id,
                                     TitleName = e.Name
                                 }).OrderBy(e => e.TitleName);
            ViewData["Titles"] = titles;
            ViewData["defaultTitle"] = titles.First();
            var lstEmp = EmpRepository.GetAllRecords();
            return View(lstEmp);
            //return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }
        public ActionResult PartialViewTreeView()
        {
            return PartialView();
        }

        public ActionResult PartialViewGrid()
        {
            var lstEmp = EmpRepository.GetAllRecords();
            ViewData["Titles"] = new ReckonPayrollEntities().Titles
                .Select(e => new Titles
                {
                    TitleId = e.Id,
                    TitleName = e.Name
                }).OrderBy(e => e.TitleName);
            return PartialView(lstEmp);
        }

        public ActionResult Products_Read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(EmpRepository.GetAllRecords().ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<EmployeeListModel> employeeListModels)
        {
            var results = new List<EmployeeListModel>();

            if (employeeListModels != null && ModelState.IsValid)
            {
                foreach (var employeeListModel in employeeListModels)
                {
                    EmpRepository.Add(employeeListModel);
                    results.Add(employeeListModel);
                }
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<EmployeeListModel> employeeListModels)
        {
            EmpRepository.Update(employeeListModels);

            //if (employeeListModels != null && ModelState.IsValid)
            //{
            //    foreach (var product in employeeListModels)
            //    {
            //        var target = EmpRepository.One(p => p.ProductID == product.ProductID);
            //        if (target != null)
            //        {
            //            target.ProductName = product.ProductName;
            //            target.UnitPrice = product.UnitPrice;
            //            target.UnitsInStock = product.UnitsInStock;
            //            target.LastSupply = product.LastSupply;
            //            target.Discontinued = product.Discontinued;
            //            EmpRepository.Update(target);
            //        }
            //    }
            //}

            return Json(ModelState.ToDataSourceResult());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<EmployeeListModel> employeeListModels)
        {
            //if (employeeListModels.Any())
            //{
            //    foreach (var product in employeeListModels)
            //    {
            //        EmpRepository.Delete(product);
            //    }
            //}
            EmpRepository.Delete(employeeListModels);
            return Json(ModelState.ToDataSourceResult());
        }
    }
}
