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

<?php
$prio   = $lookups->taskPriorities[$workOrder->priority] ?? ['label' => ucfirst($workOrder->priority), 'badge' => 'secondary'];
$status = $isOverdue ? ['label' => 'Overdue', 'badge' => 'danger'] : ($lookups->workOrderStatuses[$workOrder->status] ?? ['label' => ucfirst($workOrder->status), 'badge' => 'secondary']);
?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0">WO-<?= sprintf('%04d', $workOrder->id) ?> — <?= esc($workOrder->title) ?></h1>
        <p class="text-muted mb-0">
            <?= esc($workOrder->asset ?: ($workOrder->room_number ? 'Room ' . $workOrder->room_number : 'No asset/room specified')) ?> ·
            <span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span>
        </p>
    </div>
    <a href="<?= site_url('maintenance/' . $workOrder->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">Work Order Details</h2>
                <div class="row row-cols-2 g-3 small">
                    <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">Due Date</span><br><span class="fw-semibold"><?= $workOrder->due_date ? esc(date('d M Y', strtotime($workOrder->due_date))) : '—' ?></span></div>
                    <div class="col"><span class="text-muted">Reported By</span><br><span class="fw-semibold"><?= esc($workOrder->reported_by_name ?: '—') ?></span></div>
                    <div class="col"><span class="text-muted">Assigned To</span><br><span class="fw-semibold"><?= esc($workOrder->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($workOrder->created_at))) ?></span></div>
                    <div class="col"><span class="text-muted">Completed</span><br><span class="fw-semibold"><?= $workOrder->completed_at ? esc(date('d M Y, g:i a', strtotime($workOrder->completed_at))) : '—' ?></span></div>
                </div>
                <?php if ($workOrder->notes): ?>
                    <hr>
                    <span class="text-muted small">Notes</span>
                    <p class="mb-0"><?= esc($workOrder->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 ($workOrder->status === 'open'): ?>
                    <form action="<?= site_url('maintenance/' . $workOrder->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 Work</button>
                    </form>
                <?php elseif ($workOrder->status === 'in_progress'): ?>
                    <form action="<?= site_url('maintenance/' . $workOrder->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 else: ?>
                    <p class="text-muted small mb-0">This work order is complete.</p>
                <?php endif; ?>
                <a href="<?= site_url('maintenance') ?>" class="btn btn-outline-secondary btn-sm">Back to Maintenance</a>
            </div>
        </div>
    </div>
</div>

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