<?php

/**
 * Shared aside audit block.
 *
 * @var string|null $asideAuditDetailsName
 * @var bool $asideAuditOpen
 * @var string|null $asideAuditCreated
 * @var string|null $asideAuditCreatedByLabel
 * @var int|string|null $asideAuditCreatedById
 * @var string|null $asideAuditCreatedByUuid
 * @var string|null $asideAuditModified
 * @var string|null $asideAuditModifiedByLabel
 * @var int|string|null $asideAuditModifiedById
 * @var string|null $asideAuditModifiedByUuid
 * @var string|null $asideAuditStatusChanged
 * @var string|null $asideAuditStatusChangedByLabel
 * @var int|string|null $asideAuditStatusChangedById
 * @var string|null $asideAuditStatusChangedByUuid
 */

$asideAuditDetailsName = (string) ($asideAuditDetailsName ?? '');
$asideAuditOpen = !empty($asideAuditOpen);
$asideAuditCreated = (string) ($asideAuditCreated ?? '');
$asideAuditCreatedByLabel = (string) ($asideAuditCreatedByLabel ?? '');
$asideAuditCreatedById = $asideAuditCreatedById ?? null;
$asideAuditCreatedByUuid = (string) ($asideAuditCreatedByUuid ?? '');
$asideAuditModified = (string) ($asideAuditModified ?? '');
$asideAuditModifiedByLabel = (string) ($asideAuditModifiedByLabel ?? '');
$asideAuditModifiedById = $asideAuditModifiedById ?? null;
$asideAuditModifiedByUuid = (string) ($asideAuditModifiedByUuid ?? '');
$asideAuditStatusChanged = (string) ($asideAuditStatusChanged ?? '');
$asideAuditStatusChangedByLabel = (string) ($asideAuditStatusChangedByLabel ?? '');
$asideAuditStatusChangedById = $asideAuditStatusChangedById ?? null;
$asideAuditStatusChangedByUuid = (string) ($asideAuditStatusChangedByUuid ?? '');

$hasStatusRow = $asideAuditStatusChanged !== ''
  || $asideAuditStatusChangedByLabel !== ''
  || !empty($asideAuditStatusChangedById)
  || $asideAuditStatusChangedByUuid !== '';

$renderActor = static function (string $label, $id, string $uuid): void {
  if ($label !== '') {
    if ($uuid !== '') {
      ?>
      <a href="admin/users/edit/<?php e($uuid); ?>"><?php e($label); ?></a>
      <?php
      return;
    }
    e($label);
    return;
  }

  if (!empty($id)) {
    e('#' . $id);
    return;
  }

  e('-');
};

?>
<details <?php if ($asideAuditDetailsName !== '') { ?>name="<?php e($asideAuditDetailsName); ?>"<?php } ?> <?php if ($asideAuditOpen) { ?>open<?php } ?>>
  <summary><?php e(t('Audit')); ?></summary>
  <hr>
  <div class="grid">
    <div>
      <small><?php e(t('Created')); ?></small>
      <p><?php e(dt($asideAuditCreated) ?: '-'); ?></p>
    </div>
    <div>
      <small><?php e(t('Created by')); ?></small>
      <p><?php $renderActor($asideAuditCreatedByLabel, $asideAuditCreatedById, $asideAuditCreatedByUuid); ?></p>
    </div>
  </div>
  <div class="grid">
    <div>
      <small><?php e(t('Modified')); ?></small>
      <p><?php e(dt($asideAuditModified) ?: '-'); ?></p>
    </div>
    <div>
      <small><?php e(t('Modified by')); ?></small>
      <p><?php $renderActor($asideAuditModifiedByLabel, $asideAuditModifiedById, $asideAuditModifiedByUuid); ?></p>
    </div>
  </div>
  <?php if ($hasStatusRow): ?>
    <div class="grid">
      <div>
        <small><?php e(t('Status changed')); ?></small>
        <p><?php e(dt($asideAuditStatusChanged) ?: '-'); ?></p>
      </div>
      <div>
        <small><?php e(t('Status changed by')); ?></small>
        <p><?php $renderActor($asideAuditStatusChangedByLabel, $asideAuditStatusChangedById, $asideAuditStatusChangedByUuid); ?></p>
      </div>
    </div>
  <?php endif; ?>
</details>
