
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace TelerikMvcApp1
{
    public class Timesheet
    {
        public string Employee { get; set; }
        public float quarter1 { get; set; }
        public float quarter2 { get; set; }
        public float quarter3 { get; set; }
        public float quarter4 { get; set; }



       

    }
    public partial class SpreadsheetController : Controller
    {
        public List<Timesheet> CreateBasicTimeSheet()
        {
            List<Timesheet> tsList = new List<Timesheet>();
            Timesheet myTimeSheet;

            myTimeSheet = new Timesheet();
            myTimeSheet.Employee = "Employee A";
            myTimeSheet.quarter1 = 10F;
            myTimeSheet.quarter2 = 10F;
            myTimeSheet.quarter3 = 10F;
            myTimeSheet.quarter4 = 10F;
            tsList.Add(myTimeSheet);

            myTimeSheet = new Timesheet();
            myTimeSheet.Employee = "Employee B";
            myTimeSheet.quarter1 = 20F;
            myTimeSheet.quarter2 = 20F;
            myTimeSheet.quarter3 = 20F;
            myTimeSheet.quarter4 = 20F;
            tsList.Add(myTimeSheet);

            return tsList;
        }

        public ActionResult TimeSheet_Read([DataSourceRequest] DataSourceRequest request)
        {
            List<Timesheet> myTimeSheet = CreateBasicTimeSheet();
            return Json(myTimeSheet.ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeSheet_Update([DataSourceRequest] DataSourceRequest request,
            [Bind(Prefix = "models")]IEnumerable<Timesheet> TimesheetInfo)
        {
            if (TimesheetInfo != null && ModelState.IsValid)
            {
                // Do update stuff here
            }

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

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

            if (TimesheetInfo != null && ModelState.IsValid)
            {
                foreach (var ts in TimesheetInfo)
                {

                    // ADD Records to DB
                    results.Add(ts);
                }
            }

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

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeSheet_Destroy([DataSourceRequest] DataSourceRequest request,
            [Bind(Prefix = "models")]IEnumerable<Timesheet> TimesheetInfo)
        {
            foreach (var ts in TimesheetInfo)
            {
                //DELETE records from DB
            }

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

        [HttpPost]
        public ActionResult Data_Source_Proxy(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }
    }
}