<?php
$items = is_array($rows ?? null) ? $rows : [];
?>

<section class="card">
  <h2 class="section-title"><?= $e($t('settings.integrations_hub.title')) ?></h2>
  <p class="muted mt-12"><?= $e($t('settings.integrations_hub.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 integrations-overview">
  <h3 class="section-title"><?= $e($t('settings.integrations_hub.list_title')) ?></h3>
  <div class="table-wrap mt-12">
    <table class="table">
      <thead>
      <tr>
        <th><?= $e($t('settings.integrations_hub.fields.provider')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.instance')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.authorization')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.secret')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.active')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.last_test')) ?></th>
        <th><?= $e($t('settings.integrations_hub.fields.actions')) ?></th>
      </tr>
      </thead>
      <tbody>
      <?php if ($items === []): ?>
        <tr>
          <td class="muted" colspan="7"><?= $e($t('settings.integrations_hub.empty')) ?></td>
        </tr>
      <?php else: ?>
        <?php foreach ($items as $item): ?>
          <tr>
            <td><?= $e((string) ($item['provider'] ?? '')) ?></td>
            <td><?= $e((string) ($item['instance'] ?? '')) ?></td>
            <td><?= $e((string) ($item['authorization_status'] ?? '')) ?></td>
            <td><?= $e((string) ($item['secret_status'] ?? '')) ?></td>
            <td><?= $e(!empty($item['is_active']) ? $t('settings.integrations_hub.active.yes') : $t('settings.integrations_hub.active.no')) ?></td>
            <td><?= $e((string) ($item['last_test_at'] ?? '')) ?></td>
            <td>
              <a class="btn btn--secondary btn--sm" href="<?= $e((string) ($item['configure_url'] ?? '/settings/integrations')) ?>">
                <?= $e($t('settings.integrations_hub.actions.settings')) ?>
              </a>
            </td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</section>
