﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleCMS.Models;

namespace SimpleCMS.Controllers
{
    public class StaffController : Controller
    {
        SimpleCMSEntity storeDB = new SimpleCMSEntity();

        // GET: Staff
        public ActionResult Index()
        {
            var s = storeDB.Staffs;
            return View(s);
        }

        // GET: Staff/Details/5
        public ActionResult Details(int id)
        {
            var s = storeDB.Staffs.Find(id);
            return View(s);
        }

        // GET: Staff/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        // POST: Staff/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create(Staff s)
        {
            try
            {
                // TODO: Add insert logic here

                storeDB.Staffs.Add(s);
                foreach (string upload in Request.Files)
                {
                    var file = Request.Files[upload];
                    if (file == null || 0 >= file.ContentLength)
                        break;
                    var path = AppDomain.CurrentDomain.BaseDirectory + "uploads\\";
                    var filename = s.Name + s.Id + DateTime.Now.ToFileTime() + Path.GetExtension(file.FileName);
                    file.SaveAs(Path.Combine(path, filename));
                    s.Img = filename;
                }
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Staff/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            var s = storeDB.Staffs.Find(id);
            return View(s);
        }

        // POST: Staff/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var s = storeDB.Staffs.Find(id);
                foreach (string upload in Request.Files)
                {
                    var file = Request.Files[upload];
                    if(file == null || 0 >= file.ContentLength)
                        break;
                    var path = AppDomain.CurrentDomain.BaseDirectory + "uploads\\";
                    var filename = s.Name + s.Id + DateTime.Now.ToFileTime() + Path.GetExtension(file.FileName);
                    file.SaveAs(Path.Combine(path, filename));
                    s.Img = filename;
                }
                if (this.TryUpdateModel<Staff>(s))
                {
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return RedirectToAction("Error");
            }
        }

        // GET: Staff/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            var s = storeDB.Staffs.Find(id);
            return View(s);
        }

        // POST: Staff/Delete/5
        [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var s = storeDB.Staffs.Find(id);
                storeDB.Staffs.Remove(s);
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

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