<?php
// admin/smtp.php

// ------------------------------------------------------
// Session + Projekt-Initialisierung
// ------------------------------------------------------
session_start();
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../auth/AuthController.php';

// ------------------------------------------------------
// Zugriffsschutz
// ------------------------------------------------------
$auth = new AuthController();
if (!$auth->isLoggedIn()) {
    header('Location: ' . $adminLoginPath);
    exit;
}

$title = 'SMTP-Zugänge';
$db = Database::getInstance();
$entries = [];

// ------------------------------------------------------
// SMTP-Zugänge laden (mit Logging)
// ------------------------------------------------------
try {
    $sql = "SELECT * FROM smtp_accounts ORDER BY id DESC";
    $stmt = $db->query($sql);
    $entries = $stmt->fetchAll();

    ErrorLogger::logSqlSuccess('smtp: load accounts | SQL: ' . $sql . ' | params: []');
} catch (PDOException $e) {
    ErrorLogger::logSqlError('smtp: load accounts failed | SQL: ' . ($sql ?? 'n/a') . ' | params: [] | error: ' . $e->getMessage());
    ErrorLogger::log('Fehler beim Laden der SMTP-Zugänge: ' . $e->getMessage(), 'ERROR');
}

ob_start();
?>
<div class="page-header">
  <i class="tml tml-mail"></i> SMTP-Rotation &raquo; Übersicht
</div>

<div class="page-contend">
  <a href="smtp_create.php" class="btn btn-success mb-3">
    <i class="tml tml-plus"></i> Neuer SMTP-Zugang
  </a>

  <?php if (empty($entries)): ?>
    <div class="alert alert-info">
      <i class="tml tml-info"></i> Es sind noch keine SMTP-Zugänge angelegt.
    </div>
  <?php else: ?>
    <table class="table table-striped align-middle">
      <thead>
        <tr>
          <th>ID</th>
          <th>Host</th>
          <th>E-Mail-Adresse</th>
          <th>Port</th>
          <th>Sicherheit</th>
          <th>Status</th>
          <th>Global-Limit (20 Min)</th>
          <th>Aktion</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($entries as $entry): ?>
          <tr>
            <td><?= (int)$entry['id'] ?></td>
            <td><?= htmlspecialchars($entry['smtp_host']) ?></td>
            <td><?= htmlspecialchars($entry['smtp_email']) ?></td>
            <td><?= (int)$entry['smtp_port'] ?></td>
            <td><?= strtoupper(htmlspecialchars($entry['smtp_secure'])) ?></td>
            <td>
              <?php if (!empty($entry['is_active'])): ?>
                <span class="badge bg-success">Aktiv</span>
              <?php else: ?>
                <span class="badge bg-secondary">Inaktiv</span>
              <?php endif; ?>
            </td>
            <td>
              <?= is_numeric($entry['max_global_20min'])
                  ? (int)$entry['max_global_20min']
                  : '<span class="text-muted">–</span>' ?>
            </td>
            <td>
              <a href="smtp_edit.php?id=<?= (int)$entry['id'] ?>"
                 class="btn btn-sm btn-outline-primary">
                <i class="tml tml-edit"></i> Bearbeiten
              </a>
              <a href="smtp_delete.php?id=<?= (int)$entry['id'] ?>"
                 class="btn btn-sm btn-outline-danger ms-1"
                 onclick="return confirm('Diesen SMTP-Zugang wirklich löschen?')">
                <i class="tml tml-trash"></i> Löschen
              </a>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  <?php endif; ?>
</div>

<?php
$pageContent = ob_get_clean();
include __DIR__ . '/template.php';


