/// <summary>
 /// Deprecated Method for adding a new object to the Payments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPayments(Payment payment)
 {
     base.AddObject("Payments", payment);
 }
        public MembershipCreateStatus pCreateUser(string FirstName, string LastName, string Username, string Password, string Email, string Phone)
        {
            if (String.IsNullOrEmpty(FirstName)) throw new ArgumentException("Value cannot be null or empty.", "FirstName");
            if (String.IsNullOrEmpty(LastName)) throw new ArgumentException("Value cannot be null or empty.", "LastName");
            if (String.IsNullOrEmpty(Username)) throw new ArgumentException("Value cannot be null or empty.", "UserName");
            if (String.IsNullOrEmpty(Email)) throw new ArgumentException("Value cannot be null or empty.", "Email");
            if (String.IsNullOrEmpty(Password)) throw new ArgumentException("Value cannot be null or empty.", "Password");

            MembershipCreateStatus status;

            var checkUser = from m in StoreDb.Webmasters
                            where (m.Username == Username)
                            select m;
            if (checkUser.Count() != 0)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return status;
            }
            var checkEmail =  from m in StoreDb.Webmasters
                              where(m.Email == Email)
                              select m;
            if (checkEmail.Count() != 0)
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return status;
            }

            Webmaster web = new Webmaster();
            web.Username = Username;
            web.FirstName = FirstName;
            web.LastName = LastName;
            web.Password = Password;
            web.Phone = Phone;
            web.Email = Email;
            web.Status = 0;
            web.DateJoin = DateTime.Now;
            web.AccountType = 0; //0 la personal account  1 la bussiness account

            string hash = MPHash.hash(Username+Password);
            web.VerifyCode = hash;

            StoreDb.Webmasters.AddObject(web);
            StoreDb.SaveChanges();
            status = MembershipCreateStatus.Success;
            
            //Tao bang Earning tuong ung
            var checknow = StoreDb.Webmasters.Single(m=>m.Username == Username);

            Earning e = new Earning();
            e.Amount = 0;
            e.Currency = "VND";
            e.WebmasterId = checknow.Id;
            e.Status = 0;//keep money
            StoreDb.Earnings.AddObject(e);
            StoreDb.SaveChanges();
            //tao bang Payment tuong ung
            Payment p = new Payment();
            p.Address = "";
            p.Amount = 0;
            p.City = "";
            p.DatePayment = null;
            p.Email = "";
            p.LastPayment = null;
            p.Name = "";
            p.Phone = "";
            p.TypePayment = 0;
            p.Ward = "";
            p.WebmasterId = checknow.Id;
            StoreDb.Payments.AddObject(p);
            StoreDb.SaveChanges();


            //tao Role cho user nay
            string[] webmaster = new string[] { "Webmaster"};
            string[] user = new string[] {Username };
            MyRoleProvider role = new MyRoleProvider();
            role.AddUsersToRoles(user,webmaster);

            //tao Setting table cho webmaster nay
            int webmt = StoreDb.Webmasters.Single(w => w.Username == Username).Id;
            Setting set = new Setting();
            set.WebmasterId = webmt;
            set.Language = "EN";//default is EN
            StoreDb.Settings.AddObject(set);
            StoreDb.SaveChanges();

            return status;

 
        }
 /// <summary>
 /// Create a new Payment object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="webmasterId">Initial value of the WebmasterId property.</param>
 /// <param name="amount">Initial value of the Amount property.</param>
 /// <param name="address">Initial value of the Address property.</param>
 /// <param name="ward">Initial value of the Ward property.</param>
 /// <param name="city">Initial value of the City property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 public static Payment CreatePayment(global::System.Int32 id, global::System.Int32 webmasterId, global::System.Int32 amount, global::System.String address, global::System.String ward, global::System.String city, global::System.String email)
 {
     Payment payment = new Payment();
     payment.Id = id;
     payment.WebmasterId = webmasterId;
     payment.Amount = amount;
     payment.Address = address;
     payment.Ward = ward;
     payment.City = city;
     payment.Email = email;
     return payment;
 }
        public ActionResult setPayment(FormCollection form)
        {
            var checkit = StoreDb.Webmasters.Single(m=>m.Username == User.Identity.Name);
            string bank = "";
            string atm = "";
            try
            {
                // TRUONG HOP DA SET RUI THI UPDATE
                var pay = StoreDb.Payments.Single(m => m.WebmasterId == checkit.Id );

                foreach (var key in form.AllKeys)
                {
                    if (key == "Name")
                        pay.Name = form[key];
                    else if (key == "Email")
                        pay.Email = form[key];
                    else if (key == "Phone")
                        pay.Phone = form[key];
                    else if (key == "City")
                        pay.City = form[key];
                    else if (key == "Ward")
                        pay.Ward = form[key];
                    else if (key == "Address")
                        pay.Address = form[key];

                    else if (key == "type")
                    {
                        pay.TypePayment = int.Parse(form[key]);
                    }
                    else if (key == "Bank")
                        bank = form[key];
                    else if (key == "ATM")
                        atm = form[key];
                    else if (key == "ZipCode")
                        pay.ZipCode = form[key];
                    else if (key == "Country")
                        pay.Country = form[key];


                }
                var webmaster = StoreDb.Webmasters.Single(w=>w.Username == User.Identity.Name);
                webmaster.ATM = atm;
                webmaster.Bank = bank;

                StoreDb.SaveChanges();
                TransactionLogViewModel.AddLog(User.Identity.Name + " has just save/update the setPaymen successfullyt!", DateTime.Now);
                return View("SetPaymentDone");

            }
            catch (Exception e)
            {
                Payment p = new Payment();

                foreach (var key in form.AllKeys)
                {
                    if (key == "Name")
                        p.Name = form[key];
                    else if (key == "Email")
                        p.Email = form[key];
                    else if (key == "Phone")
                        p.Phone = form[key];
                    else if (key == "City")
                        p.City = form[key];
                    else if (key == "Ward")
                        p.Ward = form[key];
                    else if (key == "Address")
                        p.Address = form[key];

                    else if (key == "type")
                    {
                        p.TypePayment = int.Parse(form[key]);
                    }
                    else if (key == "Bank")
                        bank = form[key];
                    else if (key == "ATM")
                        atm = form[key];
                    else if (key == "ZipCode")
                        p.ZipCode = form[key];
                    else if (key == "Country")
                        p.Country = form[key];

                }
                p.WebmasterId = checkit.Id;

                var webmaster = StoreDb.Webmasters.Single(w => w.Username == User.Identity.Name);
                webmaster.ATM = atm;
                webmaster.Bank = bank;


                StoreDb.Payments.AddObject(p);
                StoreDb.SaveChanges();
                TransactionLogViewModel.AddLog(User.Identity.Name + " has just save/update the setPaymen successfully!", DateTime.Now);
                return View("SetPaymentDone");

            }
     
        }
        public ActionResult setPayment()
        {
            var country = StoreDb.Parameters.Single(s=>s.Name == "Country");

            try {
                var pay = StoreDb.Payments.Single(m => m.Webmaster.Username == User.Identity.Name);
                TransactionLogViewModel.AddLog(User.Identity.Name + " has just save/update the setPayment!", DateTime.Now);
                ViewData["ATM"] = pay.Webmaster.ATM;
                ViewData["Bank"] = pay.Webmaster.Bank;
                ViewData["Country"] = country.Value;
                return View(pay);
                
            }
            catch(Exception e){
                Payment p = new Payment();
                TransactionLogViewModel.AddLog(User.Identity.Name + " has just save/update the setPayment!", DateTime.Now);
                ViewData["ATM"] = "";
                ViewData["Bank"] = "";
                ViewData["Country"] = country.Value;
                return View(p);
            };
            
            

        }
        public ActionResult setPayment(FormCollection form)
        {
            var checkit = StoreDb.Webmasters.Single(m=>m.Username == User.Identity.Name);

            try
            {
                // TRUONG HOP DA SET RUI THI UPDATE
                var pay = StoreDb.Payments.Single(m => m.WebmasterId == checkit.Id );

                foreach (var key in form.AllKeys)
                {
                    if (key == "Name")
                        pay.Name = form[key];
                    if (key == "Email")
                        pay.Email = form[key];
                    if (key == "Phone")
                        pay.Phone = form[key];
                    if (key == "City")
                        pay.City = form[key];
                    if (key == "Ward")
                        pay.Ward = form[key];
                    if (key == "Address")
                        pay.Address = form[key];

                    if (key == "type")
                    {
                        pay.TypePayment = int.Parse(form[key]);
                    }
                }
                StoreDb.SaveChanges();
                return RedirectToAction("setpayment");

            }
            catch (Exception e)
            {
                Payment p = new Payment();

                foreach (var key in form.AllKeys)
                {
                    if (key == "Name")
                        p.Name = form[key];
                    if (key == "Email")
                        p.Email = form[key];
                    if (key == "Phone")
                        p.Phone = form[key];
                    if (key == "City")
                        p.City = form[key];
                    if (key == "Ward")
                        p.Ward = form[key];
                    if (key == "Address")
                        p.Address = form[key];

                    if (key == "type")
                    {
                        p.TypePayment = int.Parse(form[key]);
                    }
                }
                p.WebmasterId = checkit.Id;

                StoreDb.Payments.AddObject(p);
                StoreDb.SaveChanges();
                return RedirectToAction("setpayment");

            }
     
        }
        public ActionResult setPayment()
        {
            try {
                var pay = StoreDb.Payments.Single(m => m.Webmaster.Username == User.Identity.Name);
                return View(pay);
                
            }
            catch(Exception e){
                Payment p = new Payment();
                return View(p);
            };
            
            

        }