using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PcInventory_v1_1.Models;
using PagedList;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;

namespace PcInventory_v1_1.Controllers
{ 
    public class AssetsController : Controller
    {
        private pcinvnetory db = new pcinvnetory();

        //
        // GET: /Assets/

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var assets = from s in db.Assets
                         select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                assets = assets.Where(s => s.AssetTag.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "Asset Desc":
                    assets = assets.OrderByDescending(s => s.AssetTag);
                    break;
                case "Device Approver":
                    assets = assets.OrderBy(s => s.DeviceApprover);
                    break;
                default:
                    assets = assets.OrderBy(s => s.SerialNumber);
                    break;
            }

            
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(assets.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Assets/Details/5

        public ViewResult Details(string id)
        {
            Asset asset = db.Assets.Find(id);
            return View(asset);
        }

        //
        // GET: /Assets/Create

        public ActionResult Create()
        {
            ViewBag.SectionKey = new SelectList(db.Sections, "SectionKey", "SectionDesciption");
            ViewBag.SupplierKey = new SelectList(db.Suppliers, "SupplierKey", "SupplierName");
            ViewBag.DeviceInfoKey = new SelectList(db.DeviceInformations, "DeviceInfoKey", "Make");
            return View();
        } 

        //
        // POST: /Assets/Create

        [HttpPost]
        public ActionResult Create(Asset asset)
        {
            if (ModelState.IsValid)
            {
                db.Assets.Add(asset);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.SectionKey = new SelectList(db.Sections, "SectionKey", "SectionDesciption", asset.SectionKey);
            ViewBag.SupplierKey = new SelectList(db.Suppliers, "SupplierKey", "SupplierName", asset.SupplierKey);
            ViewBag.DeviceInfoKey = new SelectList(db.DeviceInformations, "DeviceInfoKey", "Make", asset.DeviceInfoKey);
            return View(asset);
        }
        
        //
        // GET: /Assets/Edit/5
 
        public ActionResult Edit(string id)
        {
            Asset asset = db.Assets.Find(id);
            ViewBag.SectionKey = new SelectList(db.Sections, "SectionKey", "SectionDesciption", asset.SectionKey);
            ViewBag.SupplierKey = new SelectList(db.Suppliers, "SupplierKey", "SupplierName", asset.SupplierKey);
            ViewBag.DeviceInfoKey = new SelectList(db.DeviceInformations, "DeviceInfoKey", "Make", asset.DeviceInfoKey);
            return View(asset);
        }

        //
        // POST: /Assets/Edit/5

        [HttpPost]
        public ActionResult Edit(Asset asset)
        {
            if (ModelState.IsValid)
            {
                db.Entry(asset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SectionKey = new SelectList(db.Sections, "SectionKey", "SectionDesciption", asset.SectionKey);
            ViewBag.SupplierKey = new SelectList(db.Suppliers, "SupplierKey", "SupplierName", asset.SupplierKey);
            ViewBag.DeviceInfoKey = new SelectList(db.DeviceInformations, "DeviceInfoKey", "Make", asset.DeviceInfoKey);
            return View(asset);
        }

        //
        // GET: /Assets/Delete/5
 
        public ActionResult Delete(string id)
        {
            Asset asset = db.Assets.Find(id);
            return View(asset);
        }

        //
        // POST: /Assets/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Asset asset = db.Assets.Find(id);
            db.Assets.Remove(asset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

      
        public ActionResult BulkEdit([DataSourceRequest]DataSourceRequest request)

        {
            var emp = db.Employees;

          
             DataSourceResult result = emp.ToDataSourceResult(request);


             return Json(result, JsonRequestBehavior.AllowGet);
        }

        
    
    
    
    }
    

}