<?php

/**
 * @var array<int, string> $errors
 * @var array $form
 * @var array $permission
 */

use MintyPHP\Session;

?>
<div class="app-details-container">
  <section>
    <?php
    $breadcrumbs = [
      ['label' => t('Home'), 'path' => 'admin'],
      ['label' => t('Permissions'), 'path' => 'admin/permissions'],
      ['label' => t('Edit permission')],
    ];
    require templatePath('partials/app-breadcrumb.phtml');
    $titlebar = [
      'title' => t('Edit permission'),
      'backHref' => 'admin/permissions',
      'backTitle' => t('Cancel'),
      'actions' => can('permissions.update') ? [
        [
          'form' => 'permission-form',
          'name' => 'action',
          'value' => 'save',
          'class' => 'secondary outline',
          'label' => t('Save'),
        ],
        [
          'form' => 'permission-form',
          'name' => 'action',
          'value' => 'save_close',
          'class' => 'primary',
          'label' => t('Save & close'),
        ],
      ] : [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>
    <?php if (!empty($errors)): ?>
      <div class="notice" data-variant="error">
        <ul>
          <?php foreach ($errors as $error): ?>
            <li><?php e($error); ?></li>
          <?php endforeach; ?>
        </ul>
      </div>
    <?php endif; ?>
    <?php
    $values = $form ?? $permission ?? [];
    $isReadOnly = !can('permissions.update');
    $detailsOpenAll = true;
    $isReadOnly = $isReadOnly ?? false;
    $showDangerZone = can('permissions.delete') && empty($permission['is_system']);
    $dangerZoneDeleteFormId = $showDangerZone ? 'permission-delete-form' : '';
    $dangerZoneWarning = t('This will permanently delete this permission.');
    $dangerZoneActionLabel = t('Delete permission');
    require __DIR__ . '/_form.phtml';
    ?>
    <?php if ($showDangerZone): ?>
      <form
        id="permission-delete-form"
        method="post"
        action="admin/permissions/delete/<?php e((string) ($permission['id'] ?? '')); ?>"
        hidden
        onsubmit="return confirm('<?php e(t('Delete this permission?')); ?>');"
      >
        <?php Session::getCsrfInput(); ?>
      </form>
    <?php endif; ?>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <h2><?php e($values['key'] ?? ''); ?></h2>
        <p><?php e(t('Permission')); ?></p>
      </hgroup>
      <div class="badge-list">
        <?php if ((string) ($values['active'] ?? '1') === '0'): ?>
          <span class="badge" data-variant="danger"><?php e(t('Inactive')); ?></span>
        <?php else: ?>
          <span class="badge" data-variant="success"><?php e(t('Active')); ?></span>
        <?php endif; ?>
        <?php if (!empty($values['is_system'])): ?>
          <span class="badge" data-variant="neutral"><?php e(t('System')); ?></span>
        <?php endif; ?>
      </div>
      <hr>
    </div>
  </aside>
</div>
