@model Disco.Web.Areas.Config.Models.Plugins.IndexViewModel
@using Disco.Services.Plugins;
@{
    Authorization.Require(Claims.Config.Plugin.Show);

    var canConfig = Authorization.Has(Claims.Config.Plugin.Configure);
    var canUninstall = Authorization.Has(Claims.Config.Plugin.Uninstall);
    var canInstall = Authorization.Has(Claims.Config.Plugin.Install);

    ViewBag.Title = Html.ToBreadcrumb("Configuration", MVC.Config.Config.Index(), "Plugins");
}
<div id="plugins">
    @{
        if (Model.PluginManifests.Count == 0)
        { 
        <div class="form" style="width: 450px; padding: 100px 0;">
            <h2>No Plugins are Installed</h2>
        </div>  
        }
        else
        {
            var pluginGroups = Model.PluginManifestsByCategory;


            int itemsPerColumn = pluginGroups.Count / 3;
            var itemNextId = 0;
    
        <table id="pageMenu">
            <tr>
                @for (int i = 0; i < 3; i++)
                {
                    <td>
                        @{
                    int itemsForThisColumn = itemsPerColumn + (pluginGroups.Count % 3 > i ? 1 : 0);
                    for (int i2 = 0; i2 < itemsForThisColumn && itemNextId < pluginGroups.Count; i2++)
                    {
                        var pluginGroup = pluginGroups[itemNextId];
                        itemNextId++;
                            <div class="pageMenuArea">
                                <h2>@Plugins.PluginFeatureCategoryDisplayName(pluginGroup.Item1)</h2>
                                @foreach (var pluginDefinition in pluginGroup.Item2)
                                {
                                    if (canConfig)
                                    {
                                    <a href="@Url.Action(MVC.Config.Plugins.Configure(pluginDefinition.Id))">
                                        <i class="fa fa-cogs"></i>
                                        <h3>@pluginDefinition.Name</h3>
                                    </a>
                                    }
                                    else
                                    {
                                    <h3>@pluginDefinition.Name</h3>
                                    }
                                    <div class="pageMenuBlurb">
                                        <span class="pluginVersion">v@(pluginDefinition.VersionFormatted)</span> | @pluginDefinition.Author | <a href="@pluginDefinition.Url" title="More Information" target="_blank"><i class="fa fa-external-link"></i></a>
                                    </div>
                                }
                            </div>
                    }
                        }
                    </td>
                }
            </tr>
        </table>
                if (canUninstall)
                {
        <div id="dialogUninstallPlugins" title="Uninstall Plugin">
            <div>
                @Html.DropDownList("uninstallPlugin", Model.PluginManifests.ToSelectListItems(null, true, "Select a Plugin to Uninstall"))
            </div>
            <div>
                <input id="uninstallPluginData" type="checkbox" /><label for="uninstallPluginData"> Uninstall Plugin Data</label>
                <div id="uninstallPluginDataAlert" style="display: none; padding: 0.7em 0.7em; margin-top: 8px;" class="ui-state-error ui-corner-all">
                    <i class="fa fa-exclamation-triangle fa-lg"></i>&nbsp;NOTE: Data will be permanently deleted
                </div>
            </div>
        </div>
        <div id="dialogUninstallPluginConfirm" title="Confirm Plugin Uninstall">
            <div style="padding: 0.7em 0.7em; margin-top: 8px;" class="ui-state-highlight ui-corner-all">
                <i class="fa fa-info-circle information"></i>&nbsp;Are you sure you want to uninstall this plugin?
            <h4 id="uninstallPluginConfirm"></h4>
            </div>
            <div id="uninstallPluginDataConfirm" style="display: none; padding: 0.7em 0.7em; margin-top: 8px;" class="ui-state-error ui-corner-all">
                <i class="fa fa-exclamation-triangle fa-lg"></i>&nbsp;NOTE: Data will be permanently deleted
            </div>
        </div>
        <script>
            $(function () {
                // Uninstall
                var uninstallUrl = '@(Url.Action(MVC.API.Plugin.Uninstall()))/';
        var uninstallPlugin, uninstallPluginData, $dialogConfirm, uninstallPluginConfirm, uninstallPluginDataConfirm;

        var pluginId, pluginName, pluginUninstallData;

        var $dialog = $('#dialogUninstallPlugins').dialog({
            resizable: false,
            modal: true,
            width: 350,
            autoOpen: false,
            buttons: {
                "Uninstall": function () {
                    pluginId = uninstallPlugin.val();
                    pluginName = uninstallPlugin.find('option:selected').text();
                    pluginUninstallData = uninstallPluginData.is(':checked');

                    if (!pluginId) {
                        alert('Select a plugin to uninstall');
                    } else {
                        uninstallPluginConfirm.text(pluginName + ' [' + pluginId + ']');
                        if (pluginUninstallData)
                            uninstallPluginDataConfirm.show();
                        else
                            uninstallPluginDataConfirm.hide();

                        $dialogConfirm.dialog('open');
                        $(this).dialog("close");
                    }
                },
                Cancel: function () {
                    uninstallPluginData.prop('checked', false);
                    $('#uninstallPluginDataAlert').hide();
                    $(this).dialog("close");
                }
            }
        });

        $dialogConfirm = $('#dialogUninstallPluginConfirm').dialog({
            resizable: false,
            modal: true,
            width: 350,
            autoOpen: false,
            buttons: {
                "Confirm Uninstall": function () {
                    var url = uninstallUrl + pluginId;
                    if (pluginUninstallData)
                        url += '?UninstallData=true'
                    else
                        url += '?UninstallData=false'

                    window.location.href = url;
                    $(this).dialog("disable");
                },
                Cancel: function () {
                    uninstallPluginData.prop('checked', false);
                    $('#uninstallPluginDataAlert').hide();
                    $(this).dialog("close");
                }
            }
        });

        uninstallPlugin = $('#uninstallPlugin');
        uninstallPluginData = $('#uninstallPluginData');
        uninstallPluginConfirm = $('#uninstallPluginConfirm');
        uninstallPluginDataConfirm = $('#uninstallPluginDataConfirm');

        $('#buttonUninstall').click(function () {
            $dialog.dialog('open');
            return false;
        });

        $('#uninstallPluginData').change(function () {
            if ($(this).is(':checked')) {
                $('#uninstallPluginDataAlert').slideDown();
            } else {
                $('#uninstallPluginDataAlert').slideUp();
            }
        });
    });
        </script>
                }
                if (Model.PluginUpdates.Count > 0)
                {
        <div id="updateAvailableContainer">
            <i class="fa fa-cloud-download"></i>
            <div>@(Model.PluginUpdates.Count) plugin update@(Model.PluginUpdates.Count == 1 ? " is" : "s are") available</div>
            @if (canInstall)
            {
                @Html.ActionLinkSmallButton("Update Now", MVC.API.Plugin.UpdateAll(), null, "alert")
            }
            else
            {
                <div class="smallMessage">You do not have sufficient permission to install these updates.</div>
            }
        </div>
        <script>
            (function () {
                $(function () {
                    var layout_PageHeading = $('#layout_PageHeading').height(80);
                    var updateAvailableContainer = $('#updateAvailableContainer');
                    updateAvailableContainer.appendTo(layout_PageHeading);
                });
            })();
        </script>    
                }
        }
    }
</div>
<div class="actionBar">
    @if (canUninstall && Model.PluginManifests.Count > 0)
    { 
        @Html.ActionLinkButton("Uninstall Plugins", MVC.Config.Plugins.Index(), "buttonUninstall")
    }
    @if (canInstall)
    {
        @Html.ActionLinkButton("Install Plugins", MVC.Config.Plugins.Install())
    }
</div>
