<?= $this->extend('layouts/main') ?>
<?= $this->section('styles') ?>
.shift-table { border-collapse: separate; border-spacing: 0; }
.shift-table th, .shift-table td { white-space: nowrap; border: 1px solid #eef0f3; vertical-align: top; }
.shift-staff-col { position: sticky; left: 0; background: #fff; z-index: 2; min-width: 140px; }
.shift-today { background-color: color-mix(in srgb, var(--brand) 6%, white) !important; }
.shift-cell { padding: 6px !important; min-width: 150px; }
.shift-pill { border-radius: 6px; padding: 0.25rem 0.4rem; font-size: 0.72rem; margin-bottom: 4px; }
<?= $this->endSection() ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-start mb-3 flex-wrap gap-2">
    <div>
        <h1 class="h3 mb-1">Shift Schedule</h1>
        <p class="text-muted mb-0">Assign shifts, and approve swap / time-off requests.</p>
    </div>
    <div class="d-flex align-items-center gap-2">
        <a href="<?= site_url('shifts?start=' . date('Y-m-d', strtotime($start . ' -7 days'))) ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-chevron-left"></i></a>
        <span class="fw-semibold"><?= esc(date('j M', strtotime($start))) ?> &ndash; <?= esc(date('j M Y', strtotime($start . ' +6 days'))) ?></span>
        <a href="<?= site_url('shifts?start=' . date('Y-m-d', strtotime($start . ' +7 days'))) ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-chevron-right"></i></a>
        <a href="<?= site_url('shifts') ?>" class="btn btn-outline-secondary btn-sm">This Week</a>
    </div>
</div>

<?php
$byStaffDate = [];
foreach ($shifts as $s) {
    $byStaffDate[$s->user_id][$s->shift_date][] = $s;
}
?>

<div class="card shadow-sm mb-3">
    <div class="card-body">
        <h2 class="h6">Assign a Shift</h2>
        <form action="<?= site_url('shifts') ?>" method="post" class="row g-2 align-items-end">
            <?= csrf_field() ?>
            <div class="col-md-3">
                <label class="form-label small">Staff</label>
                <select class="form-select form-select-sm" name="user_id" required>
                    <?php foreach ($staff as $s): ?>
                        <option value="<?= $s->id ?>"><?= esc($s->full_name) ?> (<?= esc($lookups->staffRoles[$s->role]['label'] ?? $s->role) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Date</label>
                <input type="date" class="form-control form-control-sm" name="shift_date" value="<?= esc($start) ?>" required>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Block</label>
                <select class="form-select form-select-sm" name="shift_block" required>
                    <?php foreach ($lookups->shiftBlocks as $key => $meta): ?>
                        <option value="<?= esc($key) ?>"><?= esc($meta['label']) ?> (<?= esc($meta['time']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Standard Hours</label>
                <input type="number" step="0.5" min="1" class="form-control form-control-sm" name="standard_hours" value="8">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Notes</label>
                <input type="text" class="form-control form-control-sm" name="notes">
            </div>
            <div class="col-md-1">
                <button type="submit" class="btn btn-primary btn-sm w-100">Assign</button>
            </div>
        </form>
    </div>
</div>

<div class="card mb-3">
    <div class="table-scroll">
        <table class="table shift-table mb-0">
            <thead>
                <tr>
                    <th class="shift-staff-col">Staff</th>
                    <?php foreach ($dateColumns as $date): ?>
                        <th class="text-center <?= $date === date('Y-m-d') ? 'shift-today' : '' ?>">
                            <?= date('D', strtotime($date)) ?> <?= date('j M', strtotime($date)) ?>
                        </th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($staff)): ?>
                    <tr><td colspan="<?= count($dateColumns) + 1 ?>" class="text-center text-muted py-4">No active staff.</td></tr>
                <?php endif; ?>
                <?php foreach ($staff as $s): ?>
                    <tr>
                        <td class="shift-staff-col fw-semibold small"><?= esc($s->full_name) ?><br><span class="text-muted fw-normal"><?= esc($lookups->staffRoles[$s->role]['label'] ?? $s->role) ?></span></td>
                        <?php foreach ($dateColumns as $date): ?>
                            <td class="shift-cell <?= $date === date('Y-m-d') ? 'shift-today' : '' ?>">
                                <?php foreach (($byStaffDate[$s->id][$date] ?? []) as $shift): ?>
                                    <?php $blockMeta = $lookups->shiftBlocks[$shift->shift_block] ?? ['label' => $shift->shift_block, 'badge' => 'secondary']; ?>
                                    <div class="shift-pill bg-<?= $blockMeta['badge'] ?>">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <span><?= esc($blockMeta['label']) ?></span>
                                            <span class="badge bg-<?= $lookups->shiftStatuses[$shift->status]['badge'] ?? 'secondary' ?>" style="font-size: 0.62rem;"><?= esc($lookups->shiftStatuses[$shift->status]['label'] ?? $shift->status) ?></span>
                                        </div>
                                        <?php if ($shift->actual_hours !== null): ?>
                                            <div style="font-size: 0.68rem;"><?= esc($shift->actual_hours) ?>h logged<?php $ot = max(0, (float) $shift->actual_hours - (float) $shift->standard_hours); ?><?php if ($ot > 0): ?> (+<?= number_format($ot, 1) ?>h OT)<?php endif; ?></div>
                                        <?php endif; ?>
                                        <?php if ($shift->status === 'scheduled'): ?>
                                        <form action="<?= site_url('shifts/' . $shift->id . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this shift?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-link btn-sm p-0" style="font-size: 0.65rem;">Cancel</button>
                                        </form>
                                        <?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            </td>
                        <?php endforeach; ?>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="row">
    <div class="col-md-6 mb-3">
        <div class="card shadow-sm h-100">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-arrow-left-right me-1"></i> Pending Swap Requests</span>
                <span class="badge bg-secondary"><?= count($pendingSwaps) ?></span>
            </div>
            <?php if (empty($pendingSwaps)): ?>
                <div class="card-body text-muted small">No pending swap requests.</div>
            <?php else: ?>
            <ul class="list-group list-group-flush">
                <?php foreach ($pendingSwaps as $req): ?>
                <li class="list-group-item">
                    <p class="mb-1 small"><strong><?= esc($req->requester_name) ?></strong> wants <strong><?= esc($req->target_name) ?></strong> to take their <?= esc($lookups->shiftBlocks[$req->shift_block]['label'] ?? $req->shift_block) ?> shift on <?= esc(date('d M Y', strtotime($req->shift_date))) ?>.</p>
                    <?php if ($req->reason): ?><p class="text-muted small mb-2">"<?= esc($req->reason) ?>"</p><?php endif; ?>
                    <div class="d-flex gap-2">
                        <form action="<?= site_url('shift-swaps/' . $req->id . '/approve') ?>" method="post"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-success">Approve</button></form>
                        <form action="<?= site_url('shift-swaps/' . $req->id . '/deny') ?>" method="post"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline-danger">Deny</button></form>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>
    <div class="col-md-6 mb-3">
        <div class="card shadow-sm h-100">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-calendar-x me-1"></i> Pending Time-Off Requests</span>
                <span class="badge bg-secondary"><?= count($pendingTimeOff) ?></span>
            </div>
            <?php if (empty($pendingTimeOff)): ?>
                <div class="card-body text-muted small">No pending time-off requests.</div>
            <?php else: ?>
            <ul class="list-group list-group-flush">
                <?php foreach ($pendingTimeOff as $req): ?>
                <li class="list-group-item">
                    <p class="mb-1 small"><strong><?= esc($req->staff_name) ?></strong> — <?= esc(date('d M', strtotime($req->date_from))) ?> to <?= esc(date('d M Y', strtotime($req->date_to))) ?></p>
                    <?php if ($req->reason): ?><p class="text-muted small mb-2">"<?= esc($req->reason) ?>"</p><?php endif; ?>
                    <div class="d-flex gap-2">
                        <form action="<?= site_url('time-off/' . $req->id . '/approve') ?>" method="post"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-success">Approve</button></form>
                        <form action="<?= site_url('time-off/' . $req->id . '/deny') ?>" method="post"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline-danger">Deny</button></form>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>
</div>

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