public ActionResult EditWatch([Bind(Include = "WatchId, WatchCode, WatchDescription, Quantity, Price, " +
                                                      "MovementID, ModelID, BandMaterial, CaseMaterial, " +
                                                      "CaseRadius, Discount, Guarantee, PublishedBy, PublishedTime")]
                                      Watch watch, HttpPostedFileBase thumbnail)
        {
            if (ModelState.IsValid)
            {
                watch.WaterResistant = Request["water"] == "yes";
                watch.LEDLight       = Request["led"] == "yes";
                watch.Alarm          = Request["alarm"] == "yes";
                watch.Status         = Request["status"] == "yes";
                watch.WatchCode      = watch.WatchCode.ToUpper();
                bool duplicateCode = watchService.IsDuplicatedWatchCode(watch.WatchCode, watch.WatchID);

                bool validImage = true;
                if (thumbnail != null)
                {
                    validImage = FileTypeDetector.IsImageFile(thumbnail);
                    ;
                }

                if (duplicateCode || !validImage)
                {
                    var movement   = movementService.GetMovementList();
                    var watchModel = watchModelService.GetModelsList();
                    ManageWatchDetailViewModel viewModel =
                        watchService.PrepopulateEditValue(watch, movement, watchModel);
                    if (duplicateCode)
                    {
                        viewModel.DuplicateErrorMessage =
                            "Watch with code '" + watch.WatchCode + "' already existed. Please choose another one";
                    }

                    if (!validImage)
                    {
                        viewModel.InvalidImageFileMessage = "Invalid Thumbnail. Upload file is not image";
                    }

                    return(View("~/Views/Admin/admin_manage_watch_detail.cshtml", viewModel));
                }

                String oldValue = watchService.SerializeOldValue(watch.WatchID);
                //thumbnail.InputStream.Position = 0;

                if (watchService.UpdateWatchInfo(watch, thumbnail))
                {
                    //save old value to modification table
                    String userId = Session.GetCurrentUserInfo("Username");
                    if (modificationService.CreateNewModificationHistory(watch.WatchID, oldValue, userId))
                    {
                        TempData["SHOW_MODAL"] = @"<script>$('#successModal').modal();</script>";
                        return(RedirectToAction("ViewWatch", "Admin"));
                    }
                }

                return(Content("Unexpected Error")); //change to 404
            }

            return(RedirectToAction("NotFound", "Home"));
        }
        public ActionResult AddWatch([Bind(Include =
                                               "WatchDescription, WatchCode, Quantity, Price, MovementID, ModelId, BandMaterial, CaseRadius, CaseMaterial, Discount, Guarantee")]
                                     Watch watch, HttpPostedFileBase thumbnail)
        {
            if (ModelState.IsValid)
            {
                watch.WaterResistant = Request["water"] == "yes";
                watch.LEDLight       = Request["led"] == "yes";
                watch.Alarm          = Request["alarm"] == "yes";
                watch.WatchCode      = watch.WatchCode.ToUpper();
                bool duplicateCode = watchService.IsDuplicatedWatchCode(watch.WatchCode);
                bool validImage    = FileTypeDetector.IsImageFile(thumbnail);
                //check if watch code is unique
                if (!validImage || duplicateCode)
                {
                    //code đã tồn tại hoặc hình ảnh không hợp lệ
                    //thông báo điền lại code
                    //prefill các field
                    var movement   = movementService.GetMovementList();
                    var watchModel = watchModelService.GetModelsList();
                    var viewModel  = watchService.PrepopulateInputValue(watch, movement, watchModel);
                    if (duplicateCode)
                    {
                        viewModel.DuplicateErrorMessage = "Watch with code '" + watch.WatchCode + "' already existed";
                    }

                    if (!validImage)
                    {
                        viewModel.InvalidImageFileMessage = "Invalid Thumbnail. Upload file is not an image";
                    }

                    return(View("~/Views/Admin/admin_manage_watch_add.cshtml", viewModel));
                }

                //lưu hình ảnh xuống máy
                string path = HostingEnvironment.MapPath("~/Content/img/ProductThumbnail/") + watch.WatchCode +
                              DateTime.Now.ToBinary();
                //thumbnail.InputStream.Position = 0;
                ImageProcessHelper.ResizedImage(thumbnail.InputStream, 360, 500, ResizeMode.Pad, ref path);
                watch.Thumbnail     = path;
                watch.PublishedTime = DateTime.Now;
                watch.PublishedBy   = Session.GetCurrentUserInfo("Username");
                watch.Status        = true;
                if (watchService.AddNewWatch(watch))
                {
                    TempData["SHOW_MODAL"] = @"<script>$('#successModal').modal();</script>";
                    return(RedirectToAction("AddWatch", "Admin"));
                }

                return(Content("Unexpected Error"));
            }

            return(RedirectToAction("NotFound", "Home"));
        }