public async Task <IActionResult> Create([Bind("ID,name,phone,email,pass")] Sale sale)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sale);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(sale));
        }
        public async Task <IActionResult> Create([Bind("Customer,Gender,Address,Phone,Email,Job,WorkPlace,Cmnd,Day,Place,Money,Purpose,Requires,Price,Details,DType,Cash,NCH1,NCH2,NCH21,NCH3,NMS,NS,NSHH,NSH,HKTT,password,Contract, NSH1, PlanId, supporter, IdType, IsForeigner, View, Direction, Acreage, Floor")] Appoinment appoinment, List <IFormFile> files)
        {
            //if (ModelState.IsValid)
            //{
            var contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Phone == appoinment.Phone);

            if (contact != null)
            {
                contact.Appoinment = appoinment;
                _context.Update(contact);
            }
            var curUser = await _userManager.GetUserAsync(User);

            var authorized = await IsAuthorized(curUser, "Contracts", "Create");

            if (!authorized || curUser.EmailConfirmed == false)
            {
                return(View("AccessDenied"));
            }
            if (curUser != null)
            {
                var roles = await _userManager.GetRolesAsync(curUser);

                if (roles.Contains("Collaborator"))
                {
                    if (contact != null)
                    {
                        contact.Charges = appoinment.supporter == true ? 1.0 : 1.3;
                        _context.Update(contact);
                    }
                }
            }
            PasswordHasher <Sale>      hasher = new PasswordHasher <Sale>();
            PasswordVerificationResult result = hasher.VerifyHashedPassword(curUser, curUser.PasswordHash, appoinment.password);

            if (result == PasswordVerificationResult.Failed)
            {
                TempData["StatusMessage"] = "Sai mật khẩu xác nhận";
                return(RedirectToAction("Create"));
            }
            appoinment.password    = hasher.HashPassword(curUser, appoinment.password);
            appoinment.SaleDetails = curUser.Info;
            appoinment.Sale        = curUser;
            appoinment.SEmail      = curUser.Email;
            ViewBag.plan           = await _context.Plans.ToListAsync();

            string str = "";

            if (appoinment.Sale == null)
            {
                TempData["StatusMessage"] = "Bạn chưa đăng nhập vào hệ thống";
                return(RedirectToAction("Create"));
            }
            else
            {
                //var recaptcha = await _recaptcha.Validate(Request);
                //if (!recaptcha.success)
                //{
                //    ModelState.AddModelError("Recaptcha", "There was an error validating recatpcha. Please try again!");
                //    return View();
                //}
                List <Appoinment> appoinments = await _context.appoinment.ToListAsync();

                if (appoinments.Count > 0)
                {
                    appoinment.Contract = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.Contract) + 1;
                    if (appoinment.NCH1 > 0 || appoinment.NCH2 > 0 || appoinment.NCH3 > 0 || appoinment.NCH21 > 0)
                    {
                        appoinment.ph = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.ph) + appoinment.NCH2 + appoinment.NCH1 + appoinment.NCH3 + appoinment.NCH21;
                        str          += "Căn hộ: " + Convert.ToString(appoinment.ph) + "\n";
                    }
                    if (appoinment.NSH > 0)
                    {
                        appoinment.psh = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.psh) + appoinment.NSH;
                        str           += "Biệt thự ĐL: " + Convert.ToString(appoinment.psh) + "\n";
                    }
                    if (appoinment.NSH1 > 0)
                    {
                        appoinment.psh1 = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.psh1) + appoinment.NSH1;
                        str            += "Biệt thự SL: " + Convert.ToString(appoinment.psh1) + "\n";
                    }
                    if (appoinment.NSHH > 0)
                    {
                        appoinment.pshh = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.pshh) + appoinment.NSHH;
                        str            += "Shophouse: " + Convert.ToString(appoinment.pshh) + "\n";
                    }
                    if (appoinment.NMS > 0)
                    {
                        appoinment.pms = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.pms) + appoinment.NMS;
                        str           += "Biệt thự: " + Convert.ToString(appoinment.pms) + "\n";
                    }
                    if (appoinment.NS > 0)
                    {
                        appoinment.pns = _context.appoinment.Where(a => a.IsActive == true).Max(c => c.pns) + appoinment.NS;
                        str           += "Shop (kios chung cư): " + Convert.ToString(appoinment.pns) + "\n";
                    }
                }
                //appoinment.supporter = false;
                appoinment.New      = true;
                appoinment.Official = false;
                appoinment.cTime    = DateTime.Now.ToString("ddMMyyyy HH:mm:ss.FFFFFFF");
                appoinment.Confirm  = false;
                appoinment.IsActive = true;
                //var p = await _context.Plans.SingleOrDefaultAsync(pl => pl.Id == plan);
                //if (p == null)
                //{
                //    TempData["StatusMessage"] = "Bạn chưa chọn dự án";
                //    return View();
                //}
                var           newFileName = string.Empty;
                List <string> portrait    = new List <string>();
                if (files != null)
                {
                    var    fileName = string.Empty;
                    string PathDB   = string.Empty;

                    foreach (var file in files)
                    {
                        if (file.Length > 0)
                        {
                            //Getting FileName
                            fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

                            //Assigning Unique Filename (Guid)
                            var myUniqueFileName = Convert.ToString(Guid.NewGuid());

                            //Getting file Extension
                            var FileExtension = Path.GetExtension(fileName);

                            // concating  FileName + FileExtension
                            newFileName = myUniqueFileName + FileExtension;

                            // Combines two strings into a path.
                            fileName = Path.Combine(_environment.WebRootPath, "storage") + $@"\{newFileName}";

                            // if you want to store path of folder in database

                            portrait.Add(newFileName);

                            using (FileStream fs = System.IO.File.Create(fileName))
                            {
                                file.CopyTo(fs);
                                fs.Flush();
                            }
                        }
                    }
                    if (portrait.Count > 1)
                    {
                        appoinment.Photo = portrait[0] + " " + portrait[1];
                    }
                    else if (portrait.Count > 0)
                    {
                        appoinment.Photo = portrait[0];
                    }
                }

                _context.Add(appoinment);
                await _context.SaveChangesAsync();

                //string contents = curUser.Name + " đã tạo một HĐ mới trên hệ thống AnnSmart" + " vào lúc " + DateTime.Now.ToString("HH:mm:ss MM-dd-yyyy");
                //SendMail("HĐ đặt chỗ", new MailboxAddress("Hương Ngô", "*****@*****.**"), contents);

                TempData["ct"] = appoinment.Contract;
                TempData["pt"] = str;
                return(RedirectToAction("Confirm", new { appoinment.Id }));
            }
        }
        public async Task <IActionResult> Create([Bind("Customer,Gender,Address,Phone,Email,Job,WorkPlace,Cmnd,Day,Place,Money,Purpose,Requires,Price,Details,DType,Cash,NCH1,NCH2,NCH3,NMS,NSHH,NSH,HKTT,sale,password,Contract, Priority")] Appoinment appoinment)
        {
            //if (ModelState.IsValid)
            //{
            foreach (Sale sale in sales)
            {
                if (sale.email == appoinment.sale)
                {
                    TempData["name"] = sale.name;
                    break;
                }
            }
            if (!check(appoinment.sale, appoinment.password))
            {
                return(View(appoinment));
            }
            else
            {
                string tmp = await Escalating();

                if (tmp != "")
                {
                    string [] n = tmp.Split(" ");
                    appoinment.Contract = Convert.ToInt32(n[0]);
                    appoinment.Priority = Convert.ToInt32(n[1]);
                    ViewBag.contract    = appoinment.Contract;
                    ViewBag.priority    = appoinment.Priority;
                }
                appoinment.cTime   = DateTime.Now.ToString("ddMMyyyy HH:mm:ss.FFFFFFF");
                appoinment.Confirm = false;
                _context.Add(appoinment);
                foreach (Sale sale in sales)
                {
                    if (sale.email.Equals(appoinment.sale))
                    {
                        ViewBag.sale = sale;
                    }
                }
                Random     rd      = new Random();
                List <int> numbers = new List <int>();
                for (int i = 0; i < 6; i++)
                {
                    numbers.Add(rd.Next(100, 151));
                }
                string str = "";

                if (appoinment.NCH1 > 0)
                {
                    str += " - số thứ tự đặt chỗ Căn hộ của bạn là " + Convert.ToString(numbers[0]);
                }
                if (appoinment.NMS > 0)
                {
                    str += " - số thứ tự đặt chỗ căn Biệt thự của bạn là " + Convert.ToString(numbers[2]);
                }
                if (appoinment.NSH > 0)
                {
                    str += " - số thứ tự đặt chỗ căn Nhà phố của bạn là " + Convert.ToString(numbers[3]);
                }
                if (appoinment.NSHH > 0)
                {
                    str += " - số thứ tự đặt chỗ căn Shophouse của bạn là " + Convert.ToString(numbers[5]);
                }
                TempData["namae"] = str;
                await _context.SaveChangesAsync();

                return(View("Confirm"));
            }
        }