{% requireAdmin %}

{% extends "_layouts/cp" %}
{% set title = "Plugins"|t('app') %}
{% do view.registerAssetBundle("craft\\web\\assets\\plugins\\PluginsAsset") %}

{% set crumbs = [
    { label: "Settings"|t('app'), url: url('settings') }
] %}


{% set info = craft.app.plugins.getAllPluginInfo()|multisort(
    ['isEnabled', 'isInstalled', 'hasIssues', 'name'],
    [SORT_DESC, SORT_DESC, SORT_DESC, SORT_ASC],
    [SORT_NUMERIC, SORT_NUMERIC, SORT_NUMERIC, SORT_NATURAL]
) %}


{% block content %}
    {% if info|length %}
        <table id="plugins" class="data fullwidth collapsible">
            <tbody>
                {% for handle, config in info %}
                    <tr id="plugin-{{ handle }}" data-name="{{ config.name }}" data-handle="{{ handle }}">
                        <th>
                            <div class="plugin-infos">
                                <div class="icon">
                                    {{ svg(craft.app.plugins.getPluginIconSvg(handle), sanitize=true, namespace=true) }}
                                    {% if config.licenseKeyStatus == 'valid' or config.hasIssues %}
                                        <span class="license-key-status {{ config.licenseKeyStatus }}"></span>
                                    {% endif %}
                                </div>
                                <div class="details">
                                    <h2>
                                        {{ config.name }}
                                        <span class="light">{{ config.version }}</span>
                                    </h2>
                                    {% if config.description %}
                                        <p>{{ config.description }}</p>
                                    {% endif %}
                                    {% if config.developerUrl or config.developer or config.hasCpSettings or config.documentationUrl %}
                                        <p class="links">
                                            {%- spaceless %}
                                            {#{% if config.developerUrl %}#}
                                                {#<a href="{{ config.developerUrl }}" rel="noopener" target="_blank">{{ config.developer ?: config.developerUrl }}</a>#}
                                            {#{% elseif config.developer %}#}
                                                {#{{ config.developer }}#}
                                            {#{% endif %}#}
                                            {% if config.documentationUrl %}
                                                <a href="{{ config.documentationUrl }}" rel="noopener" target="_blank">{{ "Documentation"|t('app') }}</a>
                                            {% endif %}
                                            {% if config.hasCpSettings %}
                                                <a href="{{ url('settings/plugins/'~config.moduleId) }}"><span data-icon="settings"></span>{{ "Settings"|t('app') }}</a>
                                            {% endif %}
                                            {% endspaceless -%}
                                        </p>
                                    {% endif %}
                                    {% set showLicenseKey = config.licenseKey or config.licenseKeyStatus != 'unknown' %}
                                    <div class="flex license-key{% if not showLicenseKey %} hidden{% endif %}">
                                        <div class="pane">
                                            <input class="text code{% if config.hasIssues %} error{% endif %}" size="29" maxlength="29" value="{{ config.licenseKey|replace('/.{4}/', '$0-')|trim('-') }}" placeholder="XXXX-XXXX-XXXX-XXXX-XXXX-XXXX" readonly>
                                        </div>
                                        <a class="btn{% if config.hasIssues %} submit{% endif %}{% if not (showLicenseKey and not config.licenseKey) %} hidden{% endif %}" href="{{ url('plugin-store/buy/'~handle)}}">{{ "Buy now"|t('app') }}</a>
                                        <div class="spinner hidden"></div>
                                    </div>
                                    {% if showLicenseKey and config.licenseStatusMessage %}
                                        <p class="error">{{ config.licenseStatusMessage|raw }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </th>
                        <td class="nowrap" data-title="{{ 'Status'|t('app') }}">
                            {% if config.isEnabled %}
                                <span class="status on"></span>{{ "Installed"|t('app') }}
                            {% elseif config.isInstalled %}
                                <span class="status off"></span>{{ "Disabled"|t('app') }}
                            {% else %}
                                <span class="status"></span><span class="light">{{ "Not installed"|t('app') }}</span>
                            {% endif %}
                        </td>
                        <td class="nowrap thin" data-title="{{ 'Action'|t('app') }}">
                            <form method="post" accept-charset="UTF-8">
                                <input type="hidden" name="pluginHandle" value="{{ handle }}">
                                {{ csrfInput() }}
                                <div class="btngroup">
                                    <div class="btn menubtn" data-icon="settings"></div>
                                    <div class="menu" data-align="right">
                                        <ul>
                                            {% if not config.isInstalled %}
                                                <li><a class="formsubmit" data-action="plugins/install-plugin">{{ 'Install'|t('app') }}</a></li>
                                                <li><a class="formsubmit error" data-action="pluginstore/remove" data-param="packageName" data-value="{{ config.packageName }}">{{ 'Remove'|t('app') }}</a></li>
                                            {% else %}
                                                {% if config.isEnabled %}
                                                    <li><a class="formsubmit" data-action="plugins/disable-plugin">{{ 'Disable'|t('app') }}</a></li>
                                                    <li><a class="formsubmit error" data-action="plugins/uninstall-plugin" data-confirm="{{ 'Are you sure you want to uninstall {plugin}? You will lose all of its associated data.'|t('app', { plugin: config.name }) }}">{{ 'Uninstall'|t('app') }}</a></li>
                                                {% else %}
                                                    <li><a class="formsubmit" data-action="plugins/enable-plugin">{{ 'Enable'|t('app') }}</a></li>
                                                {% endif %}
                                            {% endif %}
                                        </ul>
                                    </div>
                                </div>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p id="no-plugins">{{ "There are no available plugins."|t('app') }}
    {% endif %}
{% endblock %}

{% js %}
new Craft.PluginManager();
{% endjs %}
