public ActionResult Edit(tbl_Website model)
        {
            //ViewBag.error = "";
            if (Session["Member"] == null)
                return RedirectToAction("Index", "TrangChu");
            List<String> error = new List<string>();

            if (model.MoTa == null)
                error.Add("Bạn chưa nhập mô tả cho website");
            if (model.TenWebsite == null)
                error.Add("Bạn chưa nhập tên cho website");
            if (model.URL == null)
                error.Add("Bạn chưa nhập URL cho website");
            if (model.CoinPerClick == null || model.CoinPerClick < 100)
                error.Add("CoinPerClick phải là số và tối thiểu là 100 VNĐ");
            if (error.Count > 0)
            {
                ViewBag.error = error;
                return View();
            }
            if (ModelState.IsValid)
            {
                try
                {
                    model.Active = 0;
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChanges();
                    error.Add("Edit success!");
                    ViewBag.error = error;
                }
                catch
                {
                    error.Add("Error!");
                    ViewBag.error = error;
                }
            }
            return View(model);
        }
        public ActionResult Edit(tbl_Website web, HttpPostedFileBase file)
        {
            if (Session["Admin"] == null && Session["Mod"] == null && Session["SA"] == null)
                RedirectToAction("Index", "Login");
            if (Session["Mod"] != null)
            {
                string mod = (string)Session["Mod"];
                tbl_Account model = db.tbl_Account.FirstOrDefault(m => m.Username.Equals(mod));
                int id_danhmuc = (int)model.ID_DanhMuc;
                var tendanhmuccha = from i in db.tbl_DanhMuc
                                    where i.ID_DanhMuc == id_danhmuc
                                    select i;
                foreach (var i in tendanhmuccha)
                {
                    ViewBag.name = i.TenDanhMuc;
                }
            }
            List<String> error = new List<string>();
            if (web.MoTa == null)
                error.Add("MoTa field is required.");
            if (web.TenWebsite == null)
                error.Add("TenWebsite field is required.");
            if (web.URL == null)
                error.Add("URL field is required.");
            if (web.XepHang == null)
                error.Add("XepHang field is required.");
            if (error.Count > 0)
            {
                ViewBag.error = error;
                return View();
            }
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    db.Entry(web).State = EntityState.Modified;
                    try
                    {
                        if (web.HinhAnh == null)
                        {
                            error.Add("HinhAnh is required.");
                            ViewBag.error = error;
                            return View();
                        }
                        db.SaveChanges();
                        error.Add("Edit website success.");
                        ViewBag.error = error;
                    }
                    catch (Exception)
                    {
                        error.Add("The URL is already exits.");
                        ViewBag.error = error;
                    }

                }
                else
                {
                    string pic = Path.GetFileName(file.FileName);
                    web.HinhAnh = pic;
                    string path = Path.Combine(Server.MapPath("~/Content/Image/Website"), pic);
                    file.SaveAs(path);
                    db.Entry(web).State = EntityState.Modified;
                    try
                    {
                        db.SaveChanges();
                        error.Add("Edit website success.");
                        ViewBag.error = error;
                    }
                    catch (Exception)
                    {
                        error.Add("The URL is already exits.");
                        ViewBag.error = error;
                    }
                }
            }
            return View();
        }
        public ActionResult AddWebsite(tbl_Website web, HttpPostedFileBase file)
        {
            if (Session["Admin"] == null && Session["Mod"] == null && Session["SA"] == null)
                RedirectToAction("Index", "Login");

            List<String> error = new List<string>();

            if (web.MoTa == null)
                error.Add("MoTa field is required.");
            if (web.TenWebsite == null)
                error.Add("TenWebsite field is required.");
            if (web.URL == null)
                error.Add("URL field is required.");
            if (web.XepHang == null)
                error.Add("XepHang field is required.");
            if (error.Count > 0)
            {
                ViewBag.error = error;
                return View();
            }
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    string pic = Path.GetFileName(file.FileName);
                    web.HinhAnh = pic;
                    string path = Path.Combine(Server.MapPath("~/Content/Image/Website"), pic);
                    file.SaveAs(path);
                    web.NgayCapNhat = DateTime.Now;
                    db.tbl_Website.Add(web);
                    try
                    {
                        db.SaveChanges();
                        error.Add("Create website success.");
                        ViewBag.error = error;
                    }
                    catch
                    {
                        error.Add("The website is already exists.");
                        ViewBag.error = error;
                    }
                    return View();
                }
                else
                {
                    error.Add("HinhAnh is not selected.");
                    ViewBag.error = error;
                    return View();
                }
            }
            else
                return View();
        }
        public ActionResult AddWebMod(tbl_Website web, HttpPostedFileBase file, int id)
        {
            if (Session["Admin"] == null && Session["Mod"] == null)
                RedirectToAction("Index", "Login");
            ViewBag.id = id;
            List<String> error = new List<string>();

            if (web.MoTa == null)
                error.Add("MoTa field is required.");
            if (web.TenWebsite == null)
                error.Add("TenWebsite field is required.");
            if (web.URL == null)
                error.Add("URL field is required.");
            if (web.XepHang == null)
                error.Add("XepHang field is required.");
            if (error.Count > 0)
            {
                ViewBag.error = error;
                return View();
            }
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    tbl_WebDM webDM = new tbl_WebDM();
                    tbl_Website temp = new tbl_Website();
                    temp = db.tbl_Website.FirstOrDefault(m => m.URL.Equals(web.URL));
                    if (temp != null)
                    {
                        webDM.ID_DanhMuc = id;
                        webDM.ID_Website = temp.ID_Web;
                        db.tbl_WebDM.Add(webDM);
                        db.SaveChanges();
                        error.Add("The website is already exists. This website added in this category");
                        ViewBag.error = error;
                        return View();
                    }
                    else
                    {
                        string pic = Path.GetFileName(file.FileName);
                        web.HinhAnh = pic;
                        string path = Path.Combine(Server.MapPath("~/Content/Image/Website"), pic);
                        file.SaveAs(path);
                        web.NgayCapNhat = DateTime.Now;
                        db.tbl_Website.Add(web);

                        try
                        {
                            webDM.ID_Website = web.ID_Web;
                            webDM.ID_DanhMuc = id;
                            db.tbl_WebDM.Add(webDM);
                            db.SaveChanges();
                            error.Add("Create website success.");
                            ViewBag.error = error;
                        }
                        catch
                        {

                            error.Add("ERROR.");
                            ViewBag.error = error;
                        }
                    }
                }
                else
                {
                    error.Add("HinhAnh is not selected.");
                    ViewBag.error = error;
                }
            }

                return View();
        }
 public ActionResult WebRegister(tbl_Website model)
 {
     if (Session["Member"] == null)
         return RedirectToAction("Index", "TrangChu");
     List<String> error = new List<string>();
     if(model.MoTa == null)
         error.Add("Bạn chưa nhập mô tả cho website");
     if (model.TenWebsite == null)
         error.Add("Bạn chưa nhập tên cho website");
     if (model.URL == null)
         error.Add("Bạn chưa nhập URL cho website");
     if (model.CoinPerClick == null || model.CoinPerClick < 100)
         error.Add("CoinPerClick phải là số và tối thiểu là 100 VNĐ");
     if (error.Count > 0)
     {
         ViewBag.error = error;
         return View();
     }
     try {
         String user = Session["Member"].ToString();
         var acc = db.tbl_Account.Where(m => m.Username.Equals(user)).FirstOrDefault();
         int id = acc.ID_Account;
         model.ID_Account = id;
         model.Active = 0;
         db.tbl_Website.Add(model);
         db.SaveChanges();
         error.Add("Đăng ký website thành công! Bạn phải chờ để admin duyệt lại web");
         ViewBag.error = error;
     }catch(Exception)
     {
         error.Add("Website này đã tồn tại");
         ViewBag.error = error;
     }
     return View();
 }
        public ActionResult WebClick(int? id)
        {
            tbl_Website web = new tbl_Website();
            tbl_Account acc = new tbl_Account();
            long coinPerClick = 0;
            long coinPerDay = 0;
            int id_acc;
            DateTime lastClick;
            long timeSpan = 0;
            try
            {
                DateTime now = DateTime.Now;

                web = db.tbl_Website.Where(m => m.ID_Web == id).FirstOrDefault();
                if (web.LastClick != null)
                {
                    lastClick = (DateTime)web.LastClick;
                    timeSpan = (now - lastClick).Minutes;
                }
                else
                {
                    web.LastClick = now;
                    timeSpan = 11;
                }
                if (timeSpan > 10)
                {
                    coinPerClick = (long)web.CoinPerClick;
                    id_acc = (int)web.ID_Account;
                    acc = db.tbl_Account.Where(m => m.ID_Account == id_acc).FirstOrDefault();
                    coinPerDay = (long)acc.CoinPerDay;
                    if (coinPerDay >= coinPerClick)
                        acc.CoinPerDay -= coinPerClick;
                    else if (acc.TotalCoin >= coinPerClick)
                        acc.TotalCoin -= coinPerClick;
                    if (coinPerDay < coinPerClick && acc.TotalCoin < coinPerClick)
                        web.Active = 0;
                    web.LastClick = now;
                    web.TongSoClick++;
                }
                else
                {
                    web.SoClickFree++;
                    web.TongSoClick++;
                }

                db.SaveChanges();
            }catch(Exception)
            {

            }
            HomeViewModel h = new HomeViewModel();
            return View(h);
        }