<?php
require_once __DIR__ . '/config.php';
requireAdmin();
$pdo = getDB();

$keys = ['smtp_enabled', 'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_user', 'smtp_pass', 'smtp_from_email', 'smtp_from_name', 'smtp_notify_email'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare("INSERT INTO site_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    foreach ($keys as $k) {
        $v = isset($_POST[$k]) ? trim($_POST[$k]) : '';
        if ($k === 'smtp_enabled') $v = isset($_POST['smtp_enabled']) ? '1' : '0';
        $stmt->execute([$k, $v, $v]);
    }
    header('Location: smtp.php?saved=1');
    exit;
}

$settings = [];
foreach ($keys as $k) $settings[$k] = getSetting($k, $k === 'smtp_port' ? '587' : ($k === 'smtp_encryption' ? 'tls' : ($k === 'smtp_from_name' ? 'Fizasite' : '')));
$pageTitle = 'SMTP mail';
require_once __DIR__ . '/includes/header.php';
?>
<div class="admin-page">
    <h1>SMTP mail</h1>
    <p>When enabled, new <strong>enquiry</strong> and <strong>contact</strong> submissions send a notification email to <em>Notify email</em> (or contact email if empty).</p>
    <?php if (isset($_GET['saved'])): ?><p class="success">Saved.</p><?php endif; ?>
    <form method="post" class="admin-form" style="max-width:560px;">
        <label><input type="checkbox" name="smtp_enabled" value="1" <?php echo $settings['smtp_enabled'] === '1' ? 'checked' : ''; ?>> Enable SMTP</label>
        <label>SMTP host</label>
        <input type="text" name="smtp_host" value="<?php echo htmlspecialchars($settings['smtp_host']); ?>" placeholder="smtp.gmail.com">
        <label>Port</label>
        <input type="text" name="smtp_port" value="<?php echo htmlspecialchars($settings['smtp_port']); ?>" placeholder="587">
        <label>Encryption</label>
        <select name="smtp_encryption">
            <option value="tls" <?php echo $settings['smtp_encryption'] === 'tls' ? 'selected' : ''; ?>>TLS (STARTTLS)</option>
            <option value="ssl" <?php echo $settings['smtp_encryption'] === 'ssl' ? 'selected' : ''; ?>>SSL</option>
            <option value="" <?php echo $settings['smtp_encryption'] === '' ? 'selected' : ''; ?>>None</option>
        </select>
        <label>Username</label>
        <input type="text" name="smtp_user" value="<?php echo htmlspecialchars($settings['smtp_user']); ?>" autocomplete="off">
        <label>Password</label>
        <input type="password" name="smtp_pass" value="<?php echo htmlspecialchars($settings['smtp_pass']); ?>" placeholder="App password if Gmail" autocomplete="new-password">
        <label>From email *</label>
        <input type="email" name="smtp_from_email" value="<?php echo htmlspecialchars($settings['smtp_from_email']); ?>" placeholder="noreply@yourdomain.com">
        <label>From name</label>
        <input type="text" name="smtp_from_name" value="<?php echo htmlspecialchars($settings['smtp_from_name']); ?>">
        <label>Notify email (receives enquiry/contact)</label>
        <input type="email" name="smtp_notify_email" value="<?php echo htmlspecialchars($settings['smtp_notify_email']); ?>" placeholder="Leave empty to use contact email from Settings">
        <button type="submit" class="btn">Save</button>
    </form>
    <p style="margin-top:24px;color:#64748b;font-size:0.9rem;"><strong>Gmail:</strong> use App Password, port 587, TLS. <strong>Host:</strong> smtp.gmail.com</p>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
