<?php
/**
 * Counter - My Assignments
 *
 * Dedicated page for counters to view their personal assignments,
 * status, and quick actions. Moved from dashboard for better organization.
 */
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../middleware/role_middleware.php';
counter_only();

$pageTitle = 'My Assignments';
$db = Database::getInstance();
$userId = SessionManager::getUserId();

// Recent assignments - exact query from dashboard
$recentAssignments = $db->fetchAll("
    SELECT a.*, s.name as session_name, i.Item_Code as item_code, i.Item_Description as item_name,
           i.Stores, i.Rack, i.Level, i.Bin,
           ce.counted_quantity, ce.counted_at, ce.notes as count_notes, a.zone
    FROM assignments a
    JOIN sessions s ON a.session_id = s.id
    JOIN items i ON a.item_id = i.id
    LEFT JOIN count_entries ce ON ce.assignment_id = a.id AND ce.counter_id = a.counter_id
    WHERE a.counter_id = ?
    ORDER BY
        FIELD(a.status, 'recount', 'in_progress', 'pending', 'counted', 'approved', 'rejected'),
        a.updated_at DESC
    LIMIT 20
", [$userId]);

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

<div class="card">
    <div class="card-header">
        <h3>Recent Assignments</h3>
        <div style="display:flex;gap:8px">
            <a href="<?= APP_URL ?>/counter/count_items.php" class="btn btn-sm btn-primary">Start New Count</a>
            <a href="<?= APP_URL ?>/counter/reports.php" class="btn btn-sm btn-outline">View Reports</a>
        </div>
    </div>
    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Location</th>
                    <th>Session</th>
                    <th>Counted</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($recentAssignments)): ?>
                <tr><td colspan="6" class="text-center text-muted" style="padding:40px">
                    <div style="font-size:48px;margin-bottom:16px">&#9999;</div>
                    No assignments yet. Contact your admin to get started.
                </td></tr>
                <?php else: foreach ($recentAssignments as $a): ?>
                <tr>
                    <td>
                        <strong><?= e($a['item_code']) ?></strong><br>
                        <small class="text-muted"><?= e($a['item_name']) ?></small>
                    </td>
                    <td><?= format_location($a['Stores'] ?? null, $a['Rack'] ?? null, $a['Level'] ?? null, $a['Bin'] ?? null, $a['zone'] ?? null) ?></td>
                    <td><?= e($a['session_name']) ?></td>
                    <td>
                        <?php if ($a['counted_quantity'] !== null): ?>
                            <strong><?= format_number($a['counted_quantity'], 0) ?></strong>
                        <?php else: ?>
                            <span class="text-muted">-</span>
                        <?php endif; ?>
                    </td>
                    <td><?= status_badge($a['status']) ?></td>
                    <td>
                        <?php if (in_array($a['status'], ['pending', 'in_progress', 'recount'])): ?>
                            <a href="<?= APP_URL ?>/counter/count_items.php?assignment_id=<?= $a['id'] ?>" class="btn btn-sm btn-primary">Count Now</a>
                        <?php elseif ($a['status'] === 'counted'): ?>
                            <span class="text-muted small">Awaiting Review</span>
                        <?php else: ?>
                            <span class="text-muted small"><?= ucfirst($a['status']) ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>

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