public ActionResult Edit(TourImage tourImage, HttpPostedFileBase fileUpload)
        {
            if (ModelState.IsValid)
            {
                #region Upload and resize image if needed
                string newFilenameUrl = tourImage.ImageUrl;
                if (fileUpload != null)
                {
                    string filename    = Path.GetFileName(fileUpload.FileName);
                    string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty)
                                         + Path.GetExtension(filename);

                    newFilenameUrl = "/Uploads/Tour/" + newFilename;
                    string physicalFilename = Server.MapPath(newFilenameUrl);

                    fileUpload.SaveAs(physicalFilename);

                    tourImage.ImageUrl = newFilenameUrl;
                }
                #endregion
                tourImage.IsDelete        = false;
                db.Entry(tourImage).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index", new { id = tourImage.TourId }));
            }
            return(View(tourImage));
        }
        public ActionResult Create(TourImage tourImage, Guid id, HttpPostedFileBase fileUpload)
        {
            if (ModelState.IsValid)
            {
                #region Upload and resize image if needed
                string newFilenameUrl = string.Empty;
                if (fileUpload != null)
                {
                    string filename    = Path.GetFileName(fileUpload.FileName);
                    string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty)
                                         + Path.GetExtension(filename);

                    newFilenameUrl = "/Uploads/Tour/" + newFilename;
                    string physicalFilename = Server.MapPath(newFilenameUrl);

                    fileUpload.SaveAs(physicalFilename);

                    tourImage.ImageUrl = newFilenameUrl;
                }
                #endregion
                tourImage.IsDelete   = false;
                tourImage.SubmitDate = DateTime.Now;
                tourImage.Id         = Guid.NewGuid();
                tourImage.TourId     = id;
                db.TourImages.Add(tourImage);
                db.SaveChanges();
                return(RedirectToAction("Index", new { id = id }));
            }


            return(View(tourImage));
        }
        public ActionResult DeleteConfirmed(Guid id)
        {
            TourImage tourImage = db.TourImages.Find(id);

            tourImage.IsDelete   = true;
            tourImage.DeleteDate = DateTime.Now;

            db.SaveChanges();
            return(RedirectToAction("Index", new { id = tourImage.TourId }));
        }
        public ActionResult Edit(TourCreateEditModel tour, int Id)
        {
            Tour          thisTour   = db.Tours.Find(Id);
            List <string> Categories = new List <string>();

            foreach (int catId in tour.Categories)
            {
                Categories.Add(db.Categories.Find(catId).CategoryName.ToLower());
            }

            if (ModelState.IsValid)
            {
                thisTour.FromId              = tour.FromCity;
                thisTour.DestinationId       = tour.DestCity;
                thisTour.Price               = (decimal)tour.Price;
                thisTour.CurrencyId          = tour.Currency;
                thisTour.Duration            = tour.Duration;
                thisTour.DurationTypeId      = tour.DurationType;
                thisTour.Category            = String.Join(",", Categories.ToArray());
                thisTour.AccomodationId      = tour.Accomodation != null ? tour.Accomodation : null;
                thisTour.AccomodationLevelId = tour.AccomodationLvl != null ? tour.AccomodationLvl : null;
                thisTour.Vehicle             = tour.Transport;
                thisTour.Description         = tour.Description;
                thisTour.Approved            = 0;

                db.Entry(thisTour).State = EntityState.Modified;
                db.SaveChanges();
            }

            if (tour.Images.Count > 0)
            {
                foreach (HttpPostedFileBase img in tour.Images)
                {
                    string fileName = tour.GuideId + DateTime.Now.ToString("yyyyMMddHHmmss") + img.FileName;
                    string path     = System.IO.Path.Combine(Server.MapPath("~/uploads"), fileName);
                    img.SaveAs(path);
                    string    image     = "/uploads/" + fileName;
                    TourImage tourImage = new TourImage
                    {
                        ImageURL = image,
                        TourId   = thisTour.Id,
                    };
                    db.TourImages.Add(tourImage);
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("edit", new { thisTour.Id }));
        }
        // GET: TourImages/Edit/5
        public ActionResult Edit(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TourImage tourImage = db.TourImages.Find(id);

            if (tourImage == null)
            {
                return(HttpNotFound());
            }
            ViewBag.id = tourImage.TourId;
            return(View(tourImage));
        }
        public JsonResult DeleteTourImage(int Id)
        {
            TourImage timg = db.TourImages.Find(Id);
            Tour      tour = db.Tours.FirstOrDefault(t => t.MainImageId == Id);

            if (tour != null)
            {
                tour.MainImageId = 1;
            }
            timg.TourId = null;
            db.SaveChanges();
            string path = Server.MapPath(timg.ImageURL);

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
            db.TourImages.Remove(timg);
            db.SaveChanges();

            return(Json(1, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Create(TourCreateEditModel tour)
        {
            if (ModelState.IsValid)
            {
                List <string> Categories = new List <string>();
                foreach (int catId in tour.Categories)
                {
                    Categories.Add(db.Categories.Find(catId).CategoryName.ToLower());
                }

                Tour newTour = new Tour
                {
                    GuideId             = tour.GuideId,
                    FromId              = tour.FromCity,
                    DestinationId       = tour.DestCity,
                    Price               = (decimal)tour.Price,
                    CurrencyId          = tour.Currency,
                    Category            = String.Join(",", Categories.ToArray()),
                    Duration            = tour.Duration,
                    DurationTypeId      = tour.DurationType,
                    AccomodationId      = tour.Accomodation != null ? tour.Accomodation : null,
                    AccomodationLevelId = tour.AccomodationLvl != null ? tour.AccomodationLvl : null,
                    Vehicle             = tour.Transport,
                    Description         = tour.Description,
                    PostedDate          = DateTime.Now,
                    MainImageId         = 1,
                    Status              = 0,
                    Approved            = 0
                };
                db.Tours.Add(newTour);
                db.SaveChanges();

                if (tour.Images.Count > 0)
                {
                    foreach (HttpPostedFileBase img in tour.Images)
                    {
                        string fileName = tour.GuideId + DateTime.Now.ToString("yyyyMMddHHmmss") + img.FileName;
                        string path     = System.IO.Path.Combine(Server.MapPath("~/uploads"), fileName);
                        img.SaveAs(path);
                        string    image     = "/uploads/" + fileName;
                        TourImage tourImage = new TourImage
                        {
                            ImageURL = image,
                            TourId   = newTour.Id,
                        };
                        db.TourImages.Add(tourImage);
                        db.SaveChanges();
                    }
                }

                newTour.MainImageId = db.TourImages.Where(ti => ti.TourId == newTour.Id).OrderBy(ti => ti.Id).FirstOrDefault().Id;
                db.SaveChanges();

                Notification noti = new Notification
                {
                    UserId             = db.Users.FirstOrDefault(u => u.AccountType == 2).Id,
                    Text               = "New tour from " + db.Users.Find(tour.GuideId).Fullname,
                    Date               = DateTime.Now,
                    NotificationTypeId = 8,
                    Link               = "0",
                    Status             = 0,
                };
                db.Notifications.Add(noti);
                db.SaveChanges();
                noti.Link = "/admin/tours/details/?id=" + newTour.Id + "&notiId=" + noti.Id;
                db.SaveChanges();
            }
            ;

            return(RedirectToAction("index", new { controller = "tours", area = "manage", id = tour.GuideId }));
        }