public ActionResult DeleteConfirmed(int id)
        {
            db.tb_SanPhamTrans.RemoveRange(db.tb_SanPhamTrans.Where(t => t.MaSP == id));
            db.SaveChanges();

            tb_SanPham tb_sanpham = db.tb_SanPham.Find(id);

            db.tb_SanPham.Remove(tb_sanpham);
            db.tb_NhatKy.Add(new tb_NhatKy {
                NguoiDung = (string)Session["username"], DoiTuong = "Sản phẩm", ThaoTac = DateTime.Now.ToString("dd/MM/yyy hh:mm:ss") + " - Xóa sản phẩm\"" + "\"", MaDoiTuong = tb_sanpham.MaSP
            });
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tb_SanPham tb_sanpham = db.tb_SanPham.Find(id);

            if (tb_sanpham == null)
            {
                return(HttpNotFound());
            }
            return(View(tb_sanpham));
        }
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tb_SanPham tb_sanpham = db.tb_SanPham.Find(id);

            if (tb_sanpham == null)
            {
                return(HttpNotFound());
            }

            var spTran = db.tb_SanPhamTrans.Where(x => x.NgonNgu == "vi" && x.MaSP == id).FirstOrDefault();

            ViewBag.SpVi = spTran ?? new tb_SanPhamTrans();
            spTran       = db.tb_SanPhamTrans.Where(x => x.NgonNgu == "en" && x.MaSP == id).FirstOrDefault();
            ViewBag.SpEn = spTran ?? new tb_SanPhamTrans();
            return(View(tb_sanpham));
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tb_SanPham tb_sanpham = db.tb_SanPham.Find(id);
            string     lang       = Request.RequestContext.RouteData.Values["lang"] as string ?? "vi";

            ViewBag.MaLoai = new SelectList(db.tb_LoaiSPTrans.Where(x => x.NgonNgu == lang), "MaLoaiSP", "TenLoaiSPTrans", tb_sanpham.MaLoai);
            if (tb_sanpham == null)
            {
                return(HttpNotFound());
            }
            var spTran = db.tb_SanPhamTrans.Where(x => x.NgonNgu == "vi" && x.MaSP == id).FirstOrDefault();

            ViewBag.SpVi = spTran ?? new tb_SanPhamTrans();
            spTran       = db.tb_SanPhamTrans.Where(x => x.NgonNgu == "en" && x.MaSP == id).FirstOrDefault();
            ViewBag.SpEn = spTran ?? new tb_SanPhamTrans();
            return(View(tb_sanpham));
        }
        public ActionResult Create([Bind(Include = "MaSP,HinhAnh,QuyCachDongGoi,XuatXu,MaLoai")] tb_SanPham tb_sanpham)
        {
            if (ModelState.IsValid)
            {
                tb_sanpham.TrangThai = true;
                db.tb_SanPham.Add(tb_sanpham);
                db.SaveChanges();
                //  db.tb_NhatKy.Add(new tb_NhatKy { NguoiDung = (string)Session["username"], DoiTuong = "Sản phẩm", ThaoTac = DateTime.Now.ToString("dd/MM/yyy hh:mm:ss") + " - Thêm sản phẩm\"" + tb_sanpham.tb_SanPhamTrans.FirstOrDefault().TenSanPhamTrans+ "\"", MaDoiTuong = tb_sanpham.MaSP});
            }

            tb_SanPhamTrans tb_spVi = new tb_SanPhamTrans();

            tb_spVi.NgonNgu             = "vi";
            tb_spVi.MaSP                = tb_sanpham.MaSP;
            tb_spVi.TenSanPhamTrans     = Request.Form["tenSPVn"];
            tb_spVi.UuDienTrans         = Request.Form["uuDiemVn"];
            tb_spVi.DacDiemTrans        = Request.Form["dacDiemVn"];
            tb_spVi.ThanhPhanChinhTrans = Request.Form["tpChinhVn"];
            tb_spVi.LuuYTrans           = Request.Form["luuYVn"];
            tb_spVi.CachDungTrans       = Request.Form["cachDungVn"];
            db.tb_SanPhamTrans.Add(tb_spVi);
            db.SaveChanges();
            tb_SanPhamTrans tb_SpEn = new tb_SanPhamTrans();

            tb_SpEn.NgonNgu             = "en";
            tb_SpEn.MaSP                = tb_sanpham.MaSP;
            tb_SpEn.TenSanPhamTrans     = Request.Form["tenSPEn"];
            tb_SpEn.UuDienTrans         = Request.Form["uuDiemEn"];
            tb_SpEn.DacDiemTrans        = Request.Form["dacDiemEn"];
            tb_SpEn.ThanhPhanChinhTrans = Request.Form["tpChinhEn"];
            tb_SpEn.LuuYTrans           = Request.Form["luuYEn"];
            tb_SpEn.CachDungTrans       = Request.Form["cachDungEn"];
            db.tb_SanPhamTrans.Add(tb_SpEn);
            db.SaveChanges();

            ViewBag.MaLoai = new SelectList(db.tb_LoaiSP, "MaLoaiSP", "TenLoaiSP", tb_sanpham.MaLoai);
            return(RedirectToAction("Index"));
        }
        public ActionResult Details(int?id)
        {
            ViewBag.CurrentMenu = "SanPham";
            if (id != null)
            {
                string     lang    = Request.RequestContext.RouteData.Values["lang"] as string ?? "vi";
                tb_SanPham sanPham = db.tb_SanPham.Find(id);
                var        list    = sanPham.tb_SanPhamTrans.Where(x => x.NgonNgu == lang && x.MaSP == id).ToList();
                sanPham.tb_SanPhamTrans = list;
                if (sanPham.tb_SanPhamTrans.FirstOrDefault() != null)
                {
                    if (string.IsNullOrWhiteSpace(sanPham.tb_SanPhamTrans.FirstOrDefault().TenSanPhamTrans))
                    {
                        lang = (lang == "en" ? "vi" : "en");
                        sanPham.tb_SanPhamTrans = db.tb_SanPhamTrans.Where(x => x.NgonNgu == lang && x.MaSP == id).ToList();
                    }
                }

                var spLienQuan =
                    db.tb_SanPham.Where(x => x.MaLoai == sanPham.MaLoai && x.MaSP != sanPham.MaSP)
                    .OrderBy(r => Guid.NewGuid())
                    .Take(6).ToList();
                foreach (tb_SanPham sp in spLienQuan)
                {
                    var spTran = sp.tb_SanPhamTrans.Where(y => y.NgonNgu == lang).ToList();
                    if (spTran != null && string.IsNullOrWhiteSpace(spTran.FirstOrDefault().TenSanPhamTrans))
                    {
                        lang   = lang == "en" ? "vi" : "en";
                        spTran = sp.tb_SanPhamTrans.Where(y => y.NgonNgu == lang).ToList();
                    }
                    sp.tb_SanPhamTrans = spTran;
                }
                ViewBag.SpLienQuan = spLienQuan;
                return(View(sanPham));
            }
            return(PartialView("Error"));
        }
        public ActionResult Edit([Bind(Include = "MaSP,HinhAnh,TenSanPham,UuDiem,DacDiem,ThanhPhanChinh,LuuY,QuyCachDongGoi,XuatXu,CachDung,GiaThanh,TrangThai,MaLoai")] tb_SanPham tb_sanpham)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_sanpham).State = EntityState.Modified;
                db.tb_NhatKy.Add(new tb_NhatKy {
                    NguoiDung = (string)Session["username"], DoiTuong = "Sản phẩm", ThaoTac = DateTime.Now.ToString("dd/MM/yyy hh:mm:ss") + " - Sửa sản phẩm\"" + "\"", MaDoiTuong = tb_sanpham.MaSP
                });
                db.SaveChanges();
            }
            bool            add     = false;
            tb_SanPhamTrans tb_spVi =
                db.tb_SanPhamTrans.FirstOrDefault(x => x.NgonNgu == "vi" && x.MaSP == tb_sanpham.MaSP);

            if (tb_spVi == null)
            {
                tb_spVi = new tb_SanPhamTrans();
                add     = true;
            }
            tb_spVi.NgonNgu             = "vi";
            tb_spVi.MaSP                = tb_sanpham.MaSP;
            tb_spVi.TenSanPhamTrans     = Request.Params["tenSPVn"];
            tb_spVi.UuDienTrans         = Request.Params["uuDiemVn"];
            tb_spVi.DacDiemTrans        = Request.Params["dacDiemVn"];
            tb_spVi.ThanhPhanChinhTrans = Request.Params["tpChinhVn"];
            tb_spVi.LuuYTrans           = Request.Params["luuYVn"];
            tb_spVi.CachDungTrans       = Request.Params["cachDungVn"];
            if (add)
            {
                db.tb_SanPhamTrans.Add(tb_spVi);
            }
            else
            {
                db.Entry(tb_spVi).State = EntityState.Modified;
            }
            tb_SanPhamTrans tb_SpEn =
                db.tb_SanPhamTrans.FirstOrDefault(x => x.NgonNgu == "en" && x.MaSP == tb_sanpham.MaSP);

            add = false;
            if (tb_SpEn == null)
            {
                tb_SpEn = new tb_SanPhamTrans();
                add     = true;
            }

            tb_SpEn.NgonNgu             = "en";
            tb_SpEn.MaSP                = tb_sanpham.MaSP;
            tb_SpEn.TenSanPhamTrans     = Request.Params["tenSPEn"];
            tb_SpEn.UuDienTrans         = Request.Params["uuDiemEn"];
            tb_SpEn.DacDiemTrans        = Request.Params["dacDiemEn"];
            tb_SpEn.ThanhPhanChinhTrans = Request.Params["tpChinhEn"];
            tb_SpEn.LuuYTrans           = Request.Params["luuYEn"];
            tb_SpEn.CachDungTrans       = Request.Params["cachDungEn"];
            db.tb_SanPhamTrans.Add(tb_SpEn);
            if (add)
            {
                db.tb_SanPhamTrans.Add(tb_SpEn);
            }
            else
            {
                db.Entry(tb_SpEn).State = EntityState.Modified;
            }

            if (db.SaveChanges() > 0)
            {
                return(RedirectToAction("Index"));
            }
            ViewBag.SpVi   = tb_spVi;
            ViewBag.SpEn   = tb_SpEn;
            ViewBag.MaLoai = new SelectList(db.tb_LoaiSP, "MaLoaiSP", "TenLoaiSP", tb_sanpham.MaLoai);
            return(View(tb_sanpham));
        }