{% requireAdmin false %}

{% extends "_layouts/cp" %}
{% set title = "Plugins"|t('app') %}
{% set readOnly = not craft.app.config.general.allowAdminChanges %}

{% 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', 'name'],
  [SORT_DESC, SORT_DESC, SORT_ASC],
  [SORT_NUMERIC, SORT_NUMERIC, SORT_NATURAL]
) %}

{% set disabledPlugins = craft.app.config.general.disabledPlugins %}

{% if readOnly %}
    {% set contentNotice = readOnlyNotice() %}
{% endif %}

{% block content %}
  {% if info|length %}
    <div class="tablepane">
      <table id="plugins" class="data fullwidth">
        <tbody>
        {% for handle, config in info %}
          {% set pluginStoreUrl = url('plugin-store/' ~ handle) %}
          {% set forceDisabled = disabledPlugins == '*' or handle in disabledPlugins %}

          {% set actionItems = [
            {
              icon: 'clipboard',
              label: 'Copy plugin handle'|t('app'),
              attributes: {
                data: {
                  action: 'copy-plugin-handle',
                },
              },
            },
            {
              icon: 'clipboard',
              label: 'Copy package name'|t('app'),
              attributes: {
                data: {
                  action: 'copy-package-name',
                },
              },
            },
            {type: 'hr'},
          ] %}
          {% if not config.isInstalled %}
            {% set actionItems = actionItems|push(
              {
                icon: 'plus',
                label: 'Install'|t('app'),
                action: 'plugins/install-plugin',
                params: {pluginHandle: handle},
                disabled: forceDisabled
              },
              {
                icon: 'minus',
                label: 'Remove'|t('app'),
                action: 'pluginstore/remove',
                params: {packageName: config.packageName},
                destructive: true,
              },
            ) %}
          {% else %}
            {% if config.isEnabled %}
              {% set actionItems = actionItems|push(
                {
                  icon: 'circle-dashed',
                  label: 'Disable'|t('app'),
                  action: 'plugins/disable-plugin',
                  params: {pluginHandle: handle},
                },
                {
                  icon: 'xmark',
                  label: 'Uninstall'|t('app'),
                  action: 'plugins/uninstall-plugin',
                  params: {pluginHandle: handle},
                  destructive: true,
                  confirm: 'Are you sure you want to uninstall {plugin}? You will lose all of its associated data.'|t('app', {
                  plugin: config.name,
                }),
                }
              ) %}
            {% else %}
              {% set actionItems = actionItems|push({
                icon: 'circle',
                label: 'Enable'|t('app'),
                action: 'plugins/enable-plugin',
                params: {pluginHandle: handle},
                disabled: forceDisabled,
              }) %}
            {% endif %}
          {% endif %}

          {% tag 'tr' with {
            id: "plugin-#{handle}",
            data: {
              name: config.name,
              handle: handle,
              'package-name': config.packageName,
            },
          } %}
            <th>
              <div class="plugin-infos">
                <a class="icon" href="{{ pluginStoreUrl }}" title="{{ 'View {plugin} in the Plugin Store'|t('app', {plugin: config.name}) }}" title="{{ 'View {plugin} in the Plugin Store'|t('app', {plugin: config.name}) }}">
                  {{ svg(craft.app.plugins.getPluginIconSvg(handle), sanitize=true, namespace=true) }}
                  {% if config.licenseKeyStatus == 'valid' or config.licenseIssues is not empty %}
                    <span class="license-key-status {{ config.licenseIssues is empty ? 'valid' }}"></span>
                  {% endif %}
                </a>
                <div class="plugin-details">
                  <div class="plugin-id">
                    <h2>{{ config.name }}</h2>
                    {% if config.hasMultipleEditions or config.isTrial %}
                      {% tag (config.upgradeAvailable ? 'a' : 'div') with {
                        class: 'edition',
                        href: config.upgradeAvailable ? pluginStoreUrl : false,
                      } %}
                        {% if config.hasMultipleEditions %}<div class="edition-name">{{ config.edition }}</div>{% endif %}
                        {% if config.isTrial %}<div class="edition-trial">{{ 'Trial'|t('app') }}</div>{% endif %}
                      {% endtag %}
                    {% endif %}
                    <span class="version">{{ config.version }}</span>
                  </div>
                  {% if config.description %}
                    <p>{{ config.description }}</p>
                  {% endif %}
                  {% if config.developer or config.documentationUrl or config.hasCpSettings %}
                    <ul class="links">
                      {%- apply spaceless %}
                        {% if config.developer %}
                          <li class="link-developer">
                            {% if config.developerUrl %}
                              {{ tag('a', {
                                href: config.developerUrl,
                                rel: 'noopener',
                                target: '_blank',
                                text: config.developer,
                              }) }}
                            {% else %}
                              <span>{{ config.developer }}</span>
                            {% endif %}
                          </li>
                        {% endif %}
                        {% if config.documentationUrl %}
                          <li class="link-docs">
                            {{ tag('a', {
                              href: config.documentationUrl,
                              rel: 'noopener',
                              target: '_blank',
                              text: 'Documentation'|t('app'),
                            }) }}
                          </li>
                        {% endif %}
                        {% if config.hasCpSettings and (not readOnly or config.hasReadOnlyCpSettings) %}
                          <li class="link-settings">
                            {{ tag('a', {
                              href: url('settings/plugins/'~config.moduleId),
                              text: 'Settings'|t('app'),
                            }) }}
                          </li>
                        {% endif %}
                      {% endapply -%}
                    </ul>
                  {% 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.licenseIssues is not empty %} error{% endif %}" size="29" value="{{ config.licenseKey[0:1] == '$' ? config.licenseKey : (config.licenseKey|replace('/.{4}/', '$0-')|trim('-')) }}" placeholder="XXXX-XXXX-XXXX-XXXX-XXXX-XXXX" readonly{% if readOnly %} disabled{% endif %}>
                    </div>
                    {% if not readOnly %}
                      {{ tag('a', {
                        text: 'Buy now'|t('app'),
                        class: [
                          'btn',
                          config.licenseIssues is not empty ? 'submit',
                          config.licenseKeyStatus != 'trial' ? 'hidden',
                        ]|filter,
                        href: url('plugin-store/buy/'~handle~'/'~config.edition),
                      }) }}
                      <div class="spinner hidden"></div>
                      {% endif %}
                  </div>
                  {% for issue in config.licenseIssues %}
                    <p class="error">
                      {% switch issue %}
                      {% case 'wrong_edition' %}
                        {{ 'This license is for the {name} edition.'|t('app', {
                          name: config.licensedEdition|capitalize,
                        }) }}
                      {% case 'no_trials' %}
                        {{ 'Plugin trials are not allowed on this domain.'|t('app') }}
                      {% case 'mismatched' %}
                        {{ 'This license is tied to another Craft install. Visit {accountLink} to detach it, or <a href="{buyUrl}">buy a new license</a>.'|t('app', {
                          accountLink: '<a href="https://console.craftcms.com" rel="noopener" target="_blank">console.craftcms.com</a>',
                          buyUrl: url('plugin-store/buy/'~handle~'/'~config.edition),
                        })|raw }}
                      {% case 'astray' %}
                        {{ 'This license isn’t allowed to run version {version}.'|t('app', {
                          version: config.version
                        }) }}
                      {% case 'required' %}
                        {{ 'A license key is required.'|t('app') }}
                      {% default %}
                        {{ 'Your license key is invalid.'|t('app') }}
                      {% endswitch %}
                    </p>
                  {% endfor %}
                </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') }}
                {% if forceDisabled %}
                  <span class="info">
                    {{ '{plugin} is disabled by the {setting} config setting.'|t({
                      plugin: config.name,
                      setting: 'disabledPlugins',
                    }) }}
                  </span>
                {% endif %}
              {% else %}
                <span class="status disabled"></span><span class="light">{{ "Not installed"|t('app') }}</span>
                {% if forceDisabled %}
                  <span class="info">
                    {{ '{plugin} can’t be installed due to the {setting} config setting.'|t({
                      plugin: config.name,
                      setting: 'disabledPlugins',
                    }) }}
                  </span>
                {% endif %}
              {% endif %}
            </td>
            {% if not readOnly %}
              <td class="nowrap thin" data-title="{{ 'Actions'|t('app') }}">
                {{ disclosureMenu(actionItems, {
                  buttonAttributes: {
                    class: ['action-btn', 'hairline'],
                    hiddenLabel: 'Actions'|t('app'),
                  },
                }) }}
              </td>
            {% endif %}
          {% endtag %}
        {% endfor %}
        </tbody>
      </table>
    </div>
  {% else %}
    <p id="no-plugins" class="zilch">{{ "There are no available plugins."|t('app') }}</p>
  {% endif %}
{% endblock %}

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