<?php
// emails/submission-template.php

// Ensure variables are defined
$task_name      = $task_name      ?? '';
$request_number = $request_number ?? '';
$comms_link     = $comms_link     ?? '';
$fields_html    = $fields_html    ?? '';
$email_template_tokens = isset($email_template_tokens) && is_array($email_template_tokens)
  ? $email_template_tokens
  : [];

// Fetch subject/body templates
$subject_template = isset($subject_tpl) && is_string($subject_tpl) && trim($subject_tpl) !== ''
  ? $subject_tpl
  : get_option('afb_email_subject', 'Your request has been received');

// Construct the email body with the task name as a heading
// We'll use a default if 'afb_email_body' is not set, but prioritize the task name.
$default_body_content = 'Thanks for submitting your request. You can follow it here: {{comms_link}}';
$custom_body_content = isset($body_tpl) && is_string($body_tpl) && trim($body_tpl) !== ''
  ? $body_tpl
  : get_option('afb_email_body', $default_body_content);

// Add the task name as a heading to the email body
// Changed this line to include "Asana Task: "
$email_body_with_heading = '<h2>Asana Task: ' . esc_html($task_name) . '</h2>' . $custom_body_content;

$token_values = [
  'task_name' => $task_name,
  'request_number' => $request_number,
  'comms_link' => $comms_link,
  'fields_html' => $fields_html,
];
if (!empty($email_template_tokens)) {
  $token_values = array_merge($token_values, $email_template_tokens);
}

if (function_exists('afb_email_expand_placeholders')) {
  $email_body = afb_email_expand_placeholders($email_body_with_heading, $token_values);
  $title_text = afb_email_expand_placeholders($subject_template, $token_values);
} else {
  $legacy_map = [];
  foreach ($token_values as $k => $v) {
    $legacy_map['{{' . $k . '}}'] = (string) $v;
    $legacy_map['{' . $k . '}'] = (string) $v;
  }
  $email_body = strtr((string) $email_body_with_heading, $legacy_map);
  $title_text = strtr((string) $subject_template, $legacy_map);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title><?= esc_html($title_text) ?></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); }
    .email-head { background:linear-gradient(135deg,#0f2f5f,#1d4f91); border-radius:8px; padding:16px 18px; margin:0 0 18px; }
    .email-kicker { margin:0 0 6px; color:rgba(255,255,255,.75); font-size:12px; letter-spacing:.08em; text-transform:uppercase; font-weight:700; }
    .email-title { margin:0; color:#fff; font-size:18px; line-height:1.35; font-weight:700; }
    /* .logo related styles are no longer needed, but keeping them here for reference if you want to re-add them */
    /* .logo { text-align:center; margin-bottom:32px; } */
    /* .logo img { max-width: 120px; height: auto; } */
    /* .logo img { height:100px; } */
    .content { font-size:15px; line-height:1.6; }
    .footer  { margin-top:32px; font-size:13px; color:#6b7280; text-align:center; }
    .link    { color:#2563eb; text-decoration:underline; }
    h2 { font-size: 20px; margin-bottom: 16px; color: #1f2937; } /* Styling for the new heading */
  </style>
</head>
<body>
  <div class="container">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;border-radius:8px;margin:0 0 18px;">
      <tr><td bgcolor="#1d4f91" style="background-color:#1d4f91;padding:16px 18px;border-radius:8px;">
        <p style="margin:0 0 6px;color:rgba(255,255,255,.8);font-size:12px;letter-spacing:.08em;text-transform:uppercase;font-weight:700;">Corporate Services Division</p>
        <h1 style="margin:0;color:#ffffff;font-size:18px;line-height:1.35;font-weight:700;"><?= esc_html($task_name ?: 'Request Received') ?></h1>
      </td></tr>
    </table>
    <?php /* Removed the logo div entirely */ ?>

    <div class="content">
      <?= wp_kses_post($email_body) ?>
    </div>

    <div class="footer">
        <p>If you have an Asana account, you can use the link below to view the task.</p>
      <p style="margin:0;">
        <a href="<?= esc_url($comms_link) ?>"
           class="link"
           style="display:inline-block;padding:10px 20px;background:#2563eb;
                  color:#ffffff;border-radius:4px;text-decoration:none;font-weight:600;">
          View Task in Asana
        </a>
      </p>
    </div>
  </div>
</body>
</html>