<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/functions.php';
startSecureSession();
requireAdmin();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = (int)$_POST['id'];
    $subject = sanitize($_POST['subject'] ?? '');
    $body = strip_tags($_POST['body'] ?? '', '<h1><h2><h3><p><br><a><strong><em><ul><ol><li>');
    Database::execute("UPDATE email_templates SET subject=?, body=? WHERE id=?", [$subject, $body, $id]);
    $_SESSION['flash']['success'] = 'Email template saved.';
}
header('Location: /admin/email-templates.php?edit=' . $id);
exit;
