<?php
$pageTitle = 'Dashboard';
$currentPage = 'dashboard';
require_once APP_PATH . '/views/layouts/header.php';
?>
<div style="max-width:1200px">
<!-- Stats -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:1.25rem;margin-bottom:1.75rem">
<?php $statCards = [
    ['label'=>'Total Forms','value'=>$stats['total_forms']??0,'icon'=>'fa-wpforms','color'=>'#4f46e5','bg'=>'#e0e7ff'],
    ['label'=>'Responses','value'=>$stats['total_responses']??0,'icon'=>'fa-inbox','color'=>'#059669','bg'=>'#d1fae5'],
    ['label'=>'Funnels','value'=>$stats['total_funnels']??0,'icon'=>'fa-filter','color'=>'#d97706','bg'=>'#fef3c7'],
    ['label'=>'Fields','value'=>$stats['total_fields']??0,'icon'=>'fa-list-ul','color'=>'#7c3aed','bg'=>'#f3e8ff'],
];
foreach ($statCards as $s): ?>
<div class="card" style="padding:1.25rem;display:flex;align-items:center;gap:1rem">
    <div style="width:48px;height:48px;background:<?= $s['bg'] ?>;border-radius:.875rem;display:flex;align-items:center;justify-content:center;flex-shrink:0">
        <i class="fas <?= $s['icon'] ?>" style="color:<?= $s['color'] ?>;font-size:1.1rem"></i>
    </div>
    <div>
        <div style="font-size:1.75rem;font-weight:800;color:#111827;line-height:1"><?= $s['value'] ?></div>
        <div style="font-size:.75rem;color:#6b7280;margin-top:.25rem;font-weight:500"><?= $s['label'] ?></div>
    </div>
</div>
<?php endforeach; ?>
</div>

<div style="display:grid;grid-template-columns:1fr 1fr;gap:1.25rem">
<!-- Recent Forms -->
<div class="card" style="padding:1.5rem">
    <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:1.25rem">
        <h2 style="font-weight:700;color:#111827;font-size:.95rem;margin:0">Recent Forms</h2>
        <a href="<?= url('/forms') ?>" class="btn-secondary btn-sm">View All</a>
    </div>
    <?php if (empty($recentForms)): ?>
        <div style="text-align:center;padding:2rem;color:#9ca3af">
            <i class="fas fa-wpforms" style="font-size:2rem;margin-bottom:.75rem;display:block;color:#d1d5db"></i>
            <p style="font-size:.875rem;margin:0 0 1rem">No forms yet.</p>
            <a href="<?= url('/forms/create') ?>" class="btn-primary" style="font-size:.75rem">+ Create First Form</a>
        </div>
    <?php else: ?>
        <div style="display:flex;flex-direction:column;gap:.625rem">
        <?php foreach ($recentForms as $form): ?>
            <div style="display:flex;align-items:center;justify-content:space-between;padding:.75rem;background:#f9fafb;border-radius:.75rem;border:1px solid #f3f4f6">
                <div style="min-width:0;flex:1">
                    <div style="font-weight:600;font-size:.875rem;color:#111827;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><?= e($form['name']) ?></div>
                    <div style="font-size:.7rem;color:#9ca3af;margin-top:.125rem;font-family:'JetBrains Mono',monospace">/form/<?= e($form['slug']) ?></div>
                </div>
                <div style="display:flex;gap:.5rem;flex-shrink:0;margin-left:.75rem">
                    <a href="<?= url('/forms/'.$form['id'].'/edit') ?>" class="btn-secondary btn-sm"><i class="fas fa-edit"></i></a>
                    <button onclick="copyToClipboard('<?= url('/form/'.$form['slug']) ?>')" class="btn-secondary btn-sm" title="Copy link"><i class="fas fa-link"></i></button>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<!-- Recent Responses -->
<div class="card" style="padding:1.5rem">
    <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:1.25rem">
        <h2 style="font-weight:700;color:#111827;font-size:.95rem;margin:0">Recent Responses</h2>
    </div>
    <?php if (empty($recentResponses)): ?>
        <div style="text-align:center;padding:2rem;color:#9ca3af">
            <i class="fas fa-inbox" style="font-size:2rem;margin-bottom:.75rem;display:block;color:#d1d5db"></i>
            <p style="font-size:.875rem;margin:0">No responses yet.</p>
            <p style="font-size:.75rem;margin:.25rem 0 0;color:#d1d5db">Share your form links to start collecting.</p>
        </div>
    <?php else: ?>
        <div style="display:flex;flex-direction:column;gap:.625rem">
        <?php foreach ($recentResponses as $r): ?>
            <div style="display:flex;align-items:center;justify-content:space-between;padding:.75rem;background:#f9fafb;border-radius:.75rem;border:1px solid #f3f4f6">
                <div>
                    <div style="font-weight:600;font-size:.875rem;color:#111827"><?= e($r['form_name']) ?></div>
                    <div style="font-size:.7rem;color:#9ca3af;margin-top:.125rem"><?= timeAgo($r['created_at']) ?> &bull; <?= e($r['ip_address']) ?></div>
                </div>
                <span class="badge badge-green" style="font-size:.65rem">New</span>
            </div>
        <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
</div>

<!-- Quick Actions -->
<div class="card" style="padding:1.5rem;margin-top:1.25rem">
    <h2 style="font-weight:700;color:#111827;font-size:.95rem;margin:0 0 1rem">Quick Actions</h2>
    <div style="display:flex;gap:.75rem;flex-wrap:wrap">
        <a href="<?= url('/forms/create') ?>" class="btn-primary"><i class="fas fa-plus"></i> New Form</a>
        <a href="<?= url('/funnels/create') ?>" class="btn-primary" style="background:#7c3aed"><i class="fas fa-filter"></i> New Funnel</a>
        <a href="<?= url('/forms') ?>" class="btn-secondary"><i class="fas fa-wpforms"></i> All Forms</a>
        <a href="<?= url('/funnels') ?>" class="btn-secondary"><i class="fas fa-filter"></i> All Funnels</a>
    </div>
</div>
</div>
<?php require_once APP_PATH . '/views/layouts/footer.php'; ?>
