<?php
require __DIR__ . '/../_boot.php';
require __DIR__ . '/../partials/nav.php';
if (!$user || !(in_array($user['role_name'] ?? '', ['super_admin','event_manager'], true) || \App\Core\Auth::hasPermission('notifications.manage', $user) || \App\Core\Auth::hasPermission('backups.manage', $user))) {
    header('Location: dashboard.php'); exit;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Email Notifications & Backups | Pyrk Admin</title>
  <link rel="stylesheet" href="../assets/css/app.css">
  <link rel="stylesheet" href="../assets/css/admin_notifications.css?v=3">
</head>
<body>
<?php pyrk_render_nav('admin'); ?>
<main class="container enterprise-page notifications-admin-page">
  <section class="page-hero admin-hero dashboard-hero enterprise-hero">
    <div>
      <p class="eyebrow">Operations Center</p>
      <h1>Email notifications & database backups</h1>
      <p class="muted">Control every automated email, test delivery, monitor logs, and create safe database backups.</p>
    </div>
    <div class="hero-actions">
      <button class="btn" type="button" id="saveNotificationSettingsBtn">Save settings</button>
      <button class="btn btn-light" type="button" id="testEmailBtn">Send test email</button>
      <button class="btn btn-light" type="button" id="createBackupBtn">Create DB backup</button>
    </div>
  </section>

  <section class="grid three notification-kpis">
    <div class="stat stat-pro"><span>Email system</span><strong id="emailSystemState">—</strong></div>
    <div class="stat stat-pro"><span>Admin recipients</span><strong id="adminRecipientCount">0</strong></div>
    <div class="stat stat-pro"><span>Latest backup</span><strong id="latestBackupState">—</strong></div>
  </section>

  <section class="grid notification-layout">
    <aside class="panel notification-settings-panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Global settings</p>
          <h2>System controls</h2>
        </div>
      </div>

      <form id="notificationSettingsForm" class="stack-form">
        <label class="switch-card">
          <input type="checkbox" id="emailNotificationsEnabled">
          <span><strong>Email notifications enabled</strong><small>Turn this off to stop all automated emails except manual test logs.</small></span>
        </label>

        <label>Admin recipient emails
          <textarea id="emailAdminRecipients" placeholder="Optional. Separate emails with commas or new lines. If empty, active Super Admins and Event Managers are used."></textarea>
        </label>

        <label>Email footer text
          <textarea id="emailFooterText" placeholder="Footer shown in every email"></textarea>
        </label>


        <div class="smtp-settings-card">
          <div class="section-head tight">
            <div>
              <p class="eyebrow">Mail delivery</p>
              <h3>SMTP / PHP mail</h3>
              <p class="muted small">Use SMTP for real delivery. This version can send SMTP directly; PHPMailer/composer is optional.</p>
            </div>
          </div>

          <label>Mail driver
            <select id="mailDriver">
              <option value="log">Log only - no real email</option>
              <option value="smtp">SMTP - recommended</option>
              <option value="mail">PHP mail()</option>
            </select>
          </label>

          <div class="form-grid two">
            <label>From email<input id="mailFromEmail" type="email" placeholder="no-reply@yourdomain.com"></label>
            <label>From name<input id="mailFromName" placeholder="Pyrk Walking Club"></label>
          </div>

          <div id="smtpFields" class="smtp-fields">
            <div class="form-grid two">
              <label>SMTP host<input id="smtpHost" placeholder="smtp.yourdomain.com"></label>
              <label>SMTP port<input id="smtpPort" type="number" min="1" value="587"></label>
            </div>
            <div class="form-grid two">
              <label>SMTP username<input id="smtpUsername" autocomplete="off" placeholder="email@yourdomain.com"></label>
              <label>SMTP password<input id="smtpPassword" type="password" autocomplete="new-password" placeholder="Leave blank to keep saved password"></label>
            </div>
            <label>Encryption
              <select id="smtpEncryption">
                <option value="tls">TLS / STARTTLS</option>
                <option value="ssl">SSL</option>
                <option value="none">None</option>
              </select>
            </label>
            <p class="notice small" id="smtpSavedPasswordNote"></p>
          </div>
        </div>


        <label class="switch-card">
          <input type="checkbox" id="dbBackupEnabled">
          <span><strong>Database backup enabled</strong><small>Allow manual/cron backup jobs.</small></span>
        </label>

        <div class="form-grid two">
          <label>Retention days<input id="dbBackupRetentionDays" type="number" min="1" value="30"></label>
          <label>Backup path<input id="dbBackupPath" placeholder="Leave empty for storage/backups"></label>
        </div>

        <p id="notificationSettingsMsg" class="notice"></p>
      </form>
    </aside>

    <section class="panel notification-events-panel">
      <div class="section-head">
        <div>
          <p class="eyebrow">Automated emails</p>
          <h2>Event notification rules</h2>
          <p class="muted small">Choose which events send emails to admins and which also notify members.</p>
        </div>
        <div class="inline-controls">
          <button class="btn btn-light btn-small" type="button" id="enableAllEventsBtn">Enable all</button>
          <button class="btn btn-light btn-small" type="button" id="disableAllEventsBtn">Disable all</button>
        </div>
      </div>
      <div id="notificationEventsGrid" class="notification-events-grid"></div>
    </section>
  </section>

  <section class="grid two">
    <section class="panel">
      <div class="section-head">
        <div><p class="eyebrow">Delivery log</p><h2>Recent email activity</h2></div>
      </div>
      <div class="table-wrap">
        <table class="pro-table notification-log-table">
          <thead><tr><th>Event</th><th>Recipient</th><th>Subject</th><th>Status</th><th>Time</th></tr></thead>
          <tbody id="notificationLogsBody"></tbody>
        </table>
      </div>
    </section>

    <section class="panel">
      <div class="section-head">
        <div><p class="eyebrow">Database safety</p><h2>Backup history</h2></div>
      </div>
      <div class="table-wrap">
        <table class="pro-table backup-log-table">
          <thead><tr><th>File</th><th>Size</th><th>Status</th><th>Method</th><th>Time</th></tr></thead>
          <tbody id="backupLogsBody"></tbody>
        </table>
      </div>
    </section>
  </section>
</main>

<script>
window.PYRK_API_BASE='../../api';
window.PYRK_CSRF='<?= htmlspecialchars($csrf) ?>';
</script>
<script src="../assets/js/api.js"></script>
<script src="../assets/js/admin_notifications.js?v=3"></script>
</body>
</html>
