//
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            var viewModel = new StoreManagerViewModel { 
                Album = new Album(),
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };

            return View(viewModel);
        } 
        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            // Lay gia tri (đã lấy theo ID)
            // Truyen vao ViewModel
        
            var viewModel = new StoreManagerViewModel
            {
                Album = storeDB.Albums.Single(a=>a.AlbumId == id),
                Genres = storeDB.Genres.ToList(),
                Artists = storeDB.Artists.ToList()
            };
            
            // Xuat ra view
            return View(viewModel);
        }
        //public ActionResult Create(FormCollection collection)
        public ActionResult Create(Album album)
        {
            try
            {
                // TODO: Add insert logic here

                storeDB.AddToAlbums(album);
                storeDB.SaveChanges();
                //return RedirectToAction("Index");
                return RedirectToAction("/");
            }
            catch
            {
                // Invalid - ReDisplay with Errors
                var viewModel = new StoreManagerViewModel { 
                    Album = album,
                    Artists = storeDB.Artists.ToList(),
                    Genres = storeDB.Genres.ToList()
                };
                return View();
            }
        }
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            // Lay gia tri và truyền vào biến viewModel
            var viewModel = new StoreManagerViewModel
            {
                Album = storeDB.Albums.Single(a => a.AlbumId == id),
                Artists = storeDB.Artists.ToList(),
                Genres = storeDB.Genres.ToList()
            };

            viewModel.Album.Title = "TEST " + viewModel.Album.Title;

            // Trả kết quả ra cho View
            return View(viewModel);
        }
        public ActionResult Edit(int id, FormCollection collection)
        {
            // Lấy thông tin đối tượng cần cập nhật thông qua ID
            var album = storeDB.Albums.Single(a => a.AlbumId == id);

            try
            {
                // TODO: Add update logic here
                // Cập nhật thông tin mới
                UpdateModel(album, "Album"); // cap nhật doi tuong Album tu Model
                storeDB.SaveChanges();

                // Lưu xong trả về trang Index
                return RedirectToAction("Index");
                
            }
            catch
            {
                // Trả lại trang View nếu không cập nhật được
                var viewModel = new StoreManagerViewModel
                {
                    Album = album,
                    Artists = storeDB.Artists.ToList(),
                    Genres = storeDB.Genres.ToList()
                };

                return View(viewModel);
            }
        }