<?php
$integration = is_array($settings ?? null) ? $settings : [];
$appId = trim((string) ($integration['app_id'] ?? ''));
$hasAppSecret = (bool) ($integration['has_app_secret'] ?? false);
$hasAppId = (bool) ($integration['has_app_id'] ?? false);
$updatedAt = trim((string) ($integration['updated_at'] ?? ''));
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.apaczka.title')) ?></h2>
  <p class="muted mt-12"><?= $e($t('settings.apaczka.description')) ?></p>

  <?php if (!empty($errorMessage)): ?>
    <div class="alert alert--danger mt-12" role="alert"><?= $e((string) $errorMessage) ?></div>
  <?php endif; ?>

  <?php if (!empty($successMessage)): ?>
    <div class="alert alert--success mt-12" role="status"><?= $e((string) $successMessage) ?></div>
  <?php endif; ?>
</section>

<section class="card mt-16">
  <h3 class="section-title"><?= $e($t('settings.apaczka.config.title')) ?></h3>
  <div class="muted mt-12">
    <?= $e($t('settings.apaczka.status.app_id')) ?>:
    <strong><?= $e($hasAppId ? $t('settings.apaczka.status.saved') : $t('settings.apaczka.status.missing')) ?></strong>
    |
    <?= $e($t('settings.apaczka.status.app_secret')) ?>:
    <strong><?= $e($hasAppSecret ? $t('settings.apaczka.status.saved') : $t('settings.apaczka.status.missing')) ?></strong>
    <?php if ($updatedAt !== ''): ?>
      | <?= $e($t('settings.apaczka.status.updated_at')) ?>: <strong><?= $e($updatedAt) ?></strong>
    <?php endif; ?>
  </div>

  <form class="statuses-form mt-16" action="/settings/integrations/apaczka/save" method="post" novalidate>
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.apaczka.fields.app_id')) ?></span>
      <input class="form-control" type="text" name="app_id" maxlength="128" value="<?= $e($appId) ?>" required>
    </label>

    <label class="form-field">
      <span class="field-label"><?= $e($t('settings.apaczka.fields.app_secret')) ?></span>
      <input class="form-control" type="password" name="app_secret" autocomplete="new-password">
      <span class="muted"><?= $e($hasAppSecret ? $t('settings.apaczka.app_secret.saved') : $t('settings.apaczka.app_secret.missing')) ?></span>
    </label>

    <div class="form-actions">
      <button type="submit" class="btn btn--primary"><?= $e($t('settings.apaczka.actions.save')) ?></button>
    </div>
  </form>

  <form class="mt-12" action="/settings/integrations/apaczka/test" method="post">
    <input type="hidden" name="_token" value="<?= $e($csrfToken ?? '') ?>">
    <button type="submit" class="btn btn--secondary"><?= $e($t('settings.apaczka.actions.test')) ?></button>
  </form>
</section>
