//[NonAction]
        public ActionResult ShowTimes(string dateRes, int courtId)
        {
            CultureInfo culture = new CultureInfo("ro-RO");
            DateTime date = Convert.ToDateTime(dateRes,culture);
            CourtReservation reservation = new CourtReservation();

            if (Request.IsAjaxRequest())
            {
                var result = db.Reservations
                    .Where(r=>(r.CourtID== courtId) &&
                    (r.ReservationDate.Year==date.Year) && 
                    (r.ReservationDate.Month == date.Month) && 
                    (r.ReservationDate.Day == date.Day))
                    .Select(r=>r.ReservationTime.Hours).ToList();
                
                reservation.NZeceDoispe = result.Contains("10-12");
                reservation.NDoispePaispe = result.Contains("12-14");
                reservation.NPaispeSaispe = result.Contains("14-16");
                reservation.NSaispeOptspe = result.Contains("16-18");
                reservation.NOptspeDouazeci = result.Contains("18-20");
                reservation.NDouazeciDouajdoi = result.Contains("20-22");
            }

            return PartialView("CourtReservationTime",reservation);
        }
        public ActionResult Rent(Court court, CourtReservation reservation, string selectedDate)
        {
            Reservation res = new Reservation();

            //court
            if (court.IDCourt > 0)
            {
                res.Court = db.Courts.Find(court.IDCourt);
                res.CourtID = court.IDCourt;
            }
            else
            {
                ModelState.AddModelError("", "Teren invalid!");
                return View(res.Court);
            }

            //date
            if (selectedDate != null && selectedDate!="")
            {
                CultureInfo culture = new CultureInfo("ro-RO");
                DateTime reservationDate = Convert.ToDateTime(selectedDate,culture);
                if ((reservationDate.Year >= DateTime.Now.Year &&
                    reservationDate.Month >= DateTime.Now.Month &&
                    reservationDate.Day >= DateTime.Now.Day))
                {
                    res.ReservationDate = reservationDate;
                }
                else
                {
                    ModelState.AddModelError("", "Data invalida! Va rugam sa selectati o data din viitor!");
                    return View("Details", res.Court);
                }
            }
            else
            {
                ModelState.AddModelError("", "Va trebui sa selectati o data!");
                return View("Details", res.Court);
            }

            bool forToday = (res.ReservationDate.Year == DateTime.Now.Year) &&
                            (res.ReservationDate.Month == DateTime.Now.Month) &&
                            (res.ReservationDate.Day == DateTime.Now.Day);
            
            

            //user
            res.User = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
            if (res.User == null)
            {
                ModelState.AddModelError("", "Utilizator necunoscut!");
                return View(res.Court);
            }
            res.UserID = res.User.IDUser;
            res.User.UserGroup = db.UserGroups.Where(u => u.IDUserGroup == res.User.UserGroupID).FirstOrDefault();
            res.User.UserPasswordConfirm = res.User.UserPassword;

            //Reservation hours
            List<Reservation> reservations = new List<Reservation>();
            if (reservation.NZeceDoispe)
            {
                if ((forToday && DateTime.Now.Hour < 9) || !forToday)
                {
                    Reservation res1012 = Utilities.Clone(res);
                    res1012.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "10-12").First();
                    res1012.ReservationTimeID = res1012.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res1012);
                    db.Entry(res1012.Court).State = EntityState.Detached;
                    db.Entry(res1012.User).State = EntityState.Detached;
                    reservations.Add(res1012);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            if (reservation.NDoispePaispe)
            {
                if ((forToday && DateTime.Now.Hour < 11) || !forToday)
                {
                    Reservation res1214 = Utilities.Clone(res);
                    res1214.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "12-14").First();
                    res1214.ReservationTimeID = res1214.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res1214);
                    db.Entry(res1214.Court).State = EntityState.Detached;
                    db.Entry(res1214.User).State = EntityState.Detached;
                    reservations.Add(res1214);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            if (reservation.NPaispeSaispe)
            {
                if ((forToday && DateTime.Now.Hour < 13) || !forToday)
                {
                    Reservation res1416 = Utilities.Clone(res);
                    res1416.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "14-16").First();
                    res1416.ReservationTimeID = res1416.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res1416);
                    db.Entry(res1416.Court).State = EntityState.Detached;
                    db.Entry(res1416.User).State = EntityState.Detached;
                    reservations.Add(res1416);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            if (reservation.NSaispeOptspe)
            {
                if ((forToday && DateTime.Now.Hour < 15) || !forToday)
                {
                    Reservation res1618 = Utilities.Clone(res);
                    res1618.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "16-18").First();
                    res1618.ReservationTimeID = res1618.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res1618);
                    db.Entry(res1618.Court).State = EntityState.Detached;
                    db.Entry(res1618.User).State = EntityState.Detached;
                    reservations.Add(res1618);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            if (reservation.NOptspeDouazeci)
            {
                if ((forToday && DateTime.Now.Hour < 17) || !forToday)
                {
                    Reservation res1820 = Utilities.Clone(res);
                    res1820.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "18-20").First();
                    res1820.ReservationTimeID = res1820.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res1820);
                    db.Entry(res1820.Court).State = EntityState.Detached;
                    db.Entry(res1820.User).State = EntityState.Detached;
                    reservations.Add(res1820);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            if (reservation.NDouazeciDouajdoi)
            {
                if ((forToday && DateTime.Now.Hour < 19) || !forToday)
                {
                    Reservation res2022 = Utilities.Clone(res);
                    res2022.ReservationTime = db.ReservationTimes.Where(r => r.Hours == "20-22").First();
                    res2022.ReservationTimeID = res2022.ReservationTime.IDReservationTime;
                    db.Reservations.Add(res2022);
                    db.Entry(res2022.Court).State = EntityState.Detached;
                    db.Entry(res2022.User).State = EntityState.Detached;
                    reservations.Add(res2022);
                }
                else
                {
                    ModelState.AddModelError("", "Terenul trebuie rezervat cu minim o ora inainte de prezentare!");
                    return View("Details", res.Court);
                }
            }

            try
            {
                db.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException er)
            {
                foreach (var validationErrors in er.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        ModelState.AddModelError(validationError.PropertyName, validationError.ErrorMessage);

                    }
                }
                return View("Details", res.Court);
            }
            if (reservations.Count > 0)
            {
                if (res.Court.EmailAddress != null)
                {
                    StringBuilder body = new StringBuilder();
                    StringBuilder sms = new StringBuilder();

                    body.Append("Buna ziua,\n");
                    if (reservations.Count == 1)
                    {
                        body.Append("O noua rezervare a fost creata pe terenul " + res.Court.CourtName);
                        sms.Append("Rezervare noua ");
                    }
                    else
                    {
                        body.Append("Noi rezervari au fost create pe terenul " + res.Court.CourtName);
                        sms.Append("Rezervari noi ");
                    }

                    body.Append(" de catre "+res.User.UserEmail + ". \n");
                    body.Append("Rezervarea este in data de " + res.ReservationDate.ToString("dd.MM.yyyy") + " la orele: \n");
                    sms.Append("pe terenul " + res.Court.CourtName + " in data de " + res.ReservationDate.ToString("dd.MM.yyyy") + " la orele: ");

                    foreach (var item in reservations)
                    {
                        body.Append(item.ReservationTime.Hours + "\n");
                        sms.Append(item.ReservationTime.Hours + " ");
                    }
                    sms.Append(". Contact:" + res.User.UserEmail);
                    body.Append("O zi placuta");
                    Utilities.EmailSend(res.Court.EmailAddress, "Rezervare noua pe terenul " + res.Court.CourtName, body.ToString());
                    Utilities.SmsSend(res.Court.PhoneNumber, sms.ToString());
                    
                }
                return View("ReservationConfirmation", reservations);
            }
            else
            {
                ModelState.AddModelError("", "Nicio ora nu a fost selectata din lista de ore pentru rezervare!");
                return View("Details", res.Court);
            }
        }