<?php
// includes/functions/emails/templates/submission-template.php

// Vars provided by the loader callback
$task_name      = $task_name      ?? 'Request';
$request_number = $request_number ?? '';              // we map this to job_number in the loader
$comms_link     = $comms_link     ?? '';
$fields_html    = $fields_html    ?? '';              // already filtered/whitelisted

// Subject/body templates from options
$subject_template = get_option('afb_email_subject', 'Thanks for your request {{request_number}}');

// Default body keeps it simple; you can fully override in options
$default_body_content = 'We’ve received your submission and will review it within 1–3 business days.<br><br>You can follow it here: {{comms_link}}';
$custom_body_content  = get_option('afb_email_body', $default_body_content);

// Heading + body (tokens replaced)
$placeholders = ['{{task_name}}', '{{request_number}}', '{{comms_link}}', '{{fields_html}}'];
$replacements = [$task_name,       $request_number,      $comms_link,      $fields_html];

$subject = str_replace($placeholders, $replacements, $subject_template);
$body    = '<h2 style="margin:0 0 12px;">' . esc_html($task_name) . '</h2>' .
           str_replace($placeholders, $replacements, $custom_body_content);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title><?= esc_html($subject) ?></title>
  <style>
    body { font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Arial,sans-serif; background:#f9fafb; padding:32px; color:#1f2937; }
    .container { background:#fff; border-radius:6px; padding:32px; max-width:600px; margin:0 auto; box-shadow:0 1px 3px rgba(0,0,0,0.05); }
    .content { font-size:15px; line-height:1.6; }
    .footer  { margin-top:24px; font-size:13px; color:#6b7280; text-align:center; }
    .btn     { display:inline-block;padding:10px 20px;background:#2563eb;color:#fff;border-radius:4px;text-decoration:none;font-weight:600; }
  </style>
</head>
<body>
  <div class="container">
    <div class="content">
      <?= wp_kses_post($body) ?>
    </div>

    <?php if ($comms_link): ?>
    <div class="footer">
      <p style="margin:0 0 8px;">If you have an Asana account, you can view the task here:</p>
      <p style="margin:0;"><a class="btn" href="<?= esc_url($comms_link) ?>">View Task in Asana</a></p>
    </div>
    <?php endif; ?>
  </div>
</body>
</html>
