public ActionResult UserAddress(int id)
        {
            BuyerAddressClient buyerAddressClient = new BuyerAddressClient();

            ViewBag.Address = buyerAddressClient.findByAddressID(id);
            return(PartialView());
        }
        public ActionResult Address()
        {
            BuyerAddressClient add = new BuyerAddressClient();

            ViewBag.listAddresses = add.find(Convert.ToInt32(Session["userID"]));

            return(View());
        }
        public ActionResult Delete(int id)
        {
            BuyerAddressClient address = new BuyerAddressClient();

            address.Delete(id);
            ViewBag.listAddresses = address.find(Convert.ToInt32(Session["userID"]));
            return(PartialView("PartialAddress"));
        }
        public ActionResult GetAddress()
        {
            BuyerAddressClient BAC = new BuyerAddressClient();

            ViewBag.Address        = BAC.find(Convert.ToInt32(Session["userID"]));
            Session["tempAddress"] = Session["Address_ID"];
            string a = Session["tempAddress"].ToString();

            return(PartialView("PartialAddressCheckout"));
        }
        public ActionResult RadioButton(int id_address)
        {
            BuyerAddressClient address = new BuyerAddressClient();

            ViewBag.Address        = address.find(Convert.ToInt32(Session["userID"]));
            Session["tempAddress"] = id_address;
            var a = Session["tempAddress"];

            return(PartialView("PartialAddressCheckout"));
        }
        public ActionResult FacebookCallback(string code)
        {
            var     fb     = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id     = ConfigurationManager.AppSettings["FbAppID"],
                client_secret = ConfigurationManager.AppSettings["FbAppSecret"],
                redirect_uri  = RedirectUri.AbsoluteUri,
                code          = code
            });

            var accessToken = result.access_token;

            if (!string.IsNullOrEmpty(accessToken))
            {
                fb.AccessToken = accessToken;
                dynamic me    = fb.Get("me?fields=first_name, middle_name,last_name,id,email");
                string  email = me.email;
                //string username = me.email;
                //string name = me.first_name;
                try
                {
                    db.sp_InsUserFb(email);
                    db.SaveChanges();
                    var user = db.Users.Where(x => x.Email == email).FirstOrDefault();
                    if (user != null)
                    {
                        Session["userID"]   = user.Id;
                        Session["username"] = user.Email;
                        if (user.Avatar == null)
                        {
                            Session["Avatar"] = "#.png";
                        }
                        BuyerAddressClient buyerAddressClient = new BuyerAddressClient();
                        var addressList = buyerAddressClient.find(Convert.ToInt32(Session["userID"]));
                        Session["Address_ID"] = addressList.Where(x => x.default_address == 1).Select(x => x.Address_ID).FirstOrDefault();

                        return(RedirectToAction("Index", "MainPage", new { Area = "Buyer" }));
                    }
                }
                catch
                {
                    Response.Write("<script>alert('Invalid Email or Password')</script>");
                    return(View("Error"));
                }
            }
            else
            {
                Response.Write("<script>alert('Invalid Email or Password')</script>");
                return(View("Error"));
            }

            Response.Write("<script>alert('Invalid Email or Password')</script>");
            return(View("Error"));
        }
        public ActionResult GetUserAddress()
        {
            BuyerAddressClient buyerAddressClient = new BuyerAddressClient();

            var addressList = buyerAddressClient.find(Convert.ToInt32(Session["userID"]));

            ViewBag.Address = addressList;
            string a = Session["Address_ID"].ToString();

            return(PartialView("PartialAddressCheckout1"));
        }
        public ActionResult CompleteChangeAddress()
        {
            BuyerAddressClient address = new BuyerAddressClient();

            ViewBag.Address = address.find(Convert.ToInt32(Session["userID"]));

            Session["Address_ID"] = Session["tempAddress"];
            var a = Session["Address_ID"];

            return(PartialView("PartialAddressCheckout1"));
        }
        public ActionResult Create(Address add)
        {
            BuyerAddressClient address = new BuyerAddressClient();

            add.User_ID         = Convert.ToInt32(Session["userID"]);
            add.default_address = 0;
            address.Create(add);
            ViewBag.Address = address.find(Convert.ToInt32(Session["userID"]));

            return(PartialView("PartialAddressCheckout"));
        }
        public ActionResult Edit(Address add)
        {
            BuyerAddressClient address = new BuyerAddressClient();

            add.User_ID = Convert.ToInt32(Session["userID"]);
            if (add.default_address == 1)
            {
                Session["Address_ID"] = add.Address_ID;
            }

            address.Edit(add);
            ViewBag.listAddresses = address.find(Convert.ToInt32(Session["userID"]));
            return(PartialView("PartialAddress"));
        }
        // GET: Buyer/CartCheckout
        public ActionResult Index()
        {
            Session["payment"] = "cod";
            UserCartClient UC         = new UserCartClient();
            var            cart_Items = UC.LoadCart(Convert.ToInt32(Session["userID"]));
            // get shop_id of item in cart
            List <int> shop_IDs = new List <int>();

            foreach (UserCart item in cart_Items)
            {
                if (!shop_IDs.Any(id => id == item.Store_ID))
                {
                    shop_IDs.Add(item.Store_ID);
                }
            }
            ViewBag.Shops = shop_IDs;
            ViewBag.Items = cart_Items;

            BuyerAddressClient buyerAddressClient = new BuyerAddressClient();
            var addressList = buyerAddressClient.find(Convert.ToInt32(Session["userID"]));

            ViewBag.Address = addressList;
            return(View());
        }
        [HttpPost]// thuc hien dang nhap
        public ActionResult VerifyLogin(User user)
        {
            // kiem tra du lieu nhap
            if (ModelState.IsValid)
            {
                string checkRemember = Request["checkMe"];
                // truy van csdl
                using (var _context = new DB_A6A231_DAQLTMDTEntities())
                {
                    // query id tu email va password de kiem tra dang nhap
                    //var obj = (from u in _context.Users where u.Email == user.Email && u.Password == user.Password select u).FirstOrDefault();
                    var obj = db.sp_Login(user.Email, user.Password).FirstOrDefault();
                    if (obj != null)
                    {
                        if (checkRemember == "1") //(check Remember me
                        {
                            Response.Cookies["email"].Value   = user.Email;
                            Response.Cookies["pass"].Value    = user.Password;
                            Response.Cookies["email"].Expires = DateTime.Now.AddMinutes(1);
                            Response.Cookies["pass"].Expires  = DateTime.Now.AddMinutes(1);
                        }
                        else if (checkRemember == null)
                        {
                            Response.Cookies["email"].Expires = DateTime.Now.AddMinutes(-1);
                            Response.Cookies["pass"].Expires  = DateTime.Now.AddMinutes(-1);
                        }

                        Session["userID"]   = obj.Id.ToString();
                        Session["username"] = obj.Username.ToString();
                        Session["roleID"]   = obj.RoleID.ToString();

                        //Lay mat khau người dùng
                        Session["pass"] = user.Password.ToString();

                        if (obj.Avatar != null)
                        {
                            Session["Avatar"] = obj.Avatar.ToString();
                        }
                        else
                        {
                            Session["Avatar"] = "#.png";
                        }

                        //Lay dia chi mac dinh cua nguoi dung de lam dia chỉ mua hang
                        BuyerAddressClient buyerAddressClient = new BuyerAddressClient();
                        var addressList = buyerAddressClient.find(Convert.ToInt32(Session["userID"]));
                        int addressID   = addressList.Where(x => x.default_address == 1).Select(x => x.Address_ID).FirstOrDefault();
                        Session["Address_ID"] = addressID;
                        if (Convert.ToInt32(Session["Address_ID"]) == 0)
                        {
                            Session["Address_ID"] = -1;
                        }

                        //string username = obj.Username.ToString();
                        return(RedirectToAction("Index", "MainPage", new { Area = "Buyer" }));
                    }
                    else
                    {
                        Response.Write("<script>alert('Invalid Email or Password')</script>");
                        return(View("Error"));
                    }
                }
            }
            return(View("Error"));
        }