using EgwCoreLib.Razor; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.Services; using WebDoorCreator.UI.Data; namespace WebDoorCreator.UI.Pages { [Authorize(Roles = "SuperAdmin, DcaAdmin")] public partial class SuperAdmin { #region Protected Fields protected string _address = ""; protected string _city = ""; protected string _companyExtCode = ""; protected int _companyId = 0; protected string _companyName = ""; protected string _companyToken = ""; protected string _defaultPath = ""; protected string _email = ""; protected string _password = ""; protected string _passwordConfirm = ""; protected string _privateNote = ""; protected string _state = ""; protected string _vat = ""; protected int _zipCode = 0; protected PasswordHasher hasher = new PasswordHasher(); #endregion Protected Fields #region Protected Properties [Inject] protected IEmailSender _emailSender { get; set; } = null!; [Inject] protected UserManager _userManager { get; set; } = null!; protected string address { get => _address; set { _address = value; } } protected string city { get => _city; set { _city = value; } } protected string companyExtCode { get => _companyExtCode; set { _companyExtCode = value; } } protected int companyId { get => _companyId; set { _companyId = value; } } protected string companyName { get => _companyName; set { _companyName = value; } } protected string companyToken { get => _companyToken; set { _companyToken = value; } } protected CompanyModel? currCompany { get; set; } = null; protected AspNetUsers? currUser { get; set; } = null; protected string defaultPath { get; set; } = "R:\\EgtData\\Doors\\EgtCompoBase\\Compo"; protected string email { get => _email; set { _email = value; } } protected bool isCompShow { get; set; } = true; [Inject] protected NavigationManager NavManager { get; set; } = null!; protected string password { get => _password; set { _password = value; } } protected string passwordConfirm { get => _passwordConfirm; set { _passwordConfirm = value; } } protected string privateNote { get => _privateNote; set { _privateNote = value; } } protected string selectedComp { get { string answ = ""; answ = isCompShow ? "tabActive" : "tab"; return answ; } } protected string selectedUser { get { string answ = ""; answ = !isCompShow ? "tabActive" : "tab"; return answ; } } protected string state { get => _state; set { _state = value; } } protected string vat { get => _vat; set { _vat = value; } } [Inject] protected WebDoorCreatorService WDService { get; set; } = null!; protected int zipCode { get => _zipCode; set { _zipCode = value; } } protected int totalCount { get; set; } protected void ForceReload(int newNum) { numRecord = newNum; currPage = 1; } protected void ForceReloadPage(int newNum) { currPage = newNum; } protected void UpdateTotCount(int newTotCount) { totalCount = newTotCount; } protected CompoSelectFilter currFilter { get; set; } = new CompoSelectFilter(); #endregion Protected Properties private int currPage { get => currFilter.CurrPage; set => currFilter.CurrPage = value; } private bool isLoading { get; set; } = false; private int numRecord { get => currFilter.NumRec; set => currFilter.NumRec = value; } #region Protected Methods protected DataPager? pagerCompoFiles = null!; protected async Task addModNewCompany() { Guid newCompToken = Guid.NewGuid(); bool done = false; if (currCompany == null) { var newComp = new CompanyModel() { CompanyExtCode = companyExtCode, CompanyName = companyName, Address = address, ZipCode = zipCode, City = city, State = state, VAT = vat, PrivateNote = privateNote, CompanyToken = newCompToken.ToString() }; done = await WDService.CompanyAddMod(newComp); } else { var editComp = new CompanyModel() { CompanyId = companyId, CompanyExtCode = companyExtCode, CompanyName = companyName, Address = address, ZipCode = zipCode, City = city, State = state, VAT = vat, PrivateNote = privateNote, CompanyToken = companyToken }; done = await WDService.CompanyAddMod(editComp); } if (done) { NavManager.NavigateTo(NavManager.Uri, true); } } protected async Task clearFields() { await Task.Delay(1); companyExtCode = ""; companyName = ""; address = ""; city = ""; zipCode = 0; state = ""; vat = ""; privateNote = ""; companyToken = ""; } protected async Task addModNewUser() { Guid newUserID = Guid.NewGuid(); bool done = false; if (currUser == null) { //var user = new IdentityUser { UserName = email, Email = email }; //var result = await _userManager.CreateAsync(user, password); //if (result.Succeeded) //{ // await _userManager.AddToRoleAsync(user, "User"); // var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); // code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); // var callbackUrl = // $"/Account/ConfirmEmail?UserName{user.Id}&code{code}&returnUrl=~/"; await // _emailSender.SendEmailAsync(email, "Web Door Creator: Please confirm your email // account", $"Click here to // confirm the account linked with this email."); // NavManager.NavigateTo(NavManager.Uri, true); //} AspNetUsers IU = new AspNetUsers() { Id = newUserID.ToString(), UserName = email, NormalizedUserName = email.ToUpper(), Email = email, NormalizedEmail = email.ToUpper(), EmailConfirmed = true, PasswordHash = hasher.HashPassword(null!, password), LockoutEnabled = false, AccessFailedCount = 0 }; done = await WDService.UserAddMod(IU); } else { #if false var editComp = new CompanyModel() { CompanyId = companyId, CompanyExtCode = companyExtCode, CompanyName = companyName, Address = address, ZipCode = zipCode, City = city, State = state, VAT = vat, PrivateNote = privateNote, CompanyToken = companyToken }; done = await WDService.CompanyAddMod(editComp); #endif } if (done) { NavManager.NavigateTo(NavManager.Uri, true); } } protected async Task catchCurrComp(CompanyModel? selComp) { await Task.Delay(1); if (selComp != null) { currCompany = selComp; companyId = currCompany.CompanyId; companyExtCode = currCompany.CompanyExtCode; companyName = currCompany.CompanyName; address = currCompany.Address; zipCode = currCompany.ZipCode; city = currCompany.City; state = currCompany.State; vat = currCompany.VAT; privateNote = currCompany.PrivateNote; companyToken = currCompany.CompanyToken; } } protected async Task catchCurrUser(AspNetUsers? selUser) { await Task.Delay(1); if (selUser != null) { currUser = selUser; email = currUser.Email; } } protected async Task doShowComp() { await Task.Delay(1); isCompShow = true; } protected async Task doShowUser() { await Task.Delay(1); isCompShow = false; } protected async Task refreshFiles() { await Task.Delay(1); //await WDService.ListValuesLuaNgeInsert(@$"{defaultPath}"); } protected async Task refreshHw() { await Task.Delay(1); await WDService.CompoListSetAll(@$"{defaultPath}"); } #endregion Protected Methods protected AdminMode CurrMode { get; set; } = AdminMode.Company; protected string cssMode(AdminMode modeReq) { return modeReq.Equals(CurrMode) ? "btn-primary active" : "btn-outline-primary"; } protected void SetMode(AdminMode modeReq) { CurrMode = modeReq; } #if false protected async Task refreshVoc() { await Task.Delay(1); await WDService.VocLemmaInsert(@$"{defaultPath}"); } #endif } public enum AdminMode { None, Company, Interface, Component } }