<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h3 class="mb-1"><?= lang('dashboard.welcome') ?>, <?= e(auth_user('full_name_en') ?? 'User') ?>!</h3>
        <p class="text-muted mb-0"><?= date('l, F j, Y') ?></p>
    </div>
</div>

<!-- Widgets Grid -->
<div class="row g-3">
    <?php if (empty($widgets)): ?>
    <div class="col-12">
        <div class="card">
            <div class="card-body text-center py-5">
                <i class="fas fa-cube fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">No modules installed yet</h5>
                <p class="text-muted">Dashboard widgets will appear here as modules are added to the system.</p>
            </div>
        </div>
    </div>
    <?php else: ?>
    <?php foreach ($widgets as $widget): ?>
    <div class="<?= $widget['size'] ?? 'col-md-4' ?>">
        <?php
        $partialPath = ROOT_PATH . '/modules/' . $widget['_module'] . '/' . ($widget['partial'] ?? '');
        if (file_exists($partialPath)) {
            include $partialPath;
        } else {
            echo '<div class="card"><div class="card-body text-muted">Widget: ' . e($widget['label_en'] ?? 'Unknown') . '</div></div>';
        }
        ?>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
</div>

<!-- Quick System Stats (Phase 0 default widget) -->
<div class="row g-3 mt-3">
    <?php
    try {
        $db = Database::getInstance();
        $totalMembers = $db->fetchColumn("SELECT COUNT(*) FROM members WHERE status = 'active' AND is_deleted = 0");
        $totalUsers = $db->fetchColumn("SELECT COUNT(*) FROM auth_users WHERE is_active = 1 AND is_deleted = 0");
        $modulesCount = count(ModuleRegistry::getModules());
    } catch (\Exception $e) {
        $totalMembers = 0;
        $totalUsers = 0;
        $modulesCount = 0;
    }
    ?>
    <div class="col-md-3">
        <div class="card border-primary h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1">Active Members</h6>
                        <h3 class="mb-0"><?= number_format($totalMembers) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-users fa-2x text-primary opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-success h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1">System Users</h6>
                        <h3 class="mb-0"><?= number_format($totalUsers) ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-user-shield fa-2x text-success opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-info h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1">Active Modules</h6>
                        <h3 class="mb-0"><?= $modulesCount ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-puzzle-piece fa-2x text-info opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-warning h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h6 class="text-muted mb-1">Server Time</h6>
                        <h3 class="mb-0" style="font-size:1.2rem"><?= date('H:i:s') ?></h3>
                    </div>
                    <div class="align-self-center"><i class="fas fa-clock fa-2x text-warning opacity-50"></i></div>
                </div>
            </div>
        </div>
    </div>
</div>