public ActionResult AddCar(FormCollection f)
        {
            Account acc = (Account)Session["acc"];
            Sponsor sp = db.Sponsors.SingleOrDefault(r => r.AccountID == acc.AccountID);

            Car car = new Car();
            string[] listStringNumber = f["NumberPlate"].Split(' ');
            string numberplate = "";
            for (int i = 0; i < listStringNumber.Length; i++)
            {
                if (!listStringNumber[i].Equals(" "))
                {
                    numberplate += listStringNumber[i];
                }

            }
            car.SponsorID = sp.SponsorID;
            car.IsApproved = false;
            car.NumberPlate = numberplate.ToUpper();
            car.TotalSlots = int.Parse(f["TotalSlots"]);
            car.AvailableSlots = car.TotalSlots;
            car.DriverName = f["DriverName"];
            car.DriverPhone = f["DriverPhone"];
            db.Cars.Add(car);
            db.SaveChanges();

            return RedirectToAction("ManageCar");
        }
        public ActionResult AssignToCarForStation(int id)
        {
            var DelCan = db.ExaminationsPapers.Where(r => r.CharityExamID == id).ToList();
            foreach (var c in DelCan)
            {
                c.StationCarID = null;
            }
            db.SaveChanges();
            var CountStation = db.Stations.Count();
            var cars = db.Cars.Where(r => r.CharityExamID == id).ToList();
            Car tmp = new Car();
            for (int i = 0; i < cars.Count; ++i)
                for (int j = i + 1; j < cars.Count; ++j)
                    if (cars[i].TotalSlots > cars[j].TotalSlots)
                    {
                        tmp = cars[i];
                        cars[i] = cars[j];
                        cars[j] = tmp;
                    }
            for (int j = 1; j <= CountStation; ++j)
            {
                var Candidates = db.ExaminationsPapers.Where(r => r.CharityExamID == id && r.StatitonID == j).ToList();
                var CandidatesCount = db.ExaminationsPapers.Count(r => r.CharityExamID == id && r.StatitonID == j);
                foreach (var i in cars)
                {
                    if (i.TotalSlots >= CandidatesCount)
                    {
                        StationCar sc = new StationCar();
                        sc.CarID = i.CarID;
                        db.StationCars.Add(sc);
                        db.SaveChanges();
                        foreach (var e in Candidates)
                        {
                            e.StationCarID = sc.StationCarIID;
                        }
                        cars.Remove(i);
                        db.SaveChanges();
                        break;
                    }

                }
            }
            return RedirectToAction("AssignCarForStation", new { id = id });
        }
        public ActionResult AddCar(FormCollection f)
        {
            Car car = new Car();
            car.CharityID = int.Parse(f["id"]);
            car.NumberPlate = f["NumberPlate"];
            car.TotalSlots = int.Parse(f["TotalSlots"]);
            car.AvailableSlots = car.TotalSlots;
            car.DriverName = f["DriverName"];
            car.DriverPhone = f["DriverPhone"];

            if (int.Parse(f["CharityExamID"]) != 0)
            {
                car.CharityExamID = int.Parse(f["CharityExamID"]);
            }
            car.IsApproved = true;
            db.Cars.Add(car);

            if (car.CharityExamID != null)
            {
                ChairitiesExam ce = db.ChairitiesExams.SingleOrDefault(r => r.CharityExamID == car.CharityExamID);
                ce.TotalSlotsVehicles += car.TotalSlots;
                ce.AvailableSlotsVehicles += car.AvailableSlots;
                db.SaveChanges();
                //CreateScheduleCar(car.CarID);
            }
            else
            {
                db.SaveChanges();
            }



            return RedirectToAction("ManageCarCharity");
        }
 protected void AssignTheCar(List<ExaminationsPaper> eps, Car car)
 {
     ExaminationsPaper ep = new ExaminationsPaper();
     int quantity = eps.Count < car.AvailableSlots ? eps.Count : car.AvailableSlots;
     for (int i = 0; i < quantity; ++i)
     {
         ep = eps.FirstOrDefault(r => r.CarID == null);
         ep.CarID = car.CarID;
         --car.AvailableSlots;
     }
     db.SaveChanges();
 }
        protected bool Capable(Car c, int lodgeId)
        {
            // neu chua co thi sinh
            if (c.ExaminationsPapers.FirstOrDefault() == null) return true;

            // neu da co thi sinh nhung ko thuoc lodge nay
            if (c.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID != lodgeId) return false;

            List<int> venueIDs = c.ExaminationsPapers.Select(r => r.VenueID).Distinct().ToList();
            if (venueIDs.Count < 3) return true;
            return false;
        }
        protected Car FindMeTheBestCar(List<Car> cars, int quantity, int lodgeId)
        {
            // Sort cars by instructions:
            // Seperate it into 02 list:
            // The first one is the list of cars which are belong to the lodge.
            // The second is the list of others. Free cars (not belong to any lodge) included.
            // Both list are sorted in the order of increasing available slots.
            Car c1 = new Car();
            Car c2 = new Car();
            for (int i = 0; i < cars.Count - 1; ++i)
            {
                c1 = cars[i];
                for (int j = i + 1; j < cars.Count; ++j)
                {
                    c2 = cars[i];
                    // ko co xe nao da co thi sinh
                    if (c1.ExaminationsPapers.FirstOrDefault() == null && c2.ExaminationsPapers.FirstOrDefault() == null)
                    {
                        if (c1.AvailableSlots < c2.AvailableSlots) swapCar(cars, i, j);
                    }
                    // 1 trong 2 da co thi sinh
                    else if (c1.ExaminationsPapers.FirstOrDefault() != null || c2.ExaminationsPapers.FirstOrDefault() != null)
                    {
                        if (c1.AvailableSlots < c2.AvailableSlots) swapCar(cars, i, j);
                    }
                    // ca 2 xe da co thi sinh
                    else
                    {
                        // ca 2 xe deu cung lodge
                        if (c1.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID == lodgeId && c2.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID == lodgeId)
                        {
                            if (c1.AvailableSlots < c2.AvailableSlots) swapCar(cars, i, j);
                        }
                        // 1 trong 2 cung lodge
                        else if (c1.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID != lodgeId || c2.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID != lodgeId)
                        {
                            if (c2.ExaminationsPapers.FirstOrDefault().LodgeRegisteredID == lodgeId) swapCar(cars, i, j);
                        }
                        // ca 2 xe deu ko cung lodge
                        else if (c1.AvailableSlots < c2.AvailableSlots) swapCar(cars, i, j);
                    }
                }
            }

            Car res = cars.FirstOrDefault(r => r.AvailableSlots >= quantity && Capable(r, lodgeId));
            if (res == null) res = cars.LastOrDefault(r => r.AvailableSlots > 0 && Capable(r, lodgeId));
            return res;
        }
        public ActionResult AssignToCar(int id) // ceID
        {
            ChairitiesExam ce = db.ChairitiesExams.SingleOrDefault(r => r.CharityExamID == id);
            List<Car> cars = ce.Cars.ToList();
            List<DataVenueAssignCar> venuesAssignCar = new List<DataVenueAssignCar>();

            List<int> veIDs = new List<int>();
            DataVenueAssignCar venueAC = new DataVenueAssignCar();
            foreach (Lodge lodge in ce.Lodges)
            {
                // list of venues in this lodge.
                veIDs = lodge.ExaminationsPapers.Select(r => r.VenueID).Distinct().ToList();
                foreach (int veID in veIDs)
                    venuesAssignCar.Add(new DataVenueAssignCar(veID, lodge.LodgeID, db.Venues.SingleOrDefault(r => r.VenueID == veID).ExaminationsPapers.Count));
            }

            int i = 0;
            Car bestCar = new Car();
            List<ExaminationsPaper> eps = new List<ExaminationsPaper>();
            while (i < venuesAssignCar.Count)
            {
                // sort by number of candidates take exam at each venue.
                sortVenues(venuesAssignCar);
                venueAC = venuesAssignCar[i];

                // candidates of this venue.
                eps = db.ExaminationsPapers.Where(r => r.LodgeRegisteredID == venueAC.lodgeID && r.VenueID == venueAC.venueID && r.CarID == null && r.ParticipantVolunteerID == null).ToList();

                // at least 04 candidates per venue to be assigned to a car!
                bestCar = eps.Count < 4 ? null : FindMeTheBestCar(cars, eps.Count, venueAC.lodgeID);
                if (bestCar != null)
                {
                    if (bestCar.AvailableSlots < eps.Count)
                        venuesAssignCar.Add(new DataVenueAssignCar(venueAC.venueID, venueAC.lodgeID, eps.Count - bestCar.AvailableSlots)); // split it into two pieces.
                    AssignTheCar(eps, bestCar);
                }
                else // Volunteers' area
                {
                    if (!AssignTheVolunteer(eps, ce.ParticipantVolunteers))
                    {
                        db.SaveChanges();
                        return RedirectToAction("AssignCar", new { id = id });
                    }
                }
                venuesAssignCar.RemoveAt(0);
            }
            return RedirectToAction("AssignCar", new { id = id });
        }
        public void AddNewCar(CharityCarModel carModel)
        {
            Car = new Car();
            Car.NumberPlate = carModel.NumberPlate;
            Car.TotalSlots = carModel.TotalSlot;
            Car.AvailableSlots = carModel.TotalSlot;
            Car.CharityExamID = carModel.CharityExamId;
            Car.DriverName = carModel.DriverName;
            Car.DriverPhone = carModel.DriverPhone;

            db.Cars.Add(Car);
            db.SaveChanges();
        }
 public void GetCarInfo(int carId)
 {
     Car = new Car();
     Car = db.Cars.Find(carId);
 }
        public void EditCar(Car car, int carId)
        {
            var oldCar = db.Cars.Find(carId);
            var slot = car.TotalSlots - oldCar.TotalSlots;
            oldCar.NumberPlate = car.NumberPlate;
            oldCar.TotalSlots = car.TotalSlots;
            oldCar.DriverName = car.DriverName;
            oldCar.DriverPhone = car.DriverPhone;
            oldCar.AvailableSlots = oldCar.AvailableSlots + slot;

            db.SaveChanges();
        }
 public ActionResult EditCar(FormCollection f)
 {
     Car carNew = new Car();
     int carId = int.Parse(f["carId"]);
     Car car = db.Cars.SingleOrDefault(r => r.CarID == carId);
     carNew = car;
     carNew.CharityExamID = int.Parse(f["asd"]);
     carNew.CharityID = int.Parse(f["charityid"]);
     db.Cars.Add(carNew);
     db.SaveChanges();
     return RedirectToAction("ManageCar");
 }