//
 // GET: /admin/otherLocation/Edit/5
 public ActionResult Edit(int id)
 {
     var db = new SDES_DirectoryEntities();
     var location = db.otherLocations.Find(id);
     ViewBag.offices = Init.PopulateOfficesDropDownList(id);
     return View(location);
 }
 //
 // GET: /admin/office/Edit/5
 public ActionResult Edit(int id)
 {
     var db = new SDES_DirectoryEntities();
     var office = db.offices.Find(id);
     ViewBag.groups = Init.PopulateGroupsDropDownList(id);
     return View(office);
 }
 public static IEnumerable<SelectListItem> PopulateOfficesDropDownList(int? id = null)
 {
     var db = new SDES_DirectoryEntities();
     var groups = db.offices.OrderBy(x => x.officeName).ToList();
     var list = groups.Select(x => new SelectListItem
     {
         Text = x.officeName,
         Value = x.officeId.ToString(CultureInfo.InvariantCulture),
         Selected = (x.officeId == id)
     }).ToList();
     return list;
 }
 public static IEnumerable<SelectListItem> PopulateOfficesDropDownListStatic(string id = null)
 {
     var db = new SDES_DirectoryEntities();
     var groups = db.offices.OrderBy(x => x.officeName).ToList();
     var list = groups.Select(x => new SelectListItem
     {
         Text = x.officeName,
         Value = x.officeName,
         Selected = (x.officeName == id)
     }).ToList();
     return list;
 }
        public ActionResult Create(office office)
        {
            try
            {
                using(var db = new SDES_DirectoryEntities())
                {
                    db.offices.Add(office);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create(group addition)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.groups.Add(addition);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult CreateFromOffice(website site)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.websites.Add(site);
                    db.SaveChanges();
                }

                return RedirectToAction("Details", "office", new { id = site.officeId });
            }
            catch
            {
                return View(site);
            }
        }
        public ActionResult Edit(int id, social handle)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.Entry(handle).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToAction("Details", "office", new { id = handle.officeId });
            }
            catch
            {
                return View();
            }
        }
 public ActionResult Delete(int id, social handles)
 {
     try
     {
         using (var db = new SDES_DirectoryEntities())
         {
             var handle = db.socials.Find(id);
             db.Entry(handle).State = EntityState.Deleted;
             db.SaveChanges();
             return RedirectToAction("Details", "office", new { id = handle.officeId });
         }
     }
     catch
     {
         return View();
     }
 }
        public ActionResult Create(social handle)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.socials.Add(handle);
                    db.SaveChanges();
                }

                return RedirectToAction("Details", "office", new { id = handle.officeId });
            }
            catch
            {
                return View();
            }
        }
 public ActionResult Delete(int id, office office)
 {
     try
     {
         using (var db = new SDES_DirectoryEntities())
         {
             var office1 = db.offices.Find(id);
             db.Entry(office1).State = EntityState.Deleted;
             db.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     catch
     {
         return View(office);
     }
 }
 public ActionResult Delete(int id, staff memberpost)
 {
     try
     {
         using (var db = new SDES_DirectoryEntities())
         {
             var member = db.staffs.Find(id);
             db.Entry(member).State = EntityState.Deleted;
             db.SaveChanges();
             return RedirectToAction("Details", "office", new { id = member.officeId });
         }
     }
     catch
     {
         return View();
     }
 }
        public ActionResult Create(int officeId, hour day)
        {
            try
            {
                using(var db = new SDES_DirectoryEntities())
                {
                    db.hours.Add(day);
                    db.SaveChanges();
                }

                return RedirectToAction("Details", "office", new { id = officeId });
            }
            catch
            {
                return View(day);
            }
        }
 public ActionResult Delete(int id, hour day)
 {
     try
     {
         using (var db = new SDES_DirectoryEntities())
         {
             var hours = db.hours.Find(id);
             db.Entry(hours).State = EntityState.Deleted;
             db.SaveChanges();
             return RedirectToAction("Details", "office", new { id = hours.officeId });
         }
     }
     catch
     {
         return View(day);
     }
 }
        public ActionResult Create(staff member)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.staffs.Add(member);
                    db.SaveChanges();
                }

                return RedirectToAction("Details", "office", new { id = member.officeId });
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id, website site)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    var website = db.websites.Find(id);
                    db.Entry(website).State = EntityState.Deleted;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id, group deletion)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    var group = db.groups.Find(id);
                    db.Entry(group).State = EntityState.Deleted;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id, otherLocation location)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    var specific = db.otherLocations.Find(id);
                    db.Entry(specific).State = EntityState.Deleted;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(location);
            }
        }
        public ActionResult Create(otherLocation location)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.otherLocations.Add(location);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.officeId = Init.PopulateOfficesDropDownList();
                return View(location);
            }
        }
 //
 // GET: /admin/staff/
 public ActionResult Index()
 {
     var db = new SDES_DirectoryEntities();
     var members = db.staffs.OrderBy(x => x.staffOrder);
     return View(members.ToList());
 }
        public ActionResult Edit(int id, otherLocation location)
        {
            try
            {
                using (var db = new SDES_DirectoryEntities())
                {
                    db.Entry(location).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(location);
            }
        }
 //
 // GET: /admin/group/Edit/5
 public ActionResult Edit(int id)
 {
     var db = new SDES_DirectoryEntities();
     var group = db.groups.Find(id);
     return View(group);
 }
 //
 // GET: /admin/otherLocation/Delete/5
 public ActionResult Delete(int id)
 {
     var db = new SDES_DirectoryEntities();
     var location = db.otherLocations.Find(id);
     return View(location);
 }
 //
 // GET: /pages/Directory
 public ActionResult Directory()
 {
     var db = new SDES_DirectoryEntities();
     var offices = db.offices.OrderBy(x => x.officeName);
     return View(offices.ToList());
 }
 //
 // GET: /admin/staff/Edit/5
 public ActionResult Edit(int id)
 {
     var db = new SDES_DirectoryEntities();
     var member = db.staffs.Find(id);
     return View(member);
 }
 //
 // GET: /admin/office/Details/5
 public ActionResult Details(int id)
 {
     var db = new SDES_DirectoryEntities();
     var office = db.offices.Find(id);
     return View(office);
 }
 //
 // GET: /pages/Contact
 public ActionResult Contact()
 {
     var db = new SDES_DirectoryEntities();
     var sdesit = db.offices.Single(x => x.officeAcronym == "it");
     return View(sdesit);
 }
 public ActionResult Edit(int id, office office)
 {
     try
     {
         using (var db = new SDES_DirectoryEntities())
         {
             db.Entry(office).State = EntityState.Modified;
             db.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     catch
     {
         return View();
     }
 }
        //
        // GET: /feed/
        public ActionResult Index()
        {
            var db = new SDES_DirectoryEntities();
            var offices = db.offices.OrderBy(x => x.officeName).ToList();

            //init json list
            var json = new FeedViewModel();

            foreach (var office in offices)
            {
                //begin new view model object
                var department = new JsonOffice
                {
                    name = office.officeName,
                    acronym = office.officeAcronym,
                    phone = office.officePhone,
                    fax = office.officeFax,
                    email = office.officeEmail,
                    location =
                    {
                        building = office.officeLocationBuilding,
                        buildingNumber = office.officeLocationBuildingNum,
                        roomNumber = office.officeLocationRoomNum
                    },
                    postOfficeBox = office.officeBox,
                    offersPublicServices = office.offersPublicServices,
                    isDept = office.isDept,
                    functionalGroup = office.group.groupName
                };

                //assign websites
                foreach (var website in office.websites.OrderBy(x => x.websiteOrder))
                {
                    var site = new JsonWebsite
                    {
                        name = website.websiteName,
                        uri = website.websiteUri,
                        eventCalendar = website.websiteCal
                    };

                    //add to master list
                    department.websites.Add(site);
                }

                //assign hours
                foreach(var hours in office.hours.OrderBy(x => x.hoursDay))
                {
                    var day = new JsonHours
                    {
                        day = Init.ConvertDaytoText(hours.hoursDay),
                        open = hours.hoursOpen.ToString(),
                        close = hours.hoursClose.ToString()
                    };

                    //add to master list
                    department.hours.Add(day);
                }

                //assign staff
                foreach (var staff in office.staffs.OrderBy(x => x.staffOrder))
                {
                    var member = new JsonStaff
                    {
                        name = staff.staffName,
                        position = staff.staffTitle
                    };

                    //add to master list
                    department.staff.Add(member);
                }

                //assign social networks
                foreach (var social in office.socials)
                {
                    var network = new JsonSocial
                    {
                        name = social.socialName,
                        uri = social.socialUri,
                        uid = social.socialHandle
                    };

                    //add to master list
                    department.socialNetworks.Add(network);
                }

                //assign other locations
                foreach (var locations in office.otherLocations)
                {
                    var other = new JsonOtherLocations
                    {
                        name = locations.otherLocationName,
                        building = locations.otherLocationBuildingName,
                        buildingNumber = locations.otherLocationBuildingNum,
                        roomNumber = locations.otherLocationRoomNum,
                        email = locations.otherLocationEmail,
                        phone = locations.otherLocationPhone,
                        fax = locations.otherLocationFax
                    };

                    //add to master list
                    department.otherLocations.Add(other);
                }

                //add to master list
                json.departments.Add(department);
            }

            //return json
            return Json(json, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
        }
 //
 // GET: /admin/otherLocation/
 public ActionResult Index()
 {
     var db = new SDES_DirectoryEntities();
     var locations = db.otherLocations.OrderBy(x => x.office.officeName);
     return View(locations.ToList());
 }