public IActionResult ProductTypeEdit(AdminProductTypesViewModel vm)
 {
     if (IsLogedIn() == true)
     {
         if (ModelState.IsValid)
         {
             ProductType data = vm.ProductType;
             ProductType up   = _context.ProductTypes.Where(p => p.Id == data.Id).FirstOrDefault();
             up.Type       = data.Type;
             up.DateModify = data.DateModify;
             up.URL        = StaticClass.ToUrlFriendly(data.Type);
             _context.SaveChanges();
         }
         return(View(
                    new AdminProductTypesViewModel()
         {
             ProductType = _context.ProductTypes.Where(p => p.Id == vm.ProductType.Id).FirstOrDefault(),
             ProductTypes = _context.ProductTypes.OrderBy(p => p.Type).ToList(),
             Products = _context.Products.ToList()
         }
                    ));
     }
     else
     {
         return(RedirectToAction("Login"));
     }
 }
 public IActionResult ProductTypesOverview(AdminProductTypesViewModel vm)
 {
     if (IsLogedIn() == true)
     {
         if (ModelState.IsValid)
         {
             vm.ProductType.URL = StaticClass.ToUrlFriendly(vm.ProductType.Type);
             _context.ProductTypes.Add(vm.ProductType);
             _context.SaveChanges();
         }
         return(View(new AdminProductTypesViewModel()
         {
             ProductType = new ProductType()
             {
                 URL = "Chưa có đường dẫn url",
                 Admin_Id = (int)HttpContext.Session.GetInt32("Admin_Id")
             },
             ProductTypes = _context.ProductTypes.OrderBy(p => p.Type).ToList(),
             Products = _context.Products.ToList()
         }));
     }
     else
     {
         return(RedirectToAction("Login"));
     }
 }
        public IActionResult ProductNew(ProductNewViewModel viewmodel)
        {
            if (IsLogedIn() == true)
            {
                Product new_product = viewmodel.Product;
                new_product.Thumbnail = "#";
                // Tải hình ảnh sản phẩm lên thư mục wwwroot/uploads
                if (viewmodel.Thumbnail != null)
                {
                    new_product.Thumbnail = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string uniqueName = new_product.Thumbnail;                                               // Tạo tên hình ảnh theo chuỗi ngày tháng lúc đăng ảnh
                    string newpath    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads"); // Trỏ đường dẫn đến thư mục wwwroot/uploads
                    newpath = Path.Combine(newpath, uniqueName);                                             // Trỏ đường dẫn đến tên hình ảnh
                    newpath = newpath + Path.GetExtension(viewmodel.Thumbnail.FileName);                     // Gắn đuôi (loại file) cho hình
                    viewmodel.Thumbnail.CopyTo(new FileStream(newpath, FileMode.Create));                    // Copy hình từ nguồn sang wwwroot/uploads
                    new_product.Thumbnail += Path.GetExtension(viewmodel.Thumbnail.FileName);
                }

                // Đổi slug theo tên sản phẩm
                Slug   edit_slug = _context.Slugs.Where(s => s.Id == new_product.Slug_Id).FirstOrDefault();
                string new_slug  = StaticClass.ToUrlFriendly(new_product.Name);
                if (IsSlugExists(new_slug) == false)
                {
                    edit_slug.Url = new_slug;
                }

                _context.Products.Add(new_product);
                _context.SaveChanges();
                return(RedirectToAction("ProductsOverview", "Admin"));
            }
            else
            {
                return(RedirectToAction("Login"));
            }
        }
        public IActionResult ProductEdit(ProductEditViewModel vm)
        {
            if (IsLogedIn() == true)
            {
                if (ModelState.IsValid)
                {
                    Product data = vm.Product;
                    Product up   = _context.Products.Where(p => p.Id == data.Id).FirstOrDefault();

                    up.Name            = data.Name;
                    up.Description     = data.Description;
                    up.ProductBrand_Id = data.ProductBrand_Id;
                    up.ProductType_Id  = data.ProductType_Id;
                    up.Description     = data.Description;
                    up.Price           = data.Price;
                    up.Saleoff         = data.Saleoff;
                    up.DateModify      = data.DateModify;
                    up.Instore         = data.Instore;
                    up.BuyPoints       = data.BuyPoints;

                    if (vm.Thumbnail != null)
                    {
                        // Xóa ảnh cũ
                        string old_image = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", data.Thumbnail);
                        if (System.IO.File.Exists(old_image))
                        {
                            System.IO.File.Delete(old_image);
                        }

                        // Lưu ảnh mới
                        string rename    = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + Path.GetExtension(vm.Thumbnail.FileName);
                        string new_image = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", rename);
                        vm.Thumbnail.CopyTo(new FileStream(new_image, FileMode.Create));
                        up.Thumbnail = rename;
                    }

                    // Đổi slug theo tên sản phẩm
                    Slug   edit_slug = _context.Slugs.Where(s => s.Id == up.Slug_Id).FirstOrDefault();
                    string new_slug  = StaticClass.ToUrlFriendly(up.Name);
                    if (IsSlugExists(new_slug) == false)
                    {
                        edit_slug.Url = new_slug;
                    }

                    _context.SaveChanges();

                    return(View(
                               new ProductEditViewModel()
                    {
                        Product = _context.Products.Where(p => p.Id == vm.Product.Id).FirstOrDefault(),
                        ProductBrands = _context.ProductBrands.OrderBy(p => p.Name).ToList(),
                        ProductTypes = _context.ProductTypes.OrderBy(p => p.Type).ToList()
                    }
                               ));
                }
                else
                {
                    return(View(
                               new ProductEditViewModel()
                    {
                        Product = vm.Product,
                        ProductBrands = _context.ProductBrands.OrderBy(p => p.Name).ToList(),
                        ProductTypes = _context.ProductTypes.OrderBy(p => p.Type).ToList()
                    }
                               ));
                }
            }
            else
            {
                return(RedirectToAction("Login"));
            }
        }