<?php
$page_title = 'Step 6 — List templates';
require 'header.php';
?>

<p>
    <strong>Templates</strong> let you design an email once (HTML, subject,
    sender) and then reuse it with different merge variables every time you
    send. You can create them in the Mandrill dashboard UI, or via the API
    (Step 7). Either way, this endpoint —
    <span class="endpoint">POST /templates/list</span> — shows you all the
    templates on your account.
</p>

<h2>Try it</h2>

<form method="post">
    <label>Optional label filter
        <small>(return only templates tagged with this label)</small>
    </label>
    <input type="text" name="label" value="<?= h($_POST['label'] ?? '') ?>" placeholder="leave blank for all">
    <button type="submit" name="run">Fetch templates</button>
</form>

<?php
if (isset($_POST['run']) && is_configured()) {

    // --- The call --------------------------------------------
    $params = [];
    if (!empty($_POST['label'])) {
        $params['label'] = trim($_POST['label']);
    }
    $result = mandrill_call('/templates/list', $params);
    // ---------------------------------------------------------

    if (!mandrill_is_error($result) && is_array($result['decoded'])) {
        $rows = $result['decoded'];
        if (count($rows) === 0) {
            echo '<div class="callout info">No templates found. '
               . '<a href="07-templates-add.php">Create one</a>.</div>';
        } else {
            echo '<h3>' . count($rows) . ' templates</h3>';
            echo '<table><tr><th>Slug</th><th>Name</th><th>Subject</th><th>From</th><th>Updated</th></tr>';
            foreach ($rows as $row) {
                echo '<tr>';
                echo '<td><code>' . h($row['slug'] ?? '')       . '</code></td>';
                echo '<td>'       . h($row['name'] ?? '')       . '</td>';
                echo '<td>'       . h($row['subject'] ?? '')    . '</td>';
                echo '<td>'       . h($row['from_email'] ?? '') . '</td>';
                echo '<td>'       . h($row['updated_at'] ?? '') . '</td>';
                echo '</tr>';
            }
            echo '</table>';
        }
    }

    render_debug($result);
}
?>

<h2>The call</h2>

<pre><code>$result = mandrill_call('/templates/list', [
    'label' =&gt; 'welcome-flow',  // optional filter
]);

foreach ($result['decoded'] as $tpl) {
    echo $tpl['slug'] . ' — ' . $tpl['name'];
}</code></pre>

<h2>Response shape</h2>

<pre><code>[
    {
        "slug":       "welcome-email",
        "name":       "Welcome Email",
        "labels":     ["welcome-flow"],
        "code":       "&lt;h1&gt;Welcome, *|FIRSTNAME|*&lt;/h1&gt;...",
        "subject":    "Welcome to our service",
        "from_email": "noreply@yourdomain.com",
        "from_name":  "Your Company",
        "text":       "Welcome, *|FIRSTNAME|*...",
        "publish_name":       "welcome-email",
        "publish_code":       "&lt;h1&gt;Welcome, *|FIRSTNAME|*&lt;/h1&gt;...",
        "publish_subject":    "Welcome to our service",
        "publish_from_email": "noreply@yourdomain.com",
        "publish_from_name":  "Your Company",
        "publish_text":       "Welcome, *|FIRSTNAME|*...",
        "published_at":       "2024-10-15 09:22:14",
        "created_at":         "2024-10-10 11:03:00",
        "updated_at":         "2024-10-15 09:22:14"
    },
    ...
]</code></pre>

<h2>Draft vs. published — what the two copies mean</h2>

<p>
    Notice each template has two versions of every field — one plain
    (<code>code</code>, <code>subject</code>…) and one prefixed
    <code>publish_</code>. That's because templates have a <strong>draft / published</strong>
    split:
</p>

<ul>
    <li>The plain fields are the <strong>draft</strong> — whatever you last saved.</li>
    <li>The <code>publish_*</code> fields are the <strong>currently live</strong> version that sends will actually use.</li>
</ul>

<p>
    You edit the draft freely, and then call <code>/templates/publish</code>
    to promote it to live. This prevents an in-progress edit from accidentally
    going out on the next send.
</p>

<p>
    The key identifier when you want to send with a template is
    <code>slug</code> — a URL-safe version of the name. That's what
    <code>/messages/send-template</code> (next page) wants.
</p>

<p><a href="07-templates-add.php">→ Next: create a template and send with it</a></p>

<?php require 'footer.php'; ?>
