// GET: CollectiveAgreements

        public ActionResult Index()
        {
            CollectiveAgreementIndexVM collectiveAgreementIndexVM = new CollectiveAgreementIndexVM();

            collectiveAgreementIndexVM.NumberOfCollectiveAgreements = 0;

            var collectiveAgreementHeaders = db.CollectiveAgreementHeaders.OrderBy(c => c.Id).ToList();

            collectiveAgreementIndexVM.NumberOfCollectiveAgreements = collectiveAgreementHeaders.Count();
            var collectiveAgreementInfos = db.CollectiveAgreementInfos.OrderBy(c => c.CollectiveAgreementHeaderId).ThenBy(c => c.StartDate).ToList();

            List <CollAgreementHeader> collAgreementHeaders = new List <CollAgreementHeader>();

            collectiveAgreementIndexVM.CollectiveAgreementHeader = collAgreementHeaders;

            foreach (var collectiveAgreementHeader in collectiveAgreementHeaders)
            {
                CollAgreementHeader collAgreementHeader = new CollAgreementHeader();
                collAgreementHeader.Name    = collectiveAgreementHeader.Name;
                collAgreementHeader.Counter = collectiveAgreementHeader.Counter;
                collAgreementHeader.Id      = collectiveAgreementHeader.Id;
                collectiveAgreementIndexVM.CollectiveAgreementHeader.Add(collAgreementHeader);
            }

            List <CollAgreementInfo> collAgreementInfos = new List <CollAgreementInfo>();

            collectiveAgreementIndexVM.CollectiveAgreementInfo = collAgreementInfos;

            foreach (var collectiveAgreementInfo in collectiveAgreementInfos)
            {
                CollAgreementInfo collAgreementInfo = new CollAgreementInfo();
                collAgreementInfo.Id        = collectiveAgreementInfo.Id;
                collAgreementInfo.StartDate = collectiveAgreementInfo.StartDate.ToShortDateString();
                collAgreementInfo.EndDate   = collectiveAgreementInfo.EndDate.ToShortDateString();
                collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfo.PerHourUnsocialEvening;
                collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementInfo.PerHourUnsocialNight;
                collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfo.PerHourUnsocialWeekend;
                collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfo.PerHourUnsocialHoliday;
                collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementInfo.PerHourOnCallWeekday;
                collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementInfo.PerHourOnCallWeekend;
                collectiveAgreementIndexVM.CollectiveAgreementInfo.Add(collAgreementInfo);
            }
            return(View(collectiveAgreementIndexVM));
        }
        // GET: CollectiveAgreements/Delete/5
        public ActionResult Delete(string type, int?headerId, int?infoId)
        {
            // It should not be possible to delete the last "Kollektivavtal"
            if (db.CollectiveAgreementHeaders.Count() < 2 && type == "header")
            {
                return(RedirectToAction("Index"));
            }

            if (headerId != null)
            {
                CollectiveAgreementDeleteVM collectiveAgreementDeleteVM = new CollectiveAgreementDeleteVM();
                collectiveAgreementDeleteVM.Type = type;

                CollAgreementHeader collAgreementHeader = new CollAgreementHeader();
                collectiveAgreementDeleteVM.CollAgreementHeader = collAgreementHeader;
                var collectiveAgreementHeader = db.CollectiveAgreementHeaders.Where(c => c.Id == headerId).FirstOrDefault();

                // It should not be possible to delete the last Time Period in a "Kollektivavtal"
                if (collectiveAgreementHeader.Counter < 2 && type == "info")
                {
                    return(RedirectToAction("Index"));
                }

                collectiveAgreementDeleteVM.CollAgreementHeader.Id      = collectiveAgreementHeader.Id;
                collectiveAgreementDeleteVM.CollAgreementHeader.Name    = collectiveAgreementHeader.Name;
                collectiveAgreementDeleteVM.CollAgreementHeader.Counter = collectiveAgreementHeader.Counter;

                if (type == "header")
                {
                    List <CollAgreementInfo> collAgreementInfos = new List <CollAgreementInfo>();
                    collectiveAgreementDeleteVM.CollAgreementInfo = collAgreementInfos;
                    var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreementHeader.Id).ToList();
                    foreach (var collectiveAgreementInfo in collectiveAgreementInfos)
                    {
                        CollAgreementInfo collAgreementInfo = new CollAgreementInfo();
                        collAgreementInfo.Id        = collectiveAgreementInfo.Id;
                        collAgreementInfo.StartDate = collectiveAgreementInfo.StartDate.ToShortDateString();
                        collAgreementInfo.EndDate   = collectiveAgreementInfo.EndDate.ToShortDateString();
                        collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfo.PerHourUnsocialEvening;
                        collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementInfo.PerHourUnsocialNight;
                        collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfo.PerHourUnsocialWeekend;
                        collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfo.PerHourUnsocialHoliday;
                        collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementInfo.PerHourOnCallWeekday;
                        collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementInfo.PerHourOnCallWeekend;
                        collectiveAgreementDeleteVM.CollAgreementInfo.Add(collAgreementInfo);
                    }
                }
                else if (type == "info" && infoId != null)
                {
                    List <CollAgreementInfo> collAgreementInfos = new List <CollAgreementInfo>();
                    collectiveAgreementDeleteVM.CollAgreementInfo = collAgreementInfos;
                    var collectiveAgreementInfos        = db.CollectiveAgreementInfos.Where(c => c.Id == infoId).FirstOrDefault();
                    CollAgreementInfo collAgreementInfo = new CollAgreementInfo();
                    collAgreementInfo.Id        = collectiveAgreementInfos.Id;
                    collAgreementInfo.StartDate = collectiveAgreementInfos.StartDate.ToShortDateString();
                    collAgreementInfo.EndDate   = collectiveAgreementInfos.EndDate.ToShortDateString();
                    collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfos.PerHourUnsocialEvening;
                    collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementInfos.PerHourUnsocialNight;
                    collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfos.PerHourUnsocialWeekend;
                    collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfos.PerHourUnsocialHoliday;
                    collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementInfos.PerHourOnCallWeekday;
                    collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementInfos.PerHourOnCallWeekend;
                    collectiveAgreementDeleteVM.CollAgreementInfo.Add(collAgreementInfo);
                }
                return(View(collectiveAgreementDeleteVM));
            }
            else
            {
                return(View());
            }
        }