// GET: CollectiveAgreements/Edit/5
        public ActionResult Edit(string type, int?headerId, int?infoId)
        {
            if (headerId != null)
            {
                CollectiveAgreementEditVM collectiveAgreementEditVM = new CollectiveAgreementEditVM();
                collectiveAgreementEditVM.Type = type;

                CollAgreementHeaderForVM collAgreementHeader = new CollAgreementHeaderForVM();
                collectiveAgreementEditVM.CollAgreementHeader = collAgreementHeader;
                var collectiveAgreementHeader = db.CollectiveAgreementHeaders.Where(c => c.Id == headerId).FirstOrDefault();
                collectiveAgreementEditVM.CollAgreementHeader.Id      = collectiveAgreementHeader.Id;
                collectiveAgreementEditVM.CollAgreementHeader.Name    = collectiveAgreementHeader.Name;
                collectiveAgreementEditVM.CollAgreementHeader.Counter = collectiveAgreementHeader.Counter;

                if (type == "header")
                {
                    List <CollAgreementInfoForVM> collAgreementInfos = new List <CollAgreementInfoForVM>();
                    collectiveAgreementEditVM.CollAgreementInfo = collAgreementInfos;
                    var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreementHeader.Id).ToList();
                    foreach (var collectiveAgreementInfo in collectiveAgreementInfos)
                    {
                        CollAgreementInfoForVM collAgreementInfo = new CollAgreementInfoForVM();
                        collAgreementInfo.Id        = collectiveAgreementInfo.Id;
                        collAgreementInfo.StartDate = collectiveAgreementInfo.StartDate;
                        collAgreementInfo.EndDate   = collectiveAgreementInfo.EndDate;
                        collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfo.PerHourUnsocialEvening;
                        collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementInfo.PerHourUnsocialNight;
                        collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfo.PerHourUnsocialWeekend;
                        collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfo.PerHourUnsocialHoliday;
                        collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementInfo.PerHourOnCallWeekday;
                        collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementInfo.PerHourOnCallWeekend;
                        collectiveAgreementEditVM.CollAgreementInfo.Add(collAgreementInfo);
                    }
                }
                else if (type == "info" && infoId != null)
                {
                    List <CollAgreementInfoForVM> collAgreementInfos = new List <CollAgreementInfoForVM>();
                    collectiveAgreementEditVM.CollAgreementInfo = collAgreementInfos;
                    var collectiveAgreementInfos             = db.CollectiveAgreementInfos.Where(c => c.Id == infoId).FirstOrDefault();
                    CollAgreementInfoForVM collAgreementInfo = new CollAgreementInfoForVM();
                    collAgreementInfo.Id        = collectiveAgreementInfos.Id;
                    collAgreementInfo.StartDate = collectiveAgreementInfos.StartDate;
                    collAgreementInfo.EndDate   = collectiveAgreementInfos.EndDate;
                    collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementInfos.PerHourUnsocialEvening;
                    collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementInfos.PerHourUnsocialNight;
                    collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementInfos.PerHourUnsocialWeekend;
                    collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementInfos.PerHourUnsocialHoliday;
                    collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementInfos.PerHourOnCallWeekday;
                    collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementInfos.PerHourOnCallWeekend;
                    collectiveAgreementEditVM.CollAgreementInfo.Add(collAgreementInfo);
                }
                return(View(collectiveAgreementEditVM));
            }
            else
            {
                return(View());
            }
        }
        public ActionResult Edit(CollectiveAgreementEditVM collectiveAgreementEditVM, string submitButton)
        {
            //Check if a collective agreement with the same name already exists
            if (db.CollectiveAgreementHeaders.Where(c => c.Name == collectiveAgreementEditVM.CollAgreementHeader.Name).Where(c => c.Id != collectiveAgreementEditVM.CollAgreementHeader.Id).FirstOrDefault() != null)
            {
                ModelState.AddModelError("CollagreementHeader.Name", "Det finns redan ett avtal med samma namn.");
            }
            //Check that the last date of the agreement is equal to or greater than the first date
            for (int i = 0; i < collectiveAgreementEditVM.CollAgreementInfo.Count(); i++)
            {
                if (collectiveAgreementEditVM.CollAgreementInfo[i].StartDate.Date > collectiveAgreementEditVM.CollAgreementInfo[i].EndDate.Date)
                {
                    ModelState.AddModelError("CollAgreementInfo[" + i.ToString() + "].EndDate", "Kollektivavtalets slutdatum får inte vara tidigare än avtalets startdatum.");
                }
            }
            //Check if there is an overlap between time periods for the same collective agreement for the case where several time periods may have been edited
            if (collectiveAgreementEditVM.CollAgreementInfo.Count() > 1)
            {
                //Check start date for overlaps
                bool noOverlapFound = true;
                int  idx            = 1;
                int  startIdx       = 0;
                int  stopIdx        = collectiveAgreementEditVM.CollAgreementInfo.Count();
                do
                {
                    do
                    {
                        if (collectiveAgreementEditVM.CollAgreementInfo[startIdx].StartDate >= collectiveAgreementEditVM.CollAgreementInfo[idx].StartDate && collectiveAgreementEditVM.CollAgreementInfo[startIdx].StartDate <= collectiveAgreementEditVM.CollAgreementInfo[idx].EndDate)
                        {
                            ModelState.AddModelError("CollAgreementInfo[" + startIdx.ToString() + "].StartDate", "Datumet överlappar med en existerande period.");
                            noOverlapFound = false;
                        }
                        idx++;
                    } while (noOverlapFound && idx < stopIdx);
                    startIdx++;
                    idx = startIdx + 1;
                } while (noOverlapFound && idx < stopIdx);

                //Check end date for overlaps
                noOverlapFound = true;
                idx            = 1;
                startIdx       = 0;
                do
                {
                    do
                    {
                        if (collectiveAgreementEditVM.CollAgreementInfo[startIdx].EndDate >= collectiveAgreementEditVM.CollAgreementInfo[idx].StartDate && collectiveAgreementEditVM.CollAgreementInfo[startIdx].EndDate <= collectiveAgreementEditVM.CollAgreementInfo[idx].EndDate)
                        {
                            ModelState.AddModelError("CollAgreementInfo[" + startIdx.ToString() + "].EndDate", "Datumet överlappar med en existerande period.");
                            noOverlapFound = false;
                        }
                        idx++;
                    } while (noOverlapFound && idx < stopIdx);
                    startIdx++;
                    idx = startIdx + 1;
                } while (noOverlapFound && idx < stopIdx);
            }
            else if (collectiveAgreementEditVM.CollAgreementInfo.Count() == 1 && collectiveAgreementEditVM.CollAgreementHeader.Counter > 1)
            {
                int collAgreementInfoId      = collectiveAgreementEditVM.CollAgreementInfo[0].Id;
                var collectiveAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == collectiveAgreementEditVM.CollAgreementHeader.Id).Where(c => c.Id != collAgreementInfoId).ToList();
                //Check start date for overlaps for the case where only one time period has been edited
                bool noOverlapFound = true;
                int  idx            = 0;
                int  stopIdx        = collectiveAgreementInfos.Count();
                do
                {
                    if (collectiveAgreementEditVM.CollAgreementInfo[0].StartDate >= collectiveAgreementInfos[idx].StartDate && collectiveAgreementEditVM.CollAgreementInfo[0].StartDate <= collectiveAgreementInfos[idx].EndDate)
                    {
                        ModelState.AddModelError("CollAgreementInfo[0].StartDate", "Datumet överlappar med en existerande period.");
                        noOverlapFound = false;
                    }
                    idx++;
                } while (noOverlapFound && idx < stopIdx);

                noOverlapFound = true;
                idx            = 0;
                do
                {
                    if (collectiveAgreementEditVM.CollAgreementInfo[0].EndDate >= collectiveAgreementInfos[idx].StartDate && collectiveAgreementEditVM.CollAgreementInfo[0].EndDate <= collectiveAgreementInfos[idx].EndDate)
                    {
                        ModelState.AddModelError("CollAgreementInfo[0].EndDate", "Datumet överlappar med en existerande period.");
                        noOverlapFound = false;
                    }
                    idx++;
                } while (noOverlapFound && idx < stopIdx);
            }

            if (ModelState.IsValid)
            {
                if (submitButton == "Spara")
                {
                    if (collectiveAgreementEditVM.Type == "header")
                    {
                        var headerId = collectiveAgreementEditVM.CollAgreementHeader.Id;
                        var header   = db.CollectiveAgreementHeaders.Where(c => c.Id == headerId).FirstOrDefault();

                        header.Name            = collectiveAgreementEditVM.CollAgreementHeader.Name;
                        db.Entry(header).State = EntityState.Modified;

                        //Remove the existing collective agreement info records
                        var collAgreementInfos = db.CollectiveAgreementInfos.Where(c => c.CollectiveAgreementHeaderId == headerId).ToList();
                        db.CollectiveAgreementInfos.RemoveRange(collAgreementInfos);

                        List <CollectiveAgreementInfo> updatedCollAgreementInfos = new List <CollectiveAgreementInfo>();

                        //Add updated collective agreement info records
                        for (int i = 0; i < collectiveAgreementEditVM.CollAgreementInfo.Count(); i++)
                        {
                            CollectiveAgreementInfo collectiveAgreementInfo = new CollectiveAgreementInfo();
                            collectiveAgreementInfo.CollectiveAgreementHeaderId = headerId;
                            collectiveAgreementInfo.StartDate = Convert.ToDateTime(collectiveAgreementEditVM.CollAgreementInfo[i].StartDate).Date;
                            collectiveAgreementInfo.EndDate   = Convert.ToDateTime(collectiveAgreementEditVM.CollAgreementInfo[i].EndDate).Date;
                            collectiveAgreementInfo.PerHourUnsocialEvening = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourUnsocialEvening;
                            collectiveAgreementInfo.PerHourUnsocialNight   = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourUnsocialNight;
                            collectiveAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourUnsocialWeekend;
                            collectiveAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourUnsocialHoliday;
                            collectiveAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourOnCallWeekday;
                            collectiveAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementEditVM.CollAgreementInfo[i].PerHourOnCallWeekend;
                            updatedCollAgreementInfos.Add(collectiveAgreementInfo);
                        }
                        db.CollectiveAgreementInfos.AddRange(updatedCollAgreementInfos);
                    }
                    else if (collectiveAgreementEditVM.Type == "info")
                    {
                        var headerId = collectiveAgreementEditVM.CollAgreementHeader.Id;
                        var header   = db.CollectiveAgreementHeaders.Where(c => c.Id == headerId).FirstOrDefault();

                        header.Name            = collectiveAgreementEditVM.CollAgreementHeader.Name;
                        db.Entry(header).State = EntityState.Modified;

                        int infoId            = collectiveAgreementEditVM.CollAgreementInfo[0].Id;
                        var collAgreementInfo = db.CollectiveAgreementInfos.Where(c => c.Id == infoId).FirstOrDefault();
                        db.Entry(collAgreementInfo).State        = EntityState.Modified;
                        collAgreementInfo.StartDate              = Convert.ToDateTime(collectiveAgreementEditVM.CollAgreementInfo[0].StartDate).Date;
                        collAgreementInfo.EndDate                = Convert.ToDateTime(collectiveAgreementEditVM.CollAgreementInfo[0].EndDate).Date;
                        collAgreementInfo.PerHourUnsocialEvening = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourUnsocialEvening;
                        collAgreementInfo.PerHourUnsocialNight   = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourUnsocialNight;
                        collAgreementInfo.PerHourUnsocialWeekend = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourUnsocialWeekend;
                        collAgreementInfo.PerHourUnsocialHoliday = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourUnsocialHoliday;
                        collAgreementInfo.PerHourOnCallWeekday   = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourOnCallWeekday;
                        collAgreementInfo.PerHourOnCallWeekend   = collectiveAgreementEditVM.CollAgreementInfo[0].PerHourOnCallWeekend;
                    }
                    db.SaveChanges();
                }
            }
            else
            {
                return(View(collectiveAgreementEditVM));
            }
            return(RedirectToAction("Index"));
        }