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

<h1 class="h3 mb-1">My Shifts</h1>
<p class="text-muted mb-3">Your schedule, plus swap and time-off requests.</p>

<div class="row">
    <div class="col-lg-7 mb-3">
        <div class="card shadow-sm">
            <div class="card-header">Upcoming &amp; Recent Shifts</div>
            <?php if (empty($shifts)): ?>
                <div class="card-body text-muted small">No shifts scheduled.</div>
            <?php else: ?>
            <div class="table-scroll">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light"><tr><th>Date</th><th>Block</th><th>Status</th><th>Hours</th><th></th></tr></thead>
                    <tbody>
                        <?php foreach ($shifts as $shift): ?>
                        <?php $blockMeta = $lookups->shiftBlocks[$shift->shift_block] ?? ['label' => $shift->shift_block, 'badge' => 'secondary']; ?>
                        <tr>
                            <td><?= esc(date('D, d M Y', strtotime($shift->shift_date))) ?></td>
                            <td><span class="badge bg-<?= $blockMeta['badge'] ?>"><?= esc($blockMeta['label']) ?></span></td>
                            <td><span class="badge bg-<?= $lookups->shiftStatuses[$shift->status]['badge'] ?? 'secondary' ?>"><?= esc($lookups->shiftStatuses[$shift->status]['label'] ?? $shift->status) ?></span></td>
                            <td class="small">
                                <?php if ($shift->actual_hours !== null): ?>
                                    <?= esc($shift->actual_hours) ?>h
                                    <?php $ot = max(0, (float) $shift->actual_hours - (float) $shift->standard_hours); ?>
                                    <?php if ($ot > 0): ?><span class="text-warning">(+<?= number_format($ot, 1) ?>h OT)</span><?php endif; ?>
                                <?php else: ?>
                                    <span class="text-muted">—</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end">
                                <?php if ($shift->status === 'scheduled'): ?>
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">Actions</button>
                                    <div class="dropdown-menu dropdown-menu-end p-3" style="min-width: 260px;">
                                        <form action="<?= site_url('shifts/' . $shift->id . '/log-hours') ?>" method="post" class="mb-3">
                                            <?= csrf_field() ?>
                                            <label class="form-label small fw-semibold">Log actual hours</label>
                                            <div class="input-group input-group-sm">
                                                <input type="number" step="0.5" min="0" class="form-control" name="actual_hours" required>
                                                <button type="submit" class="btn btn-outline-primary">Save</button>
                                            </div>
                                        </form>
                                        <hr>
                                        <form action="<?= site_url('shifts/' . $shift->id . '/request-swap') ?>" method="post">
                                            <?= csrf_field() ?>
                                            <label class="form-label small fw-semibold">Request a swap</label>
                                            <select class="form-select form-select-sm mb-2" name="target_user_id" required>
                                                <option value="">Who should take it?</option>
                                                <?php foreach ($colleagues as $c): ?>
                                                    <option value="<?= $c->id ?>"><?= esc($c->full_name) ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                            <input type="text" class="form-control form-control-sm mb-2" name="reason" placeholder="Reason (optional)">
                                            <button type="submit" class="btn btn-sm btn-outline-primary w-100">Request Swap</button>
                                        </form>
                                    </div>
                                </div>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="col-lg-5 mb-3">
        <div class="card shadow-sm mb-3">
            <div class="card-header">Request Time Off</div>
            <div class="card-body">
                <form action="<?= site_url('time-off') ?>" method="post" class="row g-2">
                    <?= csrf_field() ?>
                    <div class="col-6">
                        <label class="form-label small">From</label>
                        <input type="date" class="form-control form-control-sm" name="date_from" required>
                    </div>
                    <div class="col-6">
                        <label class="form-label small">To</label>
                        <input type="date" class="form-control form-control-sm" name="date_to" required>
                    </div>
                    <div class="col-12">
                        <label class="form-label small">Reason</label>
                        <input type="text" class="form-control form-control-sm" name="reason">
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-sm btn-primary w-100">Submit Request</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="card shadow-sm mb-3">
            <div class="card-header">My Swap Requests</div>
            <?php if (empty($swapRequests)): ?>
                <div class="card-body text-muted small">None yet.</div>
            <?php else: ?>
            <ul class="list-group list-group-flush small">
                <?php foreach ($swapRequests as $req): ?>
                <li class="list-group-item d-flex justify-content-between align-items-center">
                    <span><?= esc(date('d M', strtotime($req->shift_date))) ?> — <?= esc($lookups->shiftBlocks[$req->shift_block]['label'] ?? $req->shift_block) ?> → <?= esc($req->target_name) ?></span>
                    <span class="badge bg-<?= $lookups->requestStatuses[$req->status]['badge'] ?? 'secondary' ?>"><?= esc($lookups->requestStatuses[$req->status]['label'] ?? $req->status) ?></span>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>

        <div class="card shadow-sm">
            <div class="card-header">My Time-Off Requests</div>
            <?php if (empty($timeOffRequests)): ?>
                <div class="card-body text-muted small">None yet.</div>
            <?php else: ?>
            <ul class="list-group list-group-flush small">
                <?php foreach ($timeOffRequests as $req): ?>
                <li class="list-group-item d-flex justify-content-between align-items-center">
                    <span><?= esc(date('d M', strtotime($req->date_from))) ?> – <?= esc(date('d M Y', strtotime($req->date_to))) ?></span>
                    <span class="badge bg-<?= $lookups->requestStatuses[$req->status]['badge'] ?? 'secondary' ?>"><?= esc($lookups->requestStatuses[$req->status]['label'] ?? $req->status) ?></span>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>
</div>

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