<?php
$B = htmlspecialchars(rtrim((string) ($baseUrl ?? ''), '/'));
if (!function_exists('pp_where')) {
    function pp_where(string $path, string $lang = 'php'): string {
        $l = $lang === 'js' ? 'JS' : 'PHP';
        return '<div class="help-where"><span class="help-where-tag"><i class="bi bi-geo-alt-fill"></i> Where</span>'
             . '<span class="help-where-path">' . $path . '</span>'
             . '<span class="help-where-lang ' . htmlspecialchars($lang) . '">' . $l . '</span></div>';
    }
}
?>
<h1><i class="bi bi-printer-fill"></i> Print &amp; PDF pages</h1>
<p class="lead">A print page is a clean, branded rendering of one record; a PDF page is the same content saved as a
file. Both are read-only, so — like the View page — you <em>read and style</em> displayed values. Each recipe says
<strong>exactly where the code goes</strong>.</p>

<h2>Events these pages fire</h2>
<table class="help-table">
    <thead><tr><th>Event</th><th>Where</th><th>Use for</th></tr></thead>
    <tbody>
        <tr><td>On Print</td><td>Table → <b>Page Events</b> (PHP)</td><td>Add computed values / adjust data before the printable page renders.</td></tr>
        <tr><td>Process Record Values</td><td>Table → <b>Page Events</b> (PHP)</td><td>Format a single value for the printout.</td></tr>
        <tr><td>On Page Init</td><td>Table → <b>Client Events</b> (JS)</td><td>Auto-print, tweak the layout, or trigger a download.</td></tr>
    </tbody>
</table>

<h2>Recipes</h2>

<h3>1 · Add a computed value to the printout</h3>
<?= pp_where('Events → <b>invoices</b> → Page Events → <b>On Print</b>', 'php') ?>
<pre><code>$data['amount_due'] = (float) $data['total'] - (float) $data['paid'];
$data['printed_at'] = now();</code></pre>

<h3>2 · Format money / dates for the printed copy</h3>
<?= pp_where('Events → <b>invoices</b> → Page Events → <b>Process Record Values</b>', 'php') ?>
<pre><code>if (isset($data['total'])) $data['total'] = '$' . number_format((float) $data['total'], 2);
if (!empty($data['due_date'])) $data['due_date'] = date('j M Y', strtotime($data['due_date']));</code></pre>

<h3>3 · Auto-open the print dialog</h3>
<?= pp_where('Events → <b>invoices</b> → Client Events → <b>On Page Init</b>', 'js') ?>
<pre><code>setTimeout(function () { window.print(); }, 400);   // pop the browser print dialog</code></pre>

<h3>4 · Read a shown value</h3>
<?= pp_where('Events → <b>invoices</b> → Client Events → <b>On Page Init</b>', 'js') ?>
<pre><code>var ref = Runner.getControl('reference').getValue();   // getValue() reads the printed text
document.title = 'Invoice ' + ref;</code></pre>

<h3>5 · A “Save as PDF” button on the View page</h3>
<p>Drop a <a href="<?= $B ?>/help/component_custom_button">Custom&nbsp;Button</a>. The app ships a server-side PDF
endpoint per record — link straight to it, or generate + email it.</p>
<?= pp_where('Designer → Custom Button → <b>Client After</b>', 'js') ?>
<pre><code>// open the record's PDF in a new tab (PP_BASE + the generated pdf route)
window.open((window.PP_BASE || '') + '/generated/invoices/pdf.php?id=' + pp.page.id, '_blank');</code></pre>

<h3>6 · Email a generated PDF after saving</h3>
<?= pp_where('Events → <b>invoices</b> → Page Events → <b>After Add Save</b>', 'php') ?>
<pre><code>// Build the record's HTML, render it to a PDF file with PdfService, then email it.
$html = '&lt;h1&gt;Invoice ' . htmlspecialchars((string) $data['reference']) . '&lt;/h1&gt;'
      . '&lt;p&gt;Total: ' . number_format((float) $data['total'], 2) . '&lt;/p&gt;';
$file = \PpPhp\Core\PdfService::save($html, sys_get_temp_dir() . "/invoice-$id.pdf");
// attach $file with your mailer — see the PDF + Email guides for the details.</code></pre>

<div class="help-callout help-callout-info">
    <i class="bi bi-file-earmark-pdf"></i>
    <div>The read/style API (<code>Runner.getControl(field).getValue()</code>, <code>.addStyle()</code>) is the same
        one you use on View. Full server-side PDF options are in the
        <a href="<?= $B ?>/help/PDF">PDF guide</a>.</div>
</div>
