public ActionResult Rating(int bookID, double score)
        {
            string username = getUser();
            if (username == null)
            {
                return Json(new { Success = false, Message = "Bạn chưa đăng nhập" });
            }
            using (var dbContext = new CocBookEntities())
            {
                var rating = new Rating();
                rating.RateDate = DateTime.Now;
                rating.BookID = bookID;
                rating.Username = username;
                rating.Score = score;
                dbContext.Ratings.Add(rating);
                dbContext.SaveChanges();
            }

            return Json(new { Success = true , Message="Bạn đã chấm " +score+ " điểm"});
        }
        public ActionResult Order(FormCollection form)
        {
            #region GetInfo
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                return RedirectToAction("Index", "Home");
            }
            string username = getUser();
            Customer cus;

            if (username == null)
            {
                cus = new Customer();
                cus.Username = "******";
            }
            else
            {
                using (var dbContext = new CocBookEntities())
                {
                    cus = (from c in dbContext.Customers
                           where c.Username == username
                           select c).Single();
                }
            }
            string fullname = form["full-name"];
            string phone = form["phone"];
            string district = form["district"];
            string address = form["address"];
            string payMethod = form["optPaymentMethod"];
            string speed = form["optSpeed"];
            string comment = form["comment"];
            #endregion
            int oid;
            #region process order
            using (var dbContext = new CocBookEntities())
            {
                Order order = new Order();
                order.Username = cus.Username;
                order.RequestDate = DateTime.Now;
                order.Notes = comment;
                order.Total = cart.GetTotal();
                order.Fullname = fullname;
                order.Phone = phone;
                order.District = district;
                order.Street = address;
                order.City = "HCM";
                order.Elog = "";
                order.GiftCode = "";
                // luu order
                dbContext.Orders.Add(order);
                dbContext.SaveChanges();
                for (int i = 0; i < cart.lineCollection.Count; i++)
                {
                    OrderDetail od = new OrderDetail();
                    od.BookID = cart.lineCollection[i].Book.BookID;
                    od.OrderID = order.OrderID;
                    od.Quantity = cart.lineCollection[i].Quantity;
                    od.Price = cart.lineCollection[i].Book.Price;
                    dbContext.OrderDetails.Add(od);
                }
                dbContext.SaveChanges();
                oid= order.OrderID;
            }

            #endregion
            // xoa gio hang
            cart.Clear();
            Session["cart"] = cart;

            TempData["mess"] = "Xử lý";

            return RedirectToAction("Invoice", "Order", new { id = oid});
        }
        public ActionResult UpdateInfo(FormCollection form)
        {
            string fullname = form["full-name"];
            string email = form["user-email"];
            string phone = form["phone"];
            string address = form["address"];
            string district = form["district"];

            if (HttpContext.Session["username"] == null)
            {
                return View("Error");
            }
            using (var dbContext = new CocBookEntities())
            {
                string username = (string)HttpContext.Session["username"];

                Customer cus = (from c in dbContext.Customers
                                where c.Username == username
                                select c).Single();
                cus.Fullname = fullname;
                cus.Phone = phone;
                cus.Email = email;
                cus.District = district;
                cus.Street = address;
                cus.City = "HCM";
                dbContext.SaveChanges();

                UserProfile ups = new UserProfile();
                ups.CusInfo = cus;
                TempData["InfoMess"] = "Thông tin tài khoản đã được cập nhật...";

                return RedirectToAction("Profile", "Customer");

            }
        }
        public ActionResult Register(FormCollection form)
        {
            string username = form["user-name"];
            string password = form["pass-word"];
            string fullname = form["full-name"];
            string email = form["user-email"];
            string phone = form["phone"];
            string address = form["address"];
            string district = form["district"];

            Account newAcc = new Account();
            newAcc.Active = true;
            newAcc.Username = username;
            newAcc.Password = password;
            newAcc.RoleID = 1;

            Customer newCus = new Customer();
            newCus.Username = username;
            newCus.Fullname = fullname;
            newCus.Phone = phone;
            newCus.Email = email;
            newCus.District = district;
            newCus.Street = address;
            newCus.City = "HCM";
            newCus.Point = 0;

            using (var dbContext = new CocBookEntities())
            {
                dbContext.Accounts.Add(newAcc);
                dbContext.Customers.Add(newCus);
                dbContext.SaveChanges();
            }

            FormsAuthentication.SetAuthCookie(username, false);
            HttpContext.Session.Add("username", username);

            return RedirectToAction("Profile");
        }
        public ActionResult Search(FormCollection form)
        {
            int page = 1;
            string str = form["searchkey"];
            if (str.Trim() == "")
            {
                return RedirectToAction("Index");
            }
            string type = form["searchtype"] == "" ? "1" : form["searchtype"];
            PagingDisplay pagingView = new PagingDisplay();
            using (var DbContext = new CocBookEntities())
            {
                SearchHistory history = new SearchHistory();
                history.SearchValue = str + ";"+type ;
                history.Username = getUser();

                pagingView.CateList = (from c in DbContext.Categories
                                       where c.Active == true
                                       orderby c.Position
                                       select c).ToList();
                List<V_Book> books = null;

                if (type.Equals("1"))
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.Name.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();
                }
                else
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.AuthorName.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();

                }
                // store history of search
                history.HitCount = books.Count;
                history.CreatedDate = DateTime.Now;
                DbContext.SearchHistories.Add(history);
                DbContext.SaveChanges();

                // data mining search history
                string strS = str + ";" + type ;
                var extend = (from c in DbContext.SearchHistories
                              where c.SearchValue.Contains(str) && c.SearchValue.Contains(type) &&c.SearchValue!= strS && c.HitCount < books.Count
                              orderby c.HitCount descending
                              select c).Take(1).SingleOrDefault();
                int eid = 0;
                string extendSearch="";
                string extendType = "";
                if (extend != null)
                {
                    eid = extend.AutoID;
                    extendSearch = extend.SearchValue.ToString().Split(';')[0];
                    extendType = extend.SearchValue.ToString().Split(';')[1];
                }

                // display
                pagingView.TotalItem = books.Count;
                pagingView.ItemsPerPage = PageSize;
                pagingView.CurrentPage = page;
                pagingView.TotalPage = (int)Math.Ceiling((decimal)pagingView.TotalItem / pagingView.ItemsPerPage);
                pagingView.BookList = books.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                ViewBag.SearchKey = str;
                ViewBag.SearchType = type;
                ViewBag.ExtendSearch = extendSearch;
                ViewBag.ExtendType = extendType;
                ViewBag.eid = eid;
            }
            return View(pagingView);
        }