<?php
/**
 * Admin Dashboard
 */
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../middleware/role_middleware.php';
admin_only();

$pageTitle = 'Admin Dashboard';
$db = Database::getInstance();

// Statistics
$totalUsers = $db->fetchColumn("SELECT COUNT(*) FROM users");
$totalCounters = $db->fetchColumn("SELECT COUNT(*) FROM users WHERE role = 'counter'");
$totalCheckers = $db->fetchColumn("SELECT COUNT(*) FROM users WHERE role = 'checker'");
$totalItems = $db->fetchColumn("SELECT COUNT(*) FROM items WHERE Status = 'Active'");
$totalSessions = $db->fetchColumn("SELECT COUNT(*) FROM sessions");
$activeSessions = $db->fetchColumn("SELECT COUNT(*) FROM sessions WHERE status IN ('open','in_progress')");
$totalAssignments = $db->fetchColumn("SELECT COUNT(*) FROM assignments");
$pendingAssignments = $db->fetchColumn("SELECT COUNT(*) FROM assignments a WHERE a.status = 'pending'");
$countedAssignments = $db->fetchColumn("SELECT COUNT(*) FROM assignments a WHERE a.status = 'counted'");
$approvedAssignments = $db->fetchColumn("SELECT COUNT(*) FROM assignments a WHERE a.status = 'approved'");
$rejectedAssignments = $db->fetchColumn("SELECT COUNT(*) FROM assignments a WHERE a.status = 'rejected'");

// Recent activity
$recentCounts = $db->fetchAll("
    SELECT ce.*, a.status as assignment_status, u.full_name as counter_name, i.Item_Description AS item_name, i.Item_Code AS item_code, s.name as session_name, i.Stores, i.Rack, i.Level, i.Bin
    FROM count_entries ce
    JOIN assignments a ON ce.assignment_id = a.id
    JOIN users u ON ce.counter_id = u.id
    JOIN items i ON a.item_id = i.id
    JOIN sessions s ON a.session_id = s.id
    ORDER BY ce.counted_at DESC
    LIMIT 10
");

include APP_ROOT . '/templates/header.php';
?>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon primary">&#128101;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $totalUsers ?></div>
            <div class="stat-label">Total Users</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon info">&#128230;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $totalItems ?></div>
            <div class="stat-label">Active Items</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon success">&#128197;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $activeSessions ?></div>
            <div class="stat-label">Active Sessions</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon warning">&#9999;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $pendingAssignments ?></div>
            <div class="stat-label">Pending Assignments</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon info">&#9989;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $countedAssignments ?></div>
            <div class="stat-label">Awaiting Review</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon success">&#10004;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $approvedAssignments ?></div>
            <div class="stat-label">Approved</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon danger">&#10008;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $rejectedAssignments ?></div>
            <div class="stat-label">Rejected</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-icon secondary">&#128197;</div>
        <div class="stat-content">
            <div class="stat-value"><?= $db->fetchColumn("SELECT COUNT(*) FROM MonthlyInventorySnapshots") ?></div>
            <div class="stat-label">Qty Snapshots</div>
        </div>
    </div>
</div>



<div style="display:grid;grid-template-columns:1fr 1fr;gap:24px">
    <!-- Recent Counts -->
    <div class="card">
        <div class="card-header">
            <h3>Recent Count Entries</h3>
            <a href="<?= APP_URL ?>/admin/pending_counts.php" class="btn btn-sm btn-outline-primary">View All</a>
        </div>
        <div class="table-container">
            <table>
                <thead>
                    <tr>
                        <th>Item</th>
                        <th>Counter</th>
                        <th>Qty</th>
                        <th>Status</th>
                        <th>Time</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($recentCounts)): ?>
                    <tr><td colspan="5" class="text-center text-muted" style="padding:30px">No count entries yet.</td></tr>
                    <?php else: foreach ($recentCounts as $count): ?>
                    <tr>
                        <td><strong><?= e($count['item_code']) ?></strong><br><small class="text-muted"><?= e($count['item_name']) ?></small></td>
                        <td><?= format_location($count['Stores'], $count['Rack'], $count['Level'], $count['Bin']) ?></td>
                        <td><?= e($count['counter_name']) ?></td>
                        <td><strong><?= format_number($count['counted_quantity'], 0) ?></strong></td>
                        <td><?= status_badge($count['assignment_status']) ?></td>
                        <td><small><?= time_ago($count['counted_at']) ?></small></td>
                    </tr>
<?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include APP_ROOT . '/templates/footer.php'; ?>
