@model PluginSearchModel
@using Nop.Services.Plugins

@{
    //page title
    ViewBag.PageTitle = T("Admin.Configuration.Plugins.Local").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Local plugins");
}

@{
    const string hideSearchBlockAttributeName = "LocaLPluginsPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<form asp-controller="Plugin" asp-action="List" id="plugins-form-local">
    <div class="content-header clearfix">
        <h1 class="float-left">
            @T("Admin.Configuration.Plugins.Local")
        </h1>    
        <div class="float-right">
            @if (Model.NeedToRestart)
            {
                <button type="submit" name="plugin-apply-changes" class="btn btn-success">@T("Admin.Configuration.Plugins.ApplyChanges")</button>
                <button type="submit" name="plugin-discard-changes" class="btn btn-danger">@T("Admin.Configuration.Plugins.DiscardChanges")</button>
            }

            <button type="submit" name="plugin-reload-grid" class="btn btn-primary">@T("Admin.Configuration.Plugins.ReloadList")</button>
            <script>
                $(function() {
                    $("button[name='plugin-reload-grid']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.ReloadList.Progress").Text))');
                        });
                    $("button[name='plugin-apply-changes']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.ApplyChanges.Progress").Text))');
                        });
                    $("button[name='plugin-discard-changes']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.DiscardChanges.Progress").Text))');
                        });
                });
            </script>
            <button type="button" name="uploadplugin" class="btn bg-olive" data-toggle="modal" data-target="#uploadplugin-window">
                <i class="fas fa-upload"></i>
                @T("Admin.Configuration.Plugins.Upload")
            </button>
            @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.PluginListButtons, additionalData = Model })
        </div>
    </div>

    <section class="content">
    <div class="container-fluid">
        <div class="form-horizontal">
            <script>
                $(function() {
                    //install a plugin
                    $("#plugins-local-grid").on("click", ".install-plugin-link", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Install.Progress").Text))');
                        return true;
                    });

                    //uninstall a plugin
                    $("#plugins-local-grid").on("click", ".uninstall-plugin-link", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Uninstall.Progress").Text))');
                        return true;
                    });

                    //upload a plugin
                    $("#upload-plugin").on("click", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Upload.Progress").Text))');
                        return true;
                    });

                    //delete a plugin
                    $("#plugins-local-grid")
                        .on("click", ".delete-plugin-link", function (e) {
                            $(this)
                                .attr("data-toggle", "modal")
                                .attr("data-target", "#plugin-delete-confirmation");
                            $("#plugin-delete-confirmation button[type=submit]").attr("name", $(this).attr("name"));
                    });

                    $("#plugin-delete-confirmation button[type=submit]").on("click",
                        function() {
                            $('#plugin-delete-confirmation').modal('toggle');
                            $("html, body").animate({ scrollTop: 0 }, 400);
                            showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Delete.Progress").Text))');
                            return true;
                        });
                });
            </script>

            <div class="cards-group">
                <div class="card card-default card-search">
                    <div class="card-body">
                        <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                            <div class="search-text">@T("Admin.Common.Search")</div>
                            <div class="icon-search"><i class="fas fa-magnifying-glass" aria-hidden="true"></i></div>
                            <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                        </div>

                        <div class="search-body @(hideSearchBlock ? "closed" : "")">
                            <p>
                                <h4>@T("Admin.Configuration.Plugins.Description")</h4>
                                <ol>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step1")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step2")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step3")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step4")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step5")</li>
                                </ol>
                            </p>
                            <p>
                                @T("Admin.Configuration.Plugins.Description.DownloadMorePlugins", OfficialSite.Marketplace + Utm.OnAdminAllPlugins)
                            </p>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchFriendlyName" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchFriendlyName" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchGroup" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchGroup" asp-items="Model.AvailableGroups" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchLoadModeId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchLoadModeId" asp-items="Model.AvailableLoadModes" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchAuthor" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchAuthor" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="text-center col-12">
                                    <button type="button" id="search-plugins-local" class="btn btn-primary btn-search">
                                        <i class="fas fa-magnifying-glass"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card card-default">
                    <div class="card-body">
                        <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.Plugins", Docs.Plugins + Utm.OnAdmin)" />

                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "plugins-local-grid",
                            UrlRead = new DataUrl("ListSelect", "Plugin", null),
                            SearchButtonId = "search-plugins-local",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchFriendlyName)),
                                new FilterParameter(nameof(Model.SearchGroup)),
                                new FilterParameter(nameof(Model.SearchLoadModeId)),
                                new FilterParameter(nameof(Model.SearchAuthor))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(PluginModel.Group))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Group").Text,
                                    Width = "150"
                                },
                                new ColumnProperty(nameof(PluginModel.LogoUrl))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Logo").Text,
                                    Width = "200",
                                    ClassName = NopColumnClassDefaults.CenterAll,
                                    Render = new RenderPicture()
                                },
                                new ColumnProperty(nameof(PluginModel.Description))
                                {
                                    Title = T("Admin.Configuration.Plugins.Info").Text,
                                    Width = "400",
                                    Render = new RenderCustom("renderColumnDescription")
                                },
                                new ColumnProperty(nameof(PluginModel.FriendlyName))
                                {
                                    Title = T("Admin.Configuration.Plugins.AdditionalInfo").Text,
                                    Render = new RenderCustom("renderColumnFriendlyName")
                                },
                                new ColumnProperty(nameof(PluginModel.Installed))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Installation").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Render = new RenderCustom("renderColumnInstalled")
                                }
                            }
                        })

                        <script asp-location="Footer">
                            var textRenderer = $.fn.dataTable.render.text().display;

                            function renderColumnDescription(data, type, row, meta) {
                                var res = '<b>' + textRenderer(row.FriendlyName) + '</b>';

                                if (row.Description) {
                                    res = res + '<br />' + textRenderer(row.Description);
                                }
                                res = res + '<br />';
                                //allow configuration only when a plugin has a configuration URL specified
                                if (row.ConfigurationUrl && row.ConfigurationUrl.length > 0) {
                                    res = res + '<a class="btn btn-default margin-r-5" href="' + row.ConfigurationUrl + '"><i class="fas fa-gear"></i>@T("Admin.Configuration.Plugins.Fields.Configure").Text</a>';
                                }
                                if (row.Installed) {
                                    res = res + '<a class="btn btn-default" href="javascript:OpenWindow(\'@Url.Content("~/Admin/Plugin/EditPopup")?systemName=' + row.SystemName + '&btnId=btnRefreshList&formId=plugins-form-local\', 800, 500, true);"><i class="fas fa-pencil"></i>@T("Admin.Common.Edit").Text</a>';
                                }

                                return res;
                            }

                            function renderColumnFriendlyName(data, type, row, meta) {
                                var isInstall = '<li>@T("Admin.Configuration.Plugins.Fields.Installed").Text: ';
                                if (row.Installed) {
                                    isInstall = isInstall + '<i class="fas fa-check true-icon"></i></li>';
                                }
                                else {
                                    isInstall = isInstall + '<i class="fas fa-times false-icon"></i></li>';
                                }

                                var canChange = '';
                                if (row.CanChangeEnabled) {
                                    canChange = canChange + '<li>@T("Admin.Configuration.Plugins.Fields.IsEnabled").Text: ';
                                    if (row.IsEnabled) {
                                        canChange = canChange + '<i class="fas fa-check true-icon"></i>';
                                    }
                                    else {
                                        canChange = canChange + '<i class="fas fa-times false-icon"></i>';
                                    };
                                    canChange = canChange + '</li>';
                                };

                                return '<ul class="common-list">' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.Version").Text: ' +  row.Version + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.Author").Text: ' + row.Author + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.SystemName").Text: ' + row.SystemName + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.DisplayOrder").Text: ' + row.DisplayOrder + '</li>' +
                                    isInstall +
                                    canChange;
                            }

                            function renderColumnInstalled(data, type, row, meta) {
                                if (row.Installed) {
                                    return '<button type="submit" name="uninstall-plugin-link-' + row.SystemName + '" class="btn bg-red uninstall-plugin-link">@T("Admin.Configuration.Plugins.Fields.Uninstall").Text</button>';
                                } else {
                                    return '<button type="submit" name="install-plugin-link-' + row.SystemName + '" class="btn bg-green install-plugin-link">@T("Admin.Configuration.Plugins.Fields.Install").Text</button> <button type="button" name="delete-plugin-link-' + row.SystemName + '" class="btn bg-red delete-plugin-link" >@T("Admin.Configuration.Plugins.Fields.Delete").Text</button>';
                                }
                            }
                        </script>
                    </div>

                    <div id="plugin-delete-confirmation" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="plugin-delete-confirmation-title" style="display: none;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title" id="plugindelete-confirmation-title">@T("Admin.Common.AreYouSure")</h4>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>                                    
                                </div>
                                <div class="form-horizontal">
                                    <div class="modal-body">
                                        @T("Admin.Common.DeleteConfirmation")
                                    </div>
                                    <div class="modal-footer">
                                        <span class="btn btn-default" data-dismiss="modal">@T("Admin.Common.NoCancel")</span>
                                        <button type="submit" class="btn btn-danger float-right">@T("Admin.Common.Delete")</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div>
                        <button type="submit" id="btnRefreshList" style="display: none"></button>
                        <script>
                            $(function() {
                                $('#btnRefreshList').click(function () {
                                    //refresh grid
                                    updateTable('#plugins-local-grid');

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </section>
    @await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.PluginDetailsBlock, additionalData = Model })
</form>

@*upload plugin form*@
<div id="uploadplugin-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="uploadplugin-window-title">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="uploadplugin-window-title">@T("Admin.Configuration.Plugins.Upload")</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>                
            </div>
            <form asp-controller="Plugin" asp-action="UploadPluginsAndThemes" method="post" enctype="multipart/form-data">
                <div class="form-horizontal">
                    <div class="modal-body">
                        <ul class="common-list">
                            <li>
                                @T("Admin.Configuration.Plugins.Upload.Hint1")
                            </li>
                            <li>
                                @string.Format(T("Admin.Configuration.Plugins.Upload.Hint2").Text, NopPluginDefaults.UploadedItemsFileName)
                            </li>
                            <li>
                                <em>@T("Admin.Configuration.Plugins.Upload.Hint3")</em>
                            </li>
                            <li>
                                <em>@T("Admin.Configuration.Plugins.Upload.Hint4")</em>
                            </li>
                        </ul>
                        <div class="form-group row">
                            <div class="col-md-2">
                                <div class="label-wrapper">
                                    <label class="col-form-label">
                                        @T("Admin.Common.ZipFile")
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <input type="file" id="archivefile" name="archivefile" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" id="upload-plugin">
                            @T("Admin.Configuration.Plugins.Upload")
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


