<?php

/**
 * @var array<int, string> $errors
 * @var array $form
 * @var array $role
 * @var bool $canUpdateRole
 * @var bool $canDeleteRole
 */

use MintyPHP\Session;

$values = $form ?? $role ?? [];
$canUpdateRole = (bool) ($canUpdateRole ?? false);
$canDeleteRole = (bool) ($canDeleteRole ?? false);
$isReadOnly = !$canUpdateRole;
$titleText = $isReadOnly ? t('View role') : t('Edit role');

?>

<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
      'title' => $titleText,
      'backHref' => 'admin/roles',
      'backTitle' => t('Cancel'),
      'actions' => $canUpdateRole ? [
        [
          'form' => 'role-form',
          'name' => 'action',
          'value' => 'save',
          'class' => 'secondary outline',
          'label' => t('Save'),
        ],
        [
          'form' => 'role-form',
          'name' => 'action',
          'value' => 'save_close',
          'class' => 'primary',
          'label' => t('Save & close'),
        ],
      ] : [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>
    <?php
    $validationSummaryErrors = $validationSummaryErrors ?? ($errors ?? []);
    require templatePath('partials/app-details-validation-summary.phtml');
    ?>
    <?php if (!empty($warnings)): ?>
      <div class="notice" data-variant="info">
        <ul>
          <?php foreach ($warnings as $warning): ?>
            <li><?php e($warning); ?></li>
          <?php endforeach; ?>
        </ul>
      </div>
    <?php endif; ?>

    <?php
    $detailsOpenAll = true;
    $isReadOnly = $isReadOnly ?? false;
    $showDangerZone = $canDeleteRole;
    $dangerZoneDeleteFormId = $showDangerZone ? 'role-delete-form' : '';
    $dangerZoneWarning = t('This will permanently delete this role.');
    $dangerZoneActionLabel = t('Delete role');
    require __DIR__ . '/_form.phtml';
    ?>
    <?php if ($showDangerZone): ?>
      <form
        id="role-delete-form"
        method="post"
        action="admin/roles/delete/<?php e($values['uuid'] ?? ''); ?>"
        hidden
        data-detail-confirm-message="<?php e(t('Delete this role?')); ?>"
        data-detail-action-kind="delete"
      >
        <?php Session::getCsrfInput(); ?>
      </form>
    <?php endif; ?>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <h2><?php e($values['description'] ?? ''); ?></h2>
        <p><?php e(t('Role')); ?></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; ?>
      </div>
      <?php if (!empty($values['code'])): ?>
        <hr>
        <p><small><?php e(t('Code')); ?></small><br><span class="badge" data-variant="neutral"><?php e($values['code']); ?></span></p>
      <?php endif; ?>
      <?php
      $asideAuditDetailsName = 'role-audit';
      $asideAuditCreated = (string) ($values['created'] ?? '');
      $asideAuditCreatedByLabel = (string) ($values['created_by_label'] ?? '');
      $asideAuditCreatedById = $values['created_by'] ?? null;
      $asideAuditCreatedByUuid = (string) ($values['created_by_uuid'] ?? '');
      $asideAuditModified = (string) ($values['modified'] ?? '');
      $asideAuditModifiedByLabel = (string) ($values['modified_by_label'] ?? '');
      $asideAuditModifiedById = $values['modified_by'] ?? null;
      $asideAuditModifiedByUuid = (string) ($values['modified_by_uuid'] ?? '');

      $asideIdsDetailsName = 'role-ids';
      $asideIdValue = $values['id'] ?? '';
      $asideUuidValue = (string) ($values['uuid'] ?? '');
      ?>
      <hr>
      <?php require templatePath('partials/app-details-aside-audit.phtml'); ?>
      <hr>
      <?php require templatePath('partials/app-details-aside-ids.phtml'); ?>
      <hr>
    </div>
  </aside>
</div>
