<?php
$page_title = 'Step 2 — List the allowlist';
require 'header.php';
?>

<p>
    The <strong>allowlist</strong> is the list of email addresses that are
    exempt from Mandrill's automatic rejection. When a recipient bounces
    hard or marks you as spam, Mandrill adds them to the <em>denylist</em>
    and silently drops future mail to them. If you're sure an address is
    legitimate (a colleague, a client who fixed their mailbox) you can
    add it to the allowlist to override that.
</p>

<p>This step hits <span class="endpoint">POST /allowlists/list</span> to show who's currently on it.</p>

<h2>Try it</h2>

<form method="post">
    <label>Optional search prefix
        <small>(start of an email, e.g. <code>john</code> to find <code>john@...</code>)</small>
    </label>
    <input type="text" name="email" value="<?= h($_POST['email'] ?? '') ?>" placeholder="leave blank to list everything">
    <button type="submit" name="run">Fetch allowlist</button>
</form>

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

    // --- Build the params ------------------------------------
    // Only send "email" if the user typed a prefix — otherwise the
    // endpoint returns up to 1000 entries.
    $params = [];
    if (!empty($_POST['email'])) {
        $params['email'] = trim($_POST['email']);
    }

    $result = mandrill_call('/allowlists/list', $params);
    // ---------------------------------------------------------

    if (!mandrill_is_error($result) && is_array($result['decoded'])) {
        $rows = $result['decoded'];
        if (count($rows) === 0) {
            echo '<div class="callout info">Allowlist is empty for that search.</div>';
        } else {
            echo '<h3>' . count($rows) . ' entries</h3>';
            echo '<table><tr><th>Email</th><th>Detail</th><th>Created</th></tr>';
            foreach ($rows as $row) {
                echo '<tr>';
                echo '<td>' . h($row['email']   ?? '') . '</td>';
                echo '<td>' . h($row['detail']  ?? '') . '</td>';
                echo '<td>' . h($row['created_at'] ?? '') . '</td>';
                echo '</tr>';
            }
            echo '</table>';
        }
    }

    render_debug($result);
}
?>

<h2>The call</h2>

<pre><code>$result = mandrill_call('/allowlists/list', [
    'email' =&gt; 'john',   // optional prefix filter
]);

foreach ($result['decoded'] as $row) {
    echo $row['email'] . ' — added ' . $row['created_at'];
}</code></pre>

<h2>Response shape</h2>

<p>An array of entries, each with three fields:</p>

<pre><code>[
    {
        "email":      "user@example.com",
        "detail":     "optional note you passed when adding",
        "created_at": "2024-10-15 14:32:01"
    },
    ...
]</code></pre>

<p><a href="03-allowlist-add.php">→ Next: add an email to the allowlist</a></p>

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