@namespace Oqtane.Modules.Admin.Modules
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IModuleService ModuleService
@inject IStringLocalizer<Import> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

<form @ref="form" class="@(validated ? "was-validated" : "needs-validation")" novalidate>
    <div class="container">
        <div class="row mb-1 align-items-center">
            <Label Class="col-sm-3" For="content" HelpText="Enter The Module Content To Import" ResourceKey="Content">Content: </Label>
            <div class="col-sm-9">
                <textarea id="content" class="form-control" @bind="@_content" rows="5" required></textarea>

            </div>
        </div>
    </div>

    <button type="button" class="btn btn-success" @onclick="ImportModule">@Localizer["Import"]</button>
    <NavLink class="btn btn-secondary" href="@PageState.ReturnUrl">@SharedLocalizer["Cancel"]</NavLink>
</form>

@code {
    private string _content = string.Empty;
    private ElementReference form;
    private bool validated = false;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit;
    public override string Title => "Import Content";

    private async Task ImportModule()
    {
        validated = true;
        var interop = new Interop(JSRuntime);
        if (await interop.FormValid(form))
        {
            if (_content != string.Empty)
            {
                try
                {
					bool success = await ModuleService.ImportModuleAsync(ModuleState.ModuleId, PageState.Page.PageId, _content);
                    if (success)
                    {
                        AddModuleMessage(Localizer["Success.Content.Import"], MessageType.Success);
                    }
                    else
                    {
                        AddModuleMessage(Localizer["Message.Content.ImportProblem"], MessageType.Warning);
                    }
                }
                catch (Exception ex)
                {
                    await logger.LogError(ex, "Error Importing Module {ModuleId} {Error}", ModuleState.ModuleId, ex.Message);
                    AddModuleMessage(Localizer["Error.Module.Import"], MessageType.Error);
                }
            }
            else
            {
                AddModuleMessage(Localizer["Message.Required.ImportContent"], MessageType.Warning);
            }
        }
        else
        {
            AddModuleMessage(SharedLocalizer["Message.InfoRequired"], MessageType.Warning);
        }
    }
}
