<?php

/**
 * Shared details titlebar with optional back link, menu dropdown and action buttons.
 *
 * @var array $titlebar
 */

use MintyPHP\Session;

$titlebar = is_array($titlebar ?? null) ? $titlebar : [];
$title = (string) ($titlebar['title'] ?? '');
$backHref = (string) ($titlebar['backHref'] ?? '');
$backTitle = (string) ($titlebar['backTitle'] ?? t('Cancel'));
$actions = is_array($titlebar['actions'] ?? null) ? $titlebar['actions'] : [];
$menuItems = is_array($titlebar['menuItems'] ?? null) ? $titlebar['menuItems'] : [];

?>
<div class="app-details-titlebar">
  <h1>
    <?php if ($backHref !== ''): ?>
      <a href="<?php e($backHref); ?>" title="<?php e($backTitle); ?>"><i class="bi bi-arrow-left"></i></a>
    <?php endif; ?>
    <?php e($title); ?>
  </h1>
  <div class="app-details-titlebar-actions">
    <?php if ($menuItems): ?>
      <details class="dropdown">
        <summary role="button" class="outline secondary"><i class="bi bi-three-dots"></i></summary>
        <ul dir="rtl">
          <?php foreach ($menuItems as $menuItem): ?>
            <?php
            $item = is_array($menuItem) ? $menuItem : [];
            $itemType = (string) ($item['type'] ?? 'link');
            $itemLabel = (string) ($item['label'] ?? '');
            if ($itemLabel === '') {
              continue;
            }
            ?>
            <li>
              <?php if ($itemType === 'form'): ?>
                <?php
                $itemMethod = strtoupper((string) ($item['method'] ?? 'POST'));
                $itemAction = (string) ($item['action'] ?? '');
                $itemConfirm = (string) ($item['confirm'] ?? '');
                $itemButtonClass = (string) ($item['class'] ?? 'transparent');
                $itemNeedsCsrf = !array_key_exists('csrf', $item) ? true : !empty($item['csrf']);
                $itemTarget = (string) ($item['target'] ?? '');
                $itemFields = is_array($item['fields'] ?? null) ? $item['fields'] : [];
                ?>
                <?php if ($itemAction !== ''): ?>
                  <form
                    method="<?php e($itemMethod); ?>"
                    action="<?php e($itemAction); ?>"
                    <?php if ($itemTarget !== ''): ?>target="<?php e($itemTarget); ?>"<?php endif; ?>
                    <?php if ($itemConfirm !== ''): ?>onsubmit="return confirm('<?php e($itemConfirm); ?>');"<?php endif; ?>
                  >
                    <?php if ($itemNeedsCsrf && $itemMethod !== 'GET'): ?>
                      <?php Session::getCsrfInput(); ?>
                    <?php endif; ?>
                    <?php foreach ($itemFields as $fieldName => $fieldValue): ?>
                      <?php if ($fieldName !== ''): ?>
                        <input type="hidden" name="<?php e((string) $fieldName); ?>" value="<?php e((string) $fieldValue); ?>">
                      <?php endif; ?>
                    <?php endforeach; ?>
                    <button type="submit" class="<?php e($itemButtonClass); ?>">
                      <?php e($itemLabel); ?>
                    </button>
                  </form>
                <?php endif; ?>
              <?php else: ?>
                <?php
                $itemHref = (string) ($item['href'] ?? '');
                $itemLinkClass = (string) ($item['class'] ?? 'transparent');
                $itemTarget = (string) ($item['target'] ?? '');
                $itemRel = (string) ($item['rel'] ?? '');
                ?>
                <?php if ($itemHref !== ''): ?>
                  <a role="button" class="<?php e($itemLinkClass); ?>" href="<?php e($itemHref); ?>"
                    <?php if ($itemTarget !== ''): ?>target="<?php e($itemTarget); ?>"<?php endif; ?>
                    <?php if ($itemRel !== ''): ?>rel="<?php e($itemRel); ?>"<?php endif; ?>>
                    <?php e($itemLabel); ?>
                  </a>
                <?php endif; ?>
              <?php endif; ?>
            </li>
          <?php endforeach; ?>
        </ul>
      </details>
    <?php endif; ?>

    <?php foreach ($actions as $action): ?>
      <?php
      $item = is_array($action) ? $action : [];
      $itemLabel = (string) ($item['label'] ?? '');
      if ($itemLabel === '') {
        continue;
      }
      $itemType = (string) ($item['type'] ?? 'submit');
      $itemClass = (string) ($item['class'] ?? 'secondary outline');
      $itemForm = (string) ($item['form'] ?? '');
      $itemName = (string) ($item['name'] ?? '');
      $itemValue = (string) ($item['value'] ?? '');
      $itemDisabled = !empty($item['disabled']);
      ?>
      <button
        type="<?php e($itemType); ?>"
        <?php if ($itemForm !== ''): ?>form="<?php e($itemForm); ?>"<?php endif; ?>
        <?php if ($itemName !== ''): ?>name="<?php e($itemName); ?>"<?php endif; ?>
        <?php if ($itemValue !== ''): ?>value="<?php e($itemValue); ?>"<?php endif; ?>
        <?php if ($itemDisabled): ?>disabled<?php endif; ?>
        class="<?php e($itemClass); ?>"
      >
        <?php e($itemLabel); ?>
      </button>
    <?php endforeach; ?>
  </div>
</div>
