<?php
$statusBadge = [
    'new'       => 'bg-primary',
    'approved'  => 'bg-info text-dark',
    'rejected'  => 'bg-secondary',
    'converted' => 'bg-success',
];
$req = $request;
$isPending = $req['status'] === 'new';
$isApproved = $req['status'] === 'approved';
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-inbox me-2 text-primary"></i>Request #<?= (int) $req['request_id'] ?> · <?= e($req['title']) ?></h5>
    <a href="<?= url('/WorkRequests') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="rs-card p-3 mb-3">
    <dl class="rs-def row g-2 mb-0">
        <div class="col-md-3"><dt>Status</dt><dd><span class="badge <?= $statusBadge[$req['status']] ?? 'bg-secondary' ?> text-uppercase"><?= e($req['status']) ?></span></dd></div>
        <div class="col-md-3"><dt>Priority</dt><dd><span class="badge bg-secondary text-uppercase"><?= e($req['priority']) ?></span></dd></div>
        <div class="col-md-3"><dt>Asset</dt><dd><?= e($req['asset_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Site</dt><dd><?= e($req['site_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Requestor</dt><dd><?= e($req['requested_username'] ?? $req['requested_email'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Raised</dt><dd><?= e($req['created_at']) ?></dd></div>
        <?php if ($req['reviewed_at']): ?>
            <div class="col-md-3"><dt>Reviewed</dt><dd><?= e($req['reviewed_at']) ?></dd></div>
            <div class="col-md-3"><dt>Reviewed by</dt><dd><?= e($req['reviewed_username'] ?? '—') ?></dd></div>
        <?php endif; ?>
        <?php if (!empty($req['converted_wo_id'])): ?>
            <div class="col-md-6"><dt>Converted to WO</dt><dd>
                <a href="<?= url('/WorkOrders/show/' . (int) $req['converted_wo_id']) ?>" class="btn btn-sm btn-outline-success">
                    <i class="fas fa-clipboard-check me-1"></i>Open WO #<?= (int) $req['converted_wo_id'] ?>
                </a>
            </dd></div>
        <?php endif; ?>
        <div class="col-12"><dt>Description</dt><dd><?= nl2br(e($req['description'] ?? '—')) ?></dd></div>
        <?php if (!empty($req['review_note'])): ?>
            <div class="col-12"><dt>Review note</dt><dd class="text-muted"><?= nl2br(e($req['review_note'])) ?></dd></div>
        <?php endif; ?>
    </dl>
</div>

<?php if ($canReview && $isPending): ?>
    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2"><i class="fas fa-gavel me-1"></i>Triage</h6>
        <form method="post" action="<?= url('/WorkRequests/review/' . (int) $req['request_id']) ?>">
            <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
            <div class="row g-2">
                <div class="col-md-3"><label class="form-label">Decision</label>
                    <select name="review_status" class="form-select form-select-sm" required>
                        <option value="approved">Approve</option>
                        <option value="rejected">Reject</option>
                    </select>
                </div>
                <div class="col-md-7"><label class="form-label">Note (optional)</label>
                    <input type="text" class="form-control form-control-sm" name="review_note" placeholder="Why?">
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-sm btn-primary w-100"><i class="fas fa-check me-1"></i>Save</button>
                </div>
            </div>
        </form>
    </div>
<?php endif; ?>

<?php if ($canReview && $isApproved && empty($req['converted_wo_id'])): ?>
    <div class="rs-card p-3">
        <h6 class="fw-bold mb-2"><i class="fas fa-clipboard-check me-1"></i>Convert to Work Order</h6>
        <p class="text-muted small mb-2">This creates a Work Order in <em>Draft</em> using the request's title, description, priority and asset. You can refine it once created.</p>
        <form method="post" action="<?= url('/WorkRequests/convert/' . (int) $req['request_id']) ?>">
            <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
            <button type="submit" class="btn btn-sm btn-success"><i class="fas fa-arrow-right me-1"></i>Create WO from request</button>
        </form>
    </div>
<?php endif; ?>
