<?php
// ✅ Start session before any output
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// ✅ Check admin login session
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header("Location: login.php");
    exit;
}


$templateFile = 'email-templates.json';

// Load templates
$templates = file_exists($templateFile)
    ? json_decode(file_get_contents($templateFile), true)
    : [
        "from_name" => "Your Company Name",
        "from_email" => "no-reply@yourdomain.com",
        "welcome" => ["subject" => "", "body" => ""],
        "referral" => ["subject" => "", "body" => ""],
        "lead" => ["subject" => "", "body" => ""]
    ];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $templates['from_name'] = trim($_POST['from_name']);
    $templates['from_email'] = trim($_POST['from_email']);
    $templates['base_url'] = trim($_POST['base_url']);
    
    $templates['smtp_host'] = trim($_POST['smtp_host']);
    $templates['smtp_user'] = trim($_POST['smtp_user']);
    $templates['smtp_pass'] = trim($_POST['smtp_pass']);
    $templates['smtp_port'] = (int) $_POST['smtp_port'];
    $templates['smtp_secure'] = trim($_POST['smtp_secure']);



    $templates['welcome']['subject'] = $_POST['welcome_subject'];
    $templates['welcome']['body'] = $_POST['welcome_body'];
    $templates['referral']['subject'] = $_POST['referral_subject'];
    $templates['referral']['body'] = $_POST['referral_body'];
    $templates['lead']['subject'] = $_POST['lead_subject'];
    $templates['lead']['body'] = $_POST['lead_body'];

    file_put_contents($templateFile, json_encode($templates, JSON_PRETTY_PRINT));
    $msg = "✅ Templates and settings saved.";
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Email Template Editor</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  <style> textarea { height: 200px; } </style>
</head>
<body style="display: flex; min-height: 100vh; margin: 0;">
 <div class="admin-sidebar" style="width:220px; background:#343a40; padding:20px;">
  <?php include 'google-translate-widget-front-end.php'; ?>
  <?php include 'admin-links.php'; ?>
</div>

  <div style="flex-grow: 1; padding: 20px;">
  <h2>Edit System Email Settings</h2>

  <?php if (isset($msg)) echo "<div class='alert alert-success'>$msg</div>"; ?>

  <form method="post">
<h4>Sender Settings</h4>
<label>From Name:</label>
<input class="form-control mb-2" name="from_name" value="<?= htmlspecialchars($templates['from_name']) ?>" required>

<label>From Email:</label>
<input class="form-control mb-2" name="from_email" value="<?= htmlspecialchars($templates['from_email']) ?>" required>

<label>Base Domain (no trailing slash):</label>
<input class="form-control mb-4" name="base_url" value="<?= htmlspecialchars($templates['base_url'] ?? '') ?>" placeholder="https://yourdomain.com" required>


<hr class="my-4">
<h4 style="color:#0d6efd;">SMTP Settings</h4>


<label>SMTP Host:</label>
<input class="form-control mb-2" name="smtp_host" value="<?= htmlspecialchars($templates['smtp_host'] ?? '') ?>" required>

<label>SMTP Username:</label>
<input class="form-control mb-2" name="smtp_user" value="<?= htmlspecialchars($templates['smtp_user'] ?? '') ?>" required>

<label>SMTP Password:</label>
<input class="form-control mb-2" name="smtp_pass" type="password" value="<?= htmlspecialchars($templates['smtp_pass'] ?? '') ?>" required>

<label>SMTP Port:</label>
<input class="form-control mb-2" name="smtp_port" type="number" value="<?= htmlspecialchars($templates['smtp_port'] ?? 465) ?>" required>

<label>SMTP Encryption:</label>
<select class="form-control mb-4" name="smtp_secure" required>
  <option value="ssl" <?= ($templates['smtp_secure'] ?? '') === 'ssl' ? 'selected' : '' ?>>SSL</option>
  <option value="tls" <?= ($templates['smtp_secure'] ?? '') === 'tls' ? 'selected' : '' ?>>TLS</option>
</select>


    <h4>Welcome Email</h4>
    <input class="form-control mb-2" name="welcome_subject" value="<?= htmlspecialchars($templates['welcome']['subject']) ?>" placeholder="Subject">
    <textarea class="form-control mb-4" name="welcome_body"><?= htmlspecialchars($templates['welcome']['body']) ?></textarea>

    <h4>Referral Alert (To Sponsor)</h4>
    <input class="form-control mb-2" name="referral_subject" value="<?= htmlspecialchars($templates['referral']['subject']) ?>" placeholder="Subject">
    <textarea class="form-control mb-4" name="referral_body"><?= htmlspecialchars($templates['referral']['body']) ?></textarea>

    <h4>Lead Notification (To Sponsor)</h4>
    <input class="form-control mb-2" name="lead_subject" value="<?= htmlspecialchars($templates['lead']['subject']) ?>" placeholder="Subject">
    <textarea class="form-control mb-4" name="lead_body"><?= htmlspecialchars($templates['lead']['body']) ?></textarea>

    <button class="btn btn-primary">Save Templates</button>
  </form>
  
  <?php include 'admin-footer.php'; ?>

</div>
</body>
</html>
