<?php
declare(strict_types=1);
$tokens=is_array($tokens??null)?$tokens:[];$scopes=is_array($scopes??null)?$scopes:[];
?>
<section class="content-section"><div class="container narrow-container">
 <p class="eyebrow"><?= quaeriva_t($translator,'api_tokens.eyebrow') ?></p><h1><?= quaeriva_t($translator,'api_tokens.title') ?></h1><p class="section-lede"><?= quaeriva_t($translator,'api_tokens.intro') ?></p>
 <?php if(($error??'')!==''): ?><div class="notice notice-error" role="alert"><?= quaeriva_escape($error) ?></div><?php endif; ?>
 <?php if(($notice??'')!==''): ?><div class="notice" role="status"><?= quaeriva_escape($notice) ?></div><?php endif; ?>
 <?php if(($rawToken??'')!==''): ?><div class="notice api-token-once"><strong><?= quaeriva_t($translator,'api_tokens.copy_once') ?></strong><code dir="ltr"><?= quaeriva_escape($rawToken) ?></code></div><?php endif; ?>
 <section class="settings-panel"><h2><?= quaeriva_t($translator,'api_tokens.create') ?></h2><form method="post" action="<?= quaeriva_escape(quaeriva_url('settings/api-tokens')) ?>" class="question-form">
  <input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken??'') ?>"><input type="hidden" name="token_action" value="create">
  <div class="field-group"><label for="token-label"><?= quaeriva_t($translator,'api_tokens.label') ?></label><input id="token-label" name="label" maxlength="120" placeholder="<?= quaeriva_t($translator,'api_tokens.label_placeholder') ?>"></div>
  <div class="field-group"><label for="token-expiry"><?= quaeriva_t($translator,'api_tokens.expires') ?></label><input id="token-expiry" name="expires_at" type="datetime-local"></div>
  <fieldset class="token-scopes"><legend><?= quaeriva_t($translator,'api_tokens.scopes') ?></legend><?php foreach($scopes as $scope): ?><label class="check-row"><input type="checkbox" name="scopes[]" value="<?= quaeriva_escape($scope) ?>"><code dir="ltr"><?= quaeriva_escape($scope) ?></code></label><?php endforeach; ?></fieldset>
  <button class="button" type="submit"><?= quaeriva_t($translator,'api_tokens.create') ?></button>
 </form></section>
 <section class="settings-panel"><h2><?= quaeriva_t($translator,'api_tokens.active') ?></h2><?php if($tokens===[]): ?><p class="notice notice-muted"><?= quaeriva_t($translator,'api_tokens.none') ?></p><?php else: ?><div class="token-list"><?php foreach($tokens as $token): ?><article class="token-card"><div><strong dir="auto"><?= quaeriva_escape($token['device_label']??$translator->get('api_tokens.unnamed')) ?></strong><p><code dir="ltr">qv1_<?= quaeriva_escape($token['token_prefix']??'') ?>_…</code></p><small><bdi><?= quaeriva_escape($token['created_at']??'') ?></bdi><?php if(($token['last_used_at']??null)!==null): ?> · <?= quaeriva_t($translator,'api_tokens.last_used') ?> <bdi><?= quaeriva_escape($token['last_used_at']) ?></bdi><?php endif; ?></small></div><div><?php if(($token['revoked_at']??null)!==null): ?><span class="status-chip"><?= quaeriva_t($translator,'api_tokens.revoked') ?></span><?php else: ?><form method="post" action="<?= quaeriva_escape(quaeriva_url('settings/api-tokens')) ?>"><input type="hidden" name="_csrf" value="<?= quaeriva_escape($csrfToken??'') ?>"><input type="hidden" name="token_action" value="revoke"><input type="hidden" name="token_id" value="<?= (int)($token['id']??0) ?>"><button class="button button-secondary button-small" type="submit"><?= quaeriva_t($translator,'api_tokens.revoke') ?></button></form><?php endif; ?></div></article><?php endforeach; ?></div><?php endif; ?></section>
 <p><a class="text-link" href="<?= quaeriva_escape(quaeriva_url('settings/profile')) ?>"><?= quaeriva_t($translator,'profile.settings_title') ?></a></p>
</div></section>
