using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Wierszowki.Core.Linq;

namespace Wierszowki.Controllers
{
    public class MagazineController : Controller
    {
        private readonly WierszowkiDataContext _context;

        public MagazineController()
        {
            _context = new WierszowkiDataContext();
        }

        [Authorize]
        public JsonResult FindIssuesById(int? id)
        {
            var magazineId = 0;
            if (id.HasValue)
                magazineId = id.Value;

            var issues = _context.FindIssuesByMagazineId(magazineId,0);

            var selectListItems = new List<SelectListItem>
                                      {new SelectListItem {Selected = true, Text = "-- proszę wybrać --", Value = "0"}};
            foreach (var issue in issues)
            {
                selectListItems.Add(new SelectListItem
                                        {Selected = false, Text = issue.Number.ToString(), Value = issue.Id.ToString()});
            }

            return Json(selectListItems);
        }
    }
}
