﻿using Rodrigo_banach.Context;
using Rodrigo_banach.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Rodrigo_banach.Controllers
{
    public class CategoriesController : Controller
    {
        private readonly EFContext _context = new EFContext();

        // GET: Category
        public ActionResult Index()
        {

                return View(_context
                .Categories
                .OrderBy(s => s.Name));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Category category)
        {
            _context.Categories.Add(category);
            _context.SaveChanges();

            return RedirectToAction("Index");
        }


        public ActionResult Edit(long? id)
        {
               if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var category = _context.Categories.Find(id.Value);

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            return View(category);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Entry(category).State = EntityState.Modified;
                _context.SaveChanges();
            }

            return RedirectToAction("Index");
        }


        public ActionResult Details(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //  var category = _context.Categories.Find(id.Value);
            var category = _context
                .Categories
                .Where(f => f.CategoryId == id)
                .Include("Products.Supplier")
                .First();


            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            return View(category);
        }

    
        public ActionResult Delete(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var category = _context.Categories.Find(id);

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            return View(category);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(long id)
        {
            Category categories = _context.Categories.Find(id);

            _context.Categories.Remove(categories);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}







