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

<?php
$prio   = $lookups->taskPriorities[$task->priority] ?? ['label' => ucfirst($task->priority), 'badge' => 'secondary'];
$status = $lookups->housekeepingTaskStatuses[$task->status] ?? ['label' => ucfirst($task->status), 'badge' => 'secondary'];
?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0">Housekeeping Task #<?= $task->id ?></h1>
        <p class="text-muted mb-0">
            Room <?= esc($task->room_number) ?> (<?= esc($task->room_type_name) ?>) ·
            <span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span>
        </p>
    </div>
    <a href="<?= site_url('housekeeping/' . $task->id . '/edit') ?>" class="btn btn-outline-secondary"><i class="bi bi-pencil me-1"></i>Edit</a>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <h2 class="h6">Task Details</h2>
                <div class="row row-cols-2 g-3 small">
                    <div class="col"><span class="text-muted">Task Type</span><br><span class="fw-semibold"><?= esc($lookups->housekeepingTaskTypes[$task->task_type]['label'] ?? $task->task_type) ?></span></div>
                    <div class="col"><span class="text-muted">Priority</span><br><span class="badge bg-<?= $prio['badge'] ?>"><?= esc($prio['label']) ?></span></div>
                    <div class="col"><span class="text-muted">Assigned To</span><br><span class="fw-semibold"><?= esc($task->assigned_to_name ?: 'Unassigned') ?></span></div>
                    <div class="col"><span class="text-muted">Created</span><br><span class="fw-semibold"><?= esc(date('d M Y, g:i a', strtotime($task->created_at))) ?></span></div>
                    <div class="col"><span class="text-muted">Started</span><br><span class="fw-semibold"><?= $task->start_time ? esc(date('d M Y, g:i a', strtotime($task->start_time))) : '—' ?></span></div>
                    <div class="col"><span class="text-muted">Completed</span><br><span class="fw-semibold"><?= $task->completed_at ? esc(date('d M Y, g:i a', strtotime($task->completed_at))) : '—' ?></span></div>
                    <div class="col"><span class="text-muted">Inspected</span><br><span class="fw-semibold"><?= $task->inspected_at ? esc(date('d M Y, g:i a', strtotime($task->inspected_at))) : '—' ?></span></div>
                    <div class="col"><span class="text-muted">Inspected By</span><br><span class="fw-semibold"><?= esc($inspectorName ?: '—') ?></span></div>
                </div>
                <?php if ($task->notes): ?>
                    <hr>
                    <span class="text-muted small">Notes</span>
                    <p class="mb-0"><?= esc($task->notes) ?></p>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm">
            <div class="card-header">Workflow</div>
            <div class="card-body d-flex flex-column gap-2">
                <?php if ($task->status === 'not_started'): ?>
                    <form action="<?= site_url('housekeeping/' . $task->id . '/start') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary w-100"><i class="bi bi-play-fill me-1"></i>Start Task</button>
                    </form>
                <?php elseif ($task->status === 'in_progress'): ?>
                    <form action="<?= site_url('housekeeping/' . $task->id . '/complete') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-success w-100"><i class="bi bi-check2 me-1"></i>Mark Complete</button>
                    </form>
                <?php elseif ($task->status === 'completed' && ! $task->inspected_at): ?>
                    <form action="<?= site_url('housekeeping/' . $task->id . '/inspect') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-primary w-100"><i class="bi bi-clipboard-check me-1"></i>Mark Inspected</button>
                    </form>
                <?php else: ?>
                    <p class="text-muted small mb-0">This task's workflow is finished.</p>
                <?php endif; ?>
                <a href="<?= site_url('rooms/' . $task->room_id) ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-door-closed me-1"></i>View Room</a>
                <a href="<?= site_url('housekeeping') ?>" class="btn btn-outline-secondary btn-sm">Back to Housekeeping</a>
            </div>
        </div>
    </div>
</div>

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