<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex align-items-center justify-content-between mb-4">
    <h4 class="page-title mb-0"><i class="bi bi-speedometer2 me-2 text-primary"></i><?= lang('Admin.dashboard') ?></h4>
    <small class="text-muted"><?= date('D, d M Y') ?></small>
</div>

<!-- Stats Cards -->
<div class="row g-3 mb-4">
    <div class="col-sm-6 col-xl-3">
        <div class="card stat-card border-0 shadow-sm h-100">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="stat-icon bg-primary-subtle text-primary rounded-3">
                    <i class="bi bi-people-fill fs-4"></i>
                </div>
                <div>
                    <div class="stat-number fw-bold fs-3"><?= number_format($totalProfiles) ?></div>
                    <div class="stat-label text-muted small"><?= lang('Admin.total_profiles') ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-xl-3">
        <div class="card stat-card border-0 shadow-sm h-100">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="stat-icon bg-success-subtle text-success rounded-3">
                    <i class="bi bi-person-check-fill fs-4"></i>
                </div>
                <div>
                    <div class="stat-number fw-bold fs-3"><?= number_format($activeProfiles) ?></div>
                    <div class="stat-label text-muted small"><?= lang('Admin.active_profiles') ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-xl-3">
        <div class="card stat-card border-0 shadow-sm h-100">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="stat-icon bg-info-subtle text-info rounded-3">
                    <i class="bi bi-qr-code fs-4"></i>
                </div>
                <div>
                    <div class="stat-number fw-bold fs-3"><?= number_format($totalQRCodes) ?></div>
                    <div class="stat-label text-muted small"><?= lang('Admin.total_qr') ?></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-xl-3">
        <div class="card stat-card border-0 shadow-sm h-100">
            <div class="card-body d-flex align-items-center gap-3">
                <div class="stat-icon bg-warning-subtle text-warning rounded-3">
                    <i class="bi bi-person-dash-fill fs-4"></i>
                </div>
                <div>
                    <div class="stat-number fw-bold fs-3"><?= number_format($totalProfiles - $activeProfiles) ?></div>
                    <div class="stat-label text-muted small"><?= lang('Admin.inactive') ?> Profiles</div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="row g-3 mb-4">
    <div class="col-md-auto">
        <a href="<?= site_url('admin/users/create') ?>" class="btn btn-primary">
            <i class="bi bi-person-plus me-1"></i><?= lang('Admin.add_user') ?>
        </a>
    </div>
    <div class="col-md-auto">
        <a href="<?= site_url('admin/qrcode') ?>" class="btn btn-outline-secondary">
            <i class="bi bi-qr-code me-1"></i><?= lang('Admin.qr_codes') ?>
        </a>
    </div>
    <div class="col-md-auto">
        <a href="<?= site_url('admin/users/export') ?>" class="btn btn-outline-success">
            <i class="bi bi-file-earmark-spreadsheet me-1"></i><?= lang('Admin.export_csv') ?>
        </a>
    </div>
</div>

<div class="row g-4">
    <!-- Recent Profiles -->
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white d-flex align-items-center justify-content-between py-3">
                <h6 class="mb-0 fw-semibold"><?= lang('Admin.recent_profiles') ?></h6>
                <a href="<?= site_url('admin/users') ?>" class="btn btn-sm btn-outline-primary"><?= lang('Admin.view_all') ?></a>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0 align-middle">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-3"><?= lang('Admin.name') ?></th>
                                <th><?= lang('Admin.designation') ?></th>
                                <th><?= lang('Admin.status') ?></th>
                                <th class="pe-3"><?= lang('Admin.actions') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($recentProfiles)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-4"><?= lang('Admin.no_records') ?></td></tr>
                            <?php else: ?>
                            <?php foreach ($recentProfiles as $p): ?>
                            <tr>
                                <td class="ps-3">
                                    <div class="d-flex align-items-center gap-2">
                                        <img src="<?= profile_photo_url($p['profile_photo']) ?>"
                                             class="rounded-circle" width="36" height="36" style="object-fit:cover" alt="">
                                        <span class="fw-medium unicode-text"><?= esc($p['name']) ?></span>
                                    </div>
                                </td>
                                <td class="text-muted small unicode-text"><?= esc($p['designation'] ?? '—') ?></td>
                                <td>
                                    <span class="badge <?= $p['is_active'] ? 'bg-success-subtle text-success' : 'bg-secondary-subtle text-secondary' ?>">
                                        <?= $p['is_active'] ? lang('Admin.active') : lang('Admin.inactive') ?>
                                    </span>
                                </td>
                                <td class="pe-3">
                                    <a href="<?= site_url('admin/users/view/'.$p['id']) ?>" class="btn btn-xs btn-outline-primary">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Activity -->
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white d-flex align-items-center justify-content-between py-3">
                <h6 class="mb-0 fw-semibold"><?= lang('Admin.recent_activity') ?></h6>
                <a href="<?= site_url('admin/activity-logs') ?>" class="btn btn-sm btn-outline-secondary"><?= lang('Admin.view_all') ?></a>
            </div>
            <div class="card-body p-0">
                <ul class="activity-list list-unstyled mb-0">
                    <?php if (empty($recentLogs)): ?>
                    <li class="text-center text-muted py-4"><?= lang('Admin.no_records') ?></li>
                    <?php else: ?>
                    <?php foreach ($recentLogs as $log): ?>
                    <li class="activity-item d-flex gap-3 align-items-start px-3 py-2 border-bottom">
                        <div class="activity-icon <?= log_icon_class($log['action']) ?> rounded-circle d-flex align-items-center justify-content-center flex-shrink-0" style="width:32px;height:32px">
                            <i class="bi <?= log_icon($log['action']) ?> small"></i>
                        </div>
                        <div class="flex-grow-1 overflow-hidden">
                            <div class="small fw-medium text-truncate"><?= esc($log['description'] ?? $log['action']) ?></div>
                            <div class="text-muted" style="font-size:0.7rem">
                                <?= esc($log['admin_name'] ?? 'System') ?> &bull;
                                <?= time_ago($log['created_at']) ?>
                            </div>
                        </div>
                    </li>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
// Helper functions defined inline for dashboard
function timeAgo(dateStr) {
    const d = new Date(dateStr), now = new Date();
    const diff = Math.floor((now - d) / 1000);
    if (diff < 60)    return diff + 's ago';
    if (diff < 3600)  return Math.floor(diff/60) + 'm ago';
    if (diff < 86400) return Math.floor(diff/3600) + 'h ago';
    return Math.floor(diff/86400) + 'd ago';
}
</script>
<?= $this->endSection() ?>
