public ActionResult AddRoom(FormCollection f)
        {
            Room room = new Room();
            room.RoomName = f["RoomName"];
            room.LodgeID = int.Parse(f["lodgeId"]);
            room.TotalSlots = int.Parse(f["TotalSlots"]);
            room.AvailableSlots = room.TotalSlots;

            Lodge lodge = db.Lodges.SingleOrDefault(r => r.LodgeID == room.LodgeID);
            lodge.TotalSlots += room.TotalSlots;
            lodge.AvailableSlots += room.TotalSlots;
            lodge.TotalSlotsInUsed += room.TotalSlots;
            //lodge.AvailableSlots += room.AvailableSlots;
            //ChairitiesExam ce = lodge.ChairitiesExam;
            //ce.TotalSlotsLodges += room.TotalSlots;
            //ce.AvailableSlotsLodges += room.AvailableSlots;

            db.Rooms.Add(room);
            db.SaveChanges();

            return RedirectToAction("DetailsLodge", new { id = room.LodgeID });
        }
        public void ChooseRoomsForCe(int eId, string roomId, int lodgeId, int cId)
        {
            string[] listStringId = roomId.Split('-');

            int[] listIntId = new int[listStringId.Length - 1];

            for (int i = 0; i < listStringId.Length - 1; i++)
            {
                listIntId[i] = int.Parse(listStringId[i]);
            }
            Lodge lodgeNew = new Lodge();
            Lodge lodge = db.Lodges.SingleOrDefault(r => r.LodgeID == lodgeId);
            lodgeNew = lodge;
            ChairitiesExam cExam = new ChairitiesExam();
            cExam = db.ChairitiesExams.FirstOrDefault(c => c.CharityExamID == eId && c.CharityID == cId);
            lodgeNew.CharityExamID = cExam.CharityExamID;
            lodgeNew.CharityID = cId;
            lodgeNew.TotalSlotsInUsed = 0;
            lodgeNew.AvailableSlots = 0;
            db.Lodges.Add(lodgeNew);
            db.SaveChanges();
            Lodge lodgeNewest = db.Lodges.FirstOrDefault(l => l.Address == lodgeNew.Address && l.CharityExamID == cExam.CharityExamID);
            foreach (var i in listIntId)
            {
                Room roomNew = new Room();
                Room room = db.Rooms.SingleOrDefault(r => r.RoomID == i);
                roomNew = room;
                roomNew.LodgeID = lodgeNewest.LodgeID;
                roomNew.CharityExamID = cExam.CharityExamID;

                cExam.TotalSlotsLodges += room.TotalSlots;
                cExam.AvailableSlotsLodges += room.AvailableSlots;
                lodgeNewest.TotalSlotsInUsed += room.TotalSlots;
                lodgeNewest.AvailableSlots += room.AvailableSlots;
                db.Rooms.Add(roomNew);
                db.SaveChanges();
            }
        }
        protected bool rg2r(List<ExaminationsPaper> eps, int lodgeID, bool gender)
        {
            Room r = new Room();
            var rooms = getRooms(lodgeID, gender); // get list of rooms suitable (gender, available slots, ...)
            foreach (int rID in rooms)
            {
                r = db.Rooms.SingleOrDefault(re => re.RoomID == rID);
                if (r.AvailableSlots > 0) // possible to use
                {
                    // split into 02 groups:
                    // group 1: number of members equal to room's available slots.
                    List<ExaminationsPaper> eps01 = new List<ExaminationsPaper>();
                    for (int i = 0; i < r.AvailableSlots; ++i) eps01.Add(eps[i]);
                    // group 2: the rest.
                    List<ExaminationsPaper> eps02 = new List<ExaminationsPaper>();
                    for (int i = r.AvailableSlots; i < eps.Count; ++i) eps02.Add(eps[i]);

                    if (!g2r(eps01, lodgeID, gender))  // cannot fit this small group
                    {
                        // try split the group more
                        if (!rg2r(eps01, lodgeID, gender)) return false;
                    }

                    if (!g2r(eps02, lodgeID, gender)) // cannot fit this small group
                    {
                        // try split the group more
                        if (!rg2r(eps02, lodgeID, gender)) return false;
                    }

                    return true;
                }
            }
            return false;
        }
 protected bool g2r(List<ExaminationsPaper> eps, int lodgeID, bool gender)
 {
     Room r = new Room();
     var rooms = getRooms(lodgeID, gender); // get list of rooms suitable (gender, available slots, ...)
     foreach (int rID in rooms)
     {
         r = db.Rooms.SingleOrDefault(re => re.RoomID == rID);
         if (r.AvailableSlots >= eps.Count) // fit it all
         {
             foreach (ExaminationsPaper ep in eps) ep.RoomID = rID;
             r.Gender = gender;
             r.AvailableSlots -= eps.Count;
             return true;
         }
     }
     return false;
 }
 protected List<int> sortRooms(List<int> rooms, bool gender) // lodgeID
 {
     Room r1 = new Room();
     Room r2 = new Room();
     int tmp;
     for (int i = 0; i < rooms.Count - 1; ++i)
     {
         tmp = rooms[i];
         r1 = db.Rooms.SingleOrDefault(r => r.RoomID == tmp);
         for (int j = i + 1; j < rooms.Count; ++j)
         {
             tmp = rooms[j];
             r2 = db.Rooms.SingleOrDefault(r => r.RoomID == tmp);
             if (r1.Gender == gender && r2.Gender == gender) // both are same gender
             {
                 // order by available slots
                 if (r1.AvailableSlots < r2.AvailableSlots) swapRoom(rooms, i, j);
             }
             else if (r1.Gender != gender && r2.Gender != gender) // no rooms matched gender 
             {
                 // order by available slots
                 if (r1.AvailableSlots < r2.AvailableSlots) swapRoom(rooms, i, j);
             }
             else // only one room matched gender
             {
                 // r1 does not matched gender --> put it behind
                 if (r1.Gender != gender) swapRoom(rooms, i, j);
             }
         }
     }
     return rooms;
 }
        public void EditLodge(int ceId, string roomId, int lodgeId, int cId)
        {
            string[] listStringId = roomId.Split('-');

            int[] listIntId = new int[listStringId.Length - 1];

            for (int i = 0; i < listStringId.Length - 1; i++)
            {
                listIntId[i] = int.Parse(listStringId[i]);
            }
            Lodge lodgeNew = new Lodge();
            Lodge lodge = db.Lodges.SingleOrDefault(r => r.LodgeID == lodgeId);
            lodgeNew = lodge;
            lodgeNew.AvailableSlots = 0;
            lodgeNew.TotalSlotsInUsed = 0;
            //List<Room> listRoom = new List<Room>();
            //listRoom = lodge.Rooms.ToList();
            //foreach (var room in listRoom)
            //{
            //    room.CharityExamID = null;
            //    db.SaveChanges();
            //}
            lodgeNew.CharityExamID = ceId;
            lodgeNew.CharityID = cId;
            db.Lodges.Add(lodgeNew);
            db.SaveChanges();
            Lodge lodgeNewest = db.Lodges.FirstOrDefault(l => l.Address == lodgeNew.Address && l.CharityExamID == lodgeNew.CharityExamID);
            foreach (var i in listIntId)
            {
                Room roomNew = new Room();
                Room room = db.Rooms.SingleOrDefault(r => r.RoomID == i);

                roomNew = room;
                roomNew.LodgeID = lodgeNewest.LodgeID;
                roomNew.CharityExamID = lodgeNewest.CharityExamID;

                lodgeNewest.TotalSlotsInUsed += room.TotalSlots;
                lodgeNewest.AvailableSlots += room.TotalSlots;
                db.Rooms.Add(roomNew);
                db.SaveChanges();
            }
        }