﻿namespace EmployeesAndPartners.Web.Controllers
{
    using System.Web.Mvc;

    using Models.BusinessPartners;

    public class BusinessPartnersController : BaseController
    {
        public ActionResult Index(string sortOption, string sortType, int id = 1)
        {
            var model = this.LoadModel<BusinessPartnersGridModel, int>(id);
            model.BusinessPartners = model.GetPartnersByOptions(sortOption, sortType);
            return this.View(model);
        }

        public ActionResult GetPartnersGrid(string sortOption, string sortType, int id = 1)
        {
            var model = this.LoadModel<BusinessPartnersGridModel, int>(id).GetPartnersByOptions(sortOption, sortType);
            return this.PartialView("_PartnersGrid", model);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var model = this.LoadModel<BusinessPartnerModel, int>(id);

            return this.View(model);
        }

        [HttpGet]
        public ActionResult Create()
        {
            var model = this.LoadModel<BusinessPartnerModel, int>(0);

            return this.View(model);
        }

        public ActionResult Edit(int id)
        {
            var model = this.LoadModel<BusinessPartnerModel, int>(id);

            return this.View(model);
        }

        [HttpPost]
        public JsonResult Save(BusinessPartnerModel viewModel)
        {
            var result = this.LoadModel<BusinessPartnerModel, int>(0).Save(viewModel, this.ModelState);

            return this.Json(result); 
        }

        public ActionResult Delete(int id)
        {
            var model = this.LoadModel<BusinessPartnerModel, int>(id).Delete(id);

            return this.RedirectToAction("Index");
        }
    }
}