<?php ob_start();
/**
 * Admin View: Single Contact Form Submission
 */
$sub      = $submission ?? [];
$data     = $formData ?? [];
$csrfTok  = csrf_token();
?>

<div class="content-header">
    <div class="content-header-left">
        <a href="<?= admin_url('contact-form') ?>?tab=submissions" class="btn btn-ghost btn-sm" style="margin-bottom:.5rem;">
            <?= icon('arrow-left', 15) ?> <?= __('admin.contact_form.back_to_list') ?>
        </a>
        <h1><?= __('admin.contact_form.title') ?></h1>
    </div>
    <div class="content-header-actions">
        <form method="POST" action="<?= admin_url('contact-form/submission/' . $sub['id'] . '/delete') ?>">
            <?= csrf_field() ?>
            <button type="submit" class="btn btn-danger btn-sm"
                    data-confirm="<?= e(__('admin.contact_form.confirm_delete')) ?>">
                <?= icon('trash-2', 15) ?> <?= __('admin.common.delete') ?>
            </button>
        </form>
    </div>
</div>

<div class="detail-layout">

    <!-- ── Form data ── -->
    <div class="detail-main">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title"><?= icon('mail', 16) ?> Eingangsdaten</h3>
            </div>
            <div class="card-body">
                <?php if (empty($data)): ?>
                <p class="text-muted">Keine Daten vorhanden.</p>
                <?php else: ?>
                <dl class="cf-detail-list">
                    <?php foreach ($data as $entry):
                        $type  = $entry['type'] ?? 'text';
                        $label = $entry['label'] ?? '';
                        $value = $entry['value'] ?? '';
                        if ($type === 'hidden') continue; // skip hidden
                    ?>
                    <div class="cf-detail-item">
                        <dt><?= e($label) ?></dt>
                        <dd>
                            <?php if ($type === 'textarea'): ?>
                            <pre class="cf-detail-text"><?= e($value) ?></pre>
                            <?php elseif ($type === 'email'): ?>
                            <a href="mailto:<?= e($value) ?>"><?= e($value) ?></a>
                            <?php elseif ($type === 'tel'): ?>
                            <a href="tel:<?= e(preg_replace('/[^+\d]/', '', $value)) ?>"><?= e($value) ?></a>
                            <?php else: ?>
                            <?= e($value !== '' ? $value : '—') ?>
                            <?php endif; ?>
                        </dd>
                    </div>
                    <?php endforeach; ?>
                </dl>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- ── Meta info ── -->
    <div class="detail-sidebar">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title"><?= icon('info', 16) ?> Meta</h3>
            </div>
            <div class="card-body">
                <dl class="meta-list">
                    <div class="meta-item">
                        <dt>Datum</dt>
                        <dd><?= date('d.m.Y H:i', strtotime($sub['created_at'])) ?></dd>
                    </div>
                    <div class="meta-item">
                        <dt><?= __('admin.contact_form.ip') ?></dt>
                        <dd><?= e($sub['ip_address'] ?? '—') ?></dd>
                    </div>
                    <div class="meta-item">
                        <dt><?= __('admin.common.status') ?></dt>
                        <dd>
                            <?php if ($sub['is_read']): ?>
                            <span class="status-badge status-published"><?= __('admin.contact_form.status_read') ?></span>
                            <?php else: ?>
                            <span class="status-badge status-draft"><?= __('admin.contact_form.status_unread') ?></span>
                            <?php endif; ?>
                        </dd>
                    </div>
                    <?php if (!empty($sub['user_agent'])): ?>
                    <div class="meta-item">
                        <dt><?= __('admin.contact_form.user_agent') ?></dt>
                        <dd class="text-sm text-muted" style="word-break:break-all;"><?= e($sub['user_agent']) ?></dd>
                    </div>
                    <?php endif; ?>
                </dl>
            </div>
        </div>
    </div>

</div><!-- /.detail-layout -->
<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
