// Criar um novo empregado
        public IActionResult Create()
        {
            var model = new RegisterNewEmployeeViewModel
            {
                Departments = GetDepartments(),
                Countries   = _countryRepository.GetComboCountries(),
                Cities      = _countryRepository.GetComboCities(0)
            };

            return(View(model));
        }
        public async Task <IActionResult> Create(RegisterNewEmployeeViewModel model)
        {
            var user = await _userHelper.GetUserByEmailAsync(model.Username);

            if (user == null)
            {
                var city = await _countryRepository.GetCityAsync(model.CityId);

                user = new User
                {
                    FirstName            = model.FirstName,
                    LastName             = model.LastName,
                    Email                = model.Username,
                    UserName             = model.Username,
                    Address              = model.Address,
                    PhoneNumber          = model.PhoneNumber,
                    TaxNumber            = model.TaxNumber,
                    SocialSecurityNumber = model.SocialSecurityNumber,
                    CityId               = model.CityId,
                    City     = city,
                    isActive = true,
                };

                try
                {
                    var result = await _userHelper.AddUserAsync(user, "123456");
                }
                catch (Exception)
                {
                    model.Departments = GetDepartments();
                    model.Countries   = _countryRepository.GetComboCountries();
                    model.Cities      = _countryRepository.GetComboCities(model.CityId);
                    this.ModelState.AddModelError(string.Empty, "The user couldn't be created. Please, confirm data");
                    return(this.View(model));
                }

                try
                {
                    // Atribuir o role de Employee ao user
                    await _userHelper.AddUserToRoleAsync(user, "Employee");
                }
                catch (Exception)
                {
                    model.Departments = GetDepartments();
                    model.Countries   = _countryRepository.GetComboCountries();
                    model.Cities      = _countryRepository.GetComboCities(model.CityId);
                    this.ModelState.AddModelError(string.Empty, "Error adding the employee to the role! Please contact the technical support!");

                    return(this.View(model));
                }


                try
                {
                    var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                    await _userHelper.ConfirmEmailAsync(user, myToken); // Confirmar automaticamente
                }
                catch (Exception)
                {
                    model.Departments = GetDepartments();
                    model.Countries   = _countryRepository.GetComboCountries();
                    model.Cities      = _countryRepository.GetComboCities(model.CityId);
                    this.ModelState.AddModelError(string.Empty, "Error on the email confirmation! Please, contact the technical suppoprt! ");

                    return(this.View(model));
                }

                try
                {
                    // Adicionar o user ao departamento (tabela de detalhes de departamento)
                    // Obter o departamento
                    var department = await _context.Departments.FindAsync(model.DepartmentId);

                    _context.DepartmentDetails.Add(new DepartmentDetail
                    {
                        User       = user,
                        StartDate  = DateTime.Today,
                        Department = department
                    });

                    await _context.SaveChangesAsync();
                }
                catch (Exception)
                {
                    model.Departments = GetDepartments();
                    model.Countries   = _countryRepository.GetComboCountries();
                    model.Cities      = _countryRepository.GetComboCities(model.CityId);
                    this.ModelState.AddModelError(string.Empty, "Error! The department details wasn't updated. Please contact support! ");

                    return(this.View(model));
                }


                try
                {
                    // Criar um link que vai levar lá dentro uma acção. Quando o utilizador carregar neste link,
                    // vai no controlador Account executar a action "ChangePassword"
                    // Este ConfirmEmail vai receber um objecto novo que terá um userid e um token.

                    var myTokenReset = await _userHelper.GeneratePasswordResetTokenAsync(user);

                    var link = this.Url.Action(
                        "ResetPassword",
                        "Employees",
                        new { token = myTokenReset }, protocol: HttpContext.Request.Scheme);

                    _mailHelper.SendMail(user.Email, "Airline Password Reset", $"<h1>Airline Password Reset</h1>" +
                                         $"Welcome onboard! Please, reset your password, click in this link:</br></br>" +
                                         $"<a href = \"{link}\">Reset Password</a>");

                    ViewBag.Message = "The employee was created with sucess! Was sent an email to the employee for the password reset!";
                    return(View());
                }
                catch (Exception)
                {
                    model.Departments = GetDepartments();
                    model.Countries   = _countryRepository.GetComboCountries();
                    model.Cities      = _countryRepository.GetComboCities(model.CityId);
                    this.ModelState.AddModelError(string.Empty, "Error on seeding the email to the employee! Please contact support!");

                    return(RedirectToAction(nameof(Index)));
                }
            }

            this.ModelState.AddModelError(string.Empty, "The user already exists");
            return(View(model));
        }