<?php
/**
 * Admin: view last logged quote email preview (sandbox / debug).
 * GET: ?folder_id=1  (optional — defaults to latest send)
 */
require __DIR__ . '/config.php';
require_once __DIR__ . '/admin_config_helpers.php';

$folder_id = isset($_GET['folder_id']) ? (int) $_GET['folder_id'] : 0;
$paths = admin_quote_preview_paths($folder_id);

$htmlFile = '';
if ($folder_id > 0 && is_file($paths['folder_html'])) {
    $htmlFile = $paths['folder_html'];
} elseif (is_file($paths['latest_html'])) {
    $htmlFile = $paths['latest_html'];
}

if ($htmlFile !== '' && is_readable($htmlFile)) {
    header('Content-Type: text/html; charset=utf-8');
    readfile($htmlFile);
    exit;
}

header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>Quote preview</title></head><body>';
echo '<p>No quote email preview logged yet. Send a quote from a project folder first.</p>';
if ($folder_id > 0) {
    echo '<p><a href="folder_detail.php?id=' . (int) $folder_id . '">Back to project</a></p>';
}
echo '</body></html>';
