<?php
function et_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$outbox = $outbox ?? [];
$templates = $templates ?? [];
$resetTokens = $resetTokens ?? [];
$diagnostics = $diagnostics ?? [];

$success = $_SESSION['email_test_success'] ?? '';
$error = $_SESSION['email_test_error'] ?? '';
unset($_SESSION['email_test_success'], $_SESSION['email_test_error']);
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1"><i class="fa fa-envelope"></i> Email Test Center</h3>
            <div class="opacity-75">Test reset password delivery, inspect queued emails, and diagnose PHP mail.</div>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= et_h($success) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= et_h($error) ?></div>
    <?php endif; ?>

    
    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">SMTP Settings</h5>

            <form method="post" action="/easychair/admin/email-test/save-smtp" class="row g-2">
                <div class="col-md-2">
                    <label class="form-label fw-bold">Enabled</label>
                    <select name="smtp_enabled" class="form-select">
                        <option value="1" <?= (($smtpSettings['smtp_enabled'] ?? '1') === '1') ? 'selected' : '' ?>>Yes</option>
                        <option value="0" <?= (($smtpSettings['smtp_enabled'] ?? '1') === '0') ? 'selected' : '' ?>>No</option>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">Host</label>
                    <input name="smtp_host" class="form-control" value="<?= et_h($smtpSettings['smtp_host'] ?? '') ?>">
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold">Port</label>
                    <input name="smtp_port" class="form-control" value="<?= et_h($smtpSettings['smtp_port'] ?? '587') ?>">
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold">Security</label>
                    <select name="smtp_security" class="form-select">
                        <?php foreach(['tls','ssl','none'] as $sec): ?>
                            <option value="<?= et_h($sec) ?>" <?= (($smtpSettings['smtp_security'] ?? 'tls') === $sec) ? 'selected' : '' ?>>
                                <?= et_h($sec) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">Username</label>
                    <input name="smtp_username" class="form-control" value="<?= et_h($smtpSettings['smtp_username'] ?? '') ?>">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">Password</label>
                    <input type="password" name="smtp_password" class="form-control" placeholder="Leave blank to keep current">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">From Email</label>
                    <input name="smtp_from_email" class="form-control" value="<?= et_h($smtpSettings['smtp_from_email'] ?? '') ?>">
                </div>

                <div class="col-md-4">
                    <label class="form-label fw-bold">From Name</label>
                    <input name="smtp_from_name" class="form-control" value="<?= et_h($smtpSettings['smtp_from_name'] ?? 'EasyChair Conference System') ?>">
                </div>

                <div class="col-md-2 d-flex align-items-end">
                    <button class="btn btn-primary w-100">Save SMTP</button>
                </div>
            </form>
        </div>
    </div>

<div class="row g-4 mb-4">
        <div class="col-lg-5">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Send Test Email</h5>

                    <form method="post" action="/easychair/admin/email-test/send">
                        <label class="form-label fw-bold">Recipient Email</label>
                        <input type="email" name="email" class="form-control mb-3" value="shquier@gmail.com" required>

                        <label class="form-label fw-bold">Subject</label>
                        <input name="subject" class="form-control mb-3" value="EasyChair Email Test">

                        <label class="form-label fw-bold">Body</label>
                        <textarea name="body" rows="6" class="form-control mb-3">This is a test email from EasyChair.

If you receive this, PHP mail delivery is working.</textarea>

                        <button class="btn btn-success w-100 rounded-pill fw-bold">Send Test</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-7">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Mail Diagnostics</h5>

                    <table class="table table-bordered">
                        <tbody>
                        <?php foreach ($diagnostics as $k => $v): ?>
                            <tr>
                                <th><?= et_h($k) ?></th>
                                <td><?= et_h($v) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>

                    <div class="alert alert-warning small mb-0">
                        If PHP mail() returns success but Gmail does not receive the email, check server mail logs,
                        spam, SPF/DKIM/DMARC, and whether postfix/sendmail is configured for www.jpu.edu.jo.
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">System Outbox</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Template</th>
                        <th>Recipient</th>
                        <th>Subject</th>
                        <th>Status</th>
                        <th>Error</th>
                        <th>Created</th>
                        <th>Sent</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($outbox as $m): ?>
                        <tr>
                            <td><?= (int)$m['id'] ?></td>
                            <td><span class="badge bg-primary"><?= et_h($m['template_key']) ?></span></td>
                            <td><?= et_h($m['recipient_email']) ?></td>
                            <td style="min-width:260px;"><?= et_h($m['subject']) ?></td>
                            <td>
                                <?php
                                $class = match($m['status']) {
                                    'sent' => 'bg-success',
                                    'failed' => 'bg-danger',
                                    'cancelled' => 'bg-secondary',
                                    default => 'bg-warning text-dark',
                                };
                                ?>
                                <span class="badge <?= $class ?>"><?= et_h($m['status']) ?></span>
                            </td>
                            <td class="small text-danger"><?= et_h($m['error_message']) ?></td>
                            <td><?= et_h($m['created_at']) ?></td>
                            <td><?= et_h($m['sent_at']) ?></td>
                            <td>
                                <a class="btn btn-sm btn-primary" href="/easychair/admin/email-test/resend?id=<?= (int)$m['id'] ?>">Resend</a>
                                <a class="btn btn-sm btn-outline-success" href="/easychair/admin/email-test/mark-sent?id=<?= (int)$m['id'] ?>">Mark Sent</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$outbox): ?>
                        <tr><td colspan="9" class="text-center text-muted py-4">No system emails found.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Reset Tokens</h5>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                            <tr>
                                <th>ID</th>
                                <th>Email</th>
                                <th>Expires</th>
                                <th>Used</th>
                                <th>Created</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($resetTokens as $t): ?>
                                <tr>
                                    <td><?= (int)$t['id'] ?></td>
                                    <td><?= et_h($t['email']) ?></td>
                                    <td><?= et_h($t['expires_at']) ?></td>
                                    <td><?= et_h($t['used_at']) ?></td>
                                    <td><?= et_h($t['created_at']) ?></td>
                                </tr>
                            <?php endforeach; ?>

                            <?php if (!$resetTokens): ?>
                                <tr><td colspan="5" class="text-center text-muted py-4">No reset tokens found.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card border-0 shadow-sm" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">System Templates</h5>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                            <tr>
                                <th>Key</th>
                                <th>Subject EN</th>
                                <th>Active</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($templates as $tpl): ?>
                                <tr>
                                    <td><span class="badge bg-primary"><?= et_h($tpl['template_key']) ?></span></td>
                                    <td><?= et_h($tpl['subject_en']) ?></td>
                                    <td><?= (int)$tpl['is_active'] === 1 ? 'Yes' : 'No' ?></td>
                                </tr>
                            <?php endforeach; ?>

                            <?php if (!$templates): ?>
                                <tr><td colspan="3" class="text-center text-muted py-4">No templates found.</td></tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
    </div>

</div>
