{% import '@CorePluginsAdmin/macros.twig' as pluginsMacro %}

<div class="row marketplace-max-width">

    {% if (pluginsToShow|length) > 20 or query %}
        <div class="col-sm-12 clearfix">
            <form action="{{ linkTo({'sort': ''}) }}" method="post" class="plugin-search">
                <input value="{{ query }}" placeholder="{{ 'General_Search'|translate }} {{ numAvailablePlugins }} {{ 'General_Plugins'|translate|lcfirst }}..." type="text" name="query"/>
                <button type="submit">{{ 'General_Search'|translate }}</button>
            </form>

            <ul class="nav nav-pills">
                <li {% if 'lastupdated' == sort %}class="active"{% endif %}>
                    <a href="{{ linkTo({'sort': 'lastupdated', 'query': ''}) }}">
                        {{ 'Marketplace_SortByLastUpdated'|translate }}
                    </a>
                </li>
                <li {% if 'newest' == sort %}class="active"{% endif %}>
                    <a href="{{ linkTo({'sort': 'newest', 'query': ''}) }}">
                        {{ 'Marketplace_SortByNewest'|translate }}
                    </a>
                </li>
                <li {% if 'popular' == sort %}class="active"{% endif %}>
                    <a href="{{ linkTo({'sort': 'popular', 'query': ''}) }}">
                        {{ 'Marketplace_SortByPopular'|translate }}
                    </a>
                </li>
            </ul>

        </div>
    {% endif %}

    {% if pluginsToShow|length == 0 %}
        <div class="col-sm-12 clearfix">
            {% if showThemes %}
                {{ 'Marketplace_NoThemesFound'|translate }}
            {% else %}
                {{ 'Marketplace_NoPluginsFound'|translate }}
            {% endif %}
        </div>
    {% endif %}

    {% for plugin in pluginsToShow %}

        <div class="col-md-4">
            <div class="panel plugin">

                <div class="panel-heading">
                    <h3 class="panel-title panel-title-block" title="{{ 'General_MoreDetails'|translate }}">
                        <a class="plugin-details" href="#" data-pluginName="{{ plugin.name }}">{{ plugin.displayName }}</a>
                    </h3>
                </div>

                <div class="panel-body">
                    <p class="description">
                        {{ plugin.description }}
                        <a class="more plugin-details" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">
                            &rsaquo; {{ 'General_MoreLowerCase'|translate }}</a>
                    </p>

                    {% if showThemes %}
                        {# Screenshot for themes #}
                        <a class="more plugin-details" href="#" data-pluginName="{{ plugin.name }}">
                            <img title="{{ 'General_MoreDetails'|translate }}"
                                 class="preview" src="{{ plugin.screenshots|first }}?w=250&h=150"/></a>
                    {% endif %}

                    <ul class="metadata">
                        <li>
                            {% if plugin.latestVersion %}
                                {{ 'CorePluginsAdmin_Version'|translate }}: {{ plugin.latestVersion }}
                            {% endif %}

                            {% if plugin.canBeUpdated %}
                                    <a class="plugin-details update-available" href="#" data-pluginName="{{ plugin.name }}" data-activePluginTab="changelog"
                                       title="{{ 'Marketplace_PluginUpdateAvailable'|translate(plugin.currentVersion, plugin.latestVersion) }}">
                                        {{ 'Marketplace_NewVersion'|translate }}</a>
                            {% endif %}
                        </li>

                        {% if plugin.lastUpdated %}
                            <li>{{ 'Marketplace_Updated'|translate }}: {{ plugin.lastUpdated }}</li>
                        {% endif %}

                        {% if plugin.numDownloads %}
                            <li>{{ 'General_Downloads'|translate }}: {{ plugin.numDownloads }}</li>
                        {% endif %}
                        <li>{{ 'Marketplace_Developer'|translate }}: {{ marketplaceMacro.pluginDeveloper(plugin.owner) }}</li>
                    </ul>
                </div>

                {% macro moreDetailsLink(plugin) %}
                    {% set canBePurchased = not plugin.isDownloadable and plugin.shop is defined and plugin.shop and plugin.shop.url %}
                    <a class="btn btn-block plugin-details {% if canBePurchased %}purchaseable{% endif %}" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">

                        {% if canBePurchased and plugin.shop.variations %}
                            {% set foundCheapest = 0 %}
                            {% for variation in plugin.shop.variations %}
                                {% if not foundCheapest and variation.cheapest is defined and variation.cheapest %}
                                    {% set foundCheapest = 1 %}
                                    {{ 'Marketplace_PriceFromPerPeriod'|translate(variation.prettyPrice, variation.period) }}
                                {% endif %}
                            {% endfor %}
                            {% if not foundCheapest %}
                                {{ 'Marketplace_PriceFromPerPeriod'|translate(plugin.shop.variations.0.prettyPrice, plugin.shop.variations.0.period) }}
                            {% endif %}
                        {% else %}
                            {{ 'General_MoreDetails'|translate }}
                        {% endif %}

                    </a>
                {% endmacro %}

                {% if isSuperUser %}
                    <div class="panel-footer" data-pluginName="{{ plugin.name }}">
                        {% if plugin.isMissingLicense is defined and plugin.isMissingLicense %}

                            <button class="btn btn-noop" style="padding-right: 0;padding-left: 0;">
                                {{ 'Marketplace_LicenseMissing'|translate }}
                            </button>
                            <span style="white-space:nowrap">(<a class="btn btn-link plugin-details" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">{{ 'General_Help'|translate }}</a>)</span>

                        {% elseif plugin.hasExceededLicense is defined and plugin.hasExceededLicense %}

                            <button class="btn btn-noop" style="padding-right: 0;padding-left: 0;">
                                {{ 'Marketplace_LicenseExceeded'|translate }}
                            </button>
                            <span style="white-space:nowrap">(<a class="btn btn-link plugin-details" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">{{ 'General_Help'|translate }}</a>)</span>

                        {% elseif plugin.canBeUpdated and 0 == plugin.missingRequirements|length and isAutoUpdatePossible %}
                            <a class="btn btn-block"
                               href="{{ linkTo({'module': 'Marketplace', 'action':'updatePlugin', 'pluginName': plugin.name, 'nonce': updateNonce}) }}">
                                {{ 'CoreUpdater_UpdateTitle'|translate }}
                            </a>
                        {% elseif plugin.missingRequirements|length > 0 or not isAutoUpdatePossible %}

                            {% macro downloadButton(showOr, plugin, isAutoUpdatePossible, showBrackets = false) -%}
                                {%- if plugin.missingRequirements|length == 0 and plugin.isDownloadable and not isAutoUpdatePossible -%}
                                    {% if showBrackets %}({% endif %}<span onclick="$(this).css('display', 'none')">
                                        {%- if showOr %} {{ 'General_Or'|translate }} {% endif -%}
                                        <a class="btn btn-link plugin-details download"
                                           href="{{ linkTo({'module': 'Marketplace', 'action': 'download', 'pluginName': plugin.name, 'nonce': (plugin.name|nonce)}) }}"
                                        >{{ 'General_Download'|translate }}</a></span>{% if showBrackets %}){% endif %}
                                {%- endif -%}
                            {%- endmacro %}

                            {% if plugin.canBeUpdated and 0 == plugin.missingRequirements|length %}
                                <button class="btn btn-noop" style="padding-right: 0px;padding-left: 0px;">
                                    {{ 'Marketplace_CannotUpdate'|translate }}
                                </button>
                                <span style="white-space:nowrap">(<a class="btn btn-link plugin-details" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">{{ 'General_Help'|translate }}</a>{{ _self.downloadButton(true, plugin, isAutoUpdatePossible)|raw }})</span>
                            {% elseif plugin.isInstalled %}
                                <button class="btn btn-noop" style="padding-right: 0px;padding-left: 0px;">
                                    {{ 'General_Installed'|translate }}
                                </button>
                                {{ _self.downloadButton(false, plugin, isAutoUpdatePossible, true)|raw }}
                            {% elseif not plugin.isDownloadable %}
                                {{ _self.moreDetailsLink(plugin)|raw }}
                            {% else %}
                                <button class="btn btn-noop" style="padding-right: 0px;padding-left: 0px;">
                                    {{ 'Marketplace_CannotInstall'|translate }}
                                </button>
                                <span style="white-space:nowrap">(<a class="btn btn-link plugin-details" href="#" data-pluginName="{{ plugin.name }}" title="{{ 'General_MoreDetails'|translate }}">{{ 'General_Help'|translate }}</a>{{ _self.downloadButton(true, plugin, isAutoUpdatePossible)|raw }})</span>
                            {% endif %}

                        {% elseif plugin.isInstalled %}
                            <button class="btn btn-noop" style="padding-right: 0px;">
                                {{ 'General_Installed'|translate }}
                            </button>

                            {% if not plugin.isInvalid and not isMultiServerEnvironment and isPluginsAdminEnabled %}
                                ({{ pluginsMacro.pluginActivateDeactivateAction(plugin.name, plugin.isActivated, plugin.missingRequirements, deactivateNonce, activateNonce) }})
                            {% endif %}

                        {% elseif plugin.isPaid and not plugin.isDownloadable %}
                            {{ _self.moreDetailsLink(plugin)|raw }}
                        {% else %}
                            <a href="{{ linkTo({'module': 'Marketplace', 'action': 'installPlugin', 'pluginName': plugin.name, 'nonce': installNonce}) }}"
                               class="btn btn-block">
                                {{ 'Marketplace_ActionInstall'|translate }}
                            </a>
                        {% endif %}
                    </div>
                {% else %}
                    <div class="panel-footer" data-pluginName="{{ plugin.name }}">
                        {{ _self.moreDetailsLink(plugin)|raw }}
                    </div>
                {% endif %}

            </div>
        </div>
    {% endfor %}

</div>
