<?php
require_once __DIR__ . '/includes/functions.php';

$token = $_GET['t'] ?? '';
if (!hash_equals(ALL_WORKERS_TOKEN, $token)) {
    http_response_code(404);
    echo 'Link not found.';
    exit;
}

$weekStart = $_GET['week'] ?? '';
$weekStart = isValidDateStr($weekStart) ? $weekStart : currentWeekStart();
$dates = getWeekDates($weekStart);

$workers = db()->fetchAll("SELECT * FROM workers WHERE active = 1 ORDER BY name ASC");

$placeholders = implode(',', array_fill(0, count($dates), '?'));
$allEntries = db()->query("SELECT * FROM day_entries WHERE work_date IN ({$placeholders})", $dates)->fetchAll();
$entriesByWorkerDate = [];
foreach ($allEntries as $en) { $entriesByWorkerDate[$en['worker_id']][$en['work_date']] = $en; }

$settlements = db()->fetchAll("SELECT worker_id, paid FROM week_settlements WHERE week_start = :w", ['w' => $weekStart]);
$paidByWorker = [];
foreach ($settlements as $s) { $paidByWorker[$s['worker_id']] = (bool) $s['paid']; }

$pageTitle = 'All Workers - ChuckCo Time Keeper';
require_once __DIR__ . '/includes/header.php';
?>
<div class="container" style="padding-top:1.5rem;">
    <div class="card">
        <h2 class="text-center">Week of <?= e(dayLabel($weekStart)) ?></h2>
        <div style="overflow-x:auto;">
        <table class="all-table">
            <thead>
                <tr>
                    <th>Worker</th>
                    <?php foreach ($dates as $d): ?><th><?= e((new DateTime($d))->format('D')) ?></th><?php endforeach; ?>
                    <th>Owed</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($workers as $w):
                    $wEntries = $entriesByWorkerDate[$w['id']] ?? [];
                    $owed = calcOwed(array_values($wEntries));
                    $isPaid = $paidByWorker[$w['id']] ?? false;
                ?>
                <tr>
                    <td><?= e($w['name']) ?></td>
                    <?php foreach ($dates as $d):
                        $entry = $wEntries[$d] ?? null;
                        $status = $entry['status'] ?? null;
                        $location = $entry['location'] ?? '';
                    ?>
                    <td>
                        <span class="badge badge-<?= $status ? e($status) : 'unset' ?>"><?= $status ? ucfirst(substr($status,0,1)) : '-' ?></span>
                        <?php if ($location): ?>
                            <div style="font-size:0.7rem; color:var(--text-muted); margin-top:2px; max-width:70px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"><?= e($location) ?></div>
                        <?php endif; ?>
                    </td>
                    <?php endforeach; ?>
                    <td><?= formatCurrency($owed) ?></td>
                    <td><span class="badge <?= $isPaid ? 'badge-full' : 'badge-absent' ?>"><?= $isPaid ? 'Paid' : 'Unpaid' ?></span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
