<?php
$p = $permit;
$statusBadge = [
    'draft' => 'bg-secondary', 'requested' => 'bg-primary', 'approved' => 'bg-info text-dark',
    'active' => 'bg-success', 'rejected' => 'bg-danger', 'closed' => 'bg-secondary', 'expired' => 'bg-dark',
];
$isPending = $p['status'] === 'requested';
$isApproved = $p['status'] === 'approved';
$isActive = $p['status'] === 'active';
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0">
        <?php if ($p['type_icon']): ?><i class="fas <?= e($p['type_icon']) ?> me-2 text-primary"></i><?php endif; ?>
        Permit #<?= (int) $p['permit_id'] ?> · <?= e($p['title']) ?>
    </h5>
    <div class="d-flex gap-2">
        <?php if (!$isActive && $p['status'] !== 'closed'): ?>
            <a class="btn btn-sm btn-outline-primary" href="<?= url('/Permits/edit/' . (int) $p['permit_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a>
        <?php endif; ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Permits') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</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>Type</dt><dd><?= e($p['type_code']) ?> · <?= e($p['type_name']) ?></dd></div>
        <div class="col-md-3"><dt>Status</dt><dd><span class="badge <?= $statusBadge[$p['status']] ?? 'bg-secondary' ?> text-uppercase"><?= e($p['status']) ?></span></dd></div>
        <div class="col-md-3"><dt>Work Order</dt><dd>
            <?php if (!empty($p['work_order_id'])): ?>
                <a href="<?= url('/WorkOrders/show/' . (int) $p['work_order_id']) ?>">WO #<?= (int) $p['work_order_id'] ?></a>
                <div class="small text-muted"><?= e($p['wo_title'] ?? '') ?></div>
            <?php else: ?>—<?php endif; ?>
        </dd></div>
        <div class="col-md-3"><dt>Asset / Site</dt><dd><?= e($p['asset_name'] ?? $p['site_name'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>Valid from</dt><dd><?= e($p['valid_from'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>Valid to</dt><dd><?= e($p['valid_to'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>Requested by</dt><dd><?= e($p['requested_username'] ?? '—') ?> · <?= e(substr((string) $p['created_at'], 0, 16)) ?></dd></div>
        <?php if ($p['approved_at']): ?>
            <div class="col-md-4"><dt>Approved / rejected by</dt><dd><?= e($p['approved_username'] ?? '—') ?> · <?= e(substr((string) $p['approved_at'], 0, 16)) ?></dd></div>
        <?php endif; ?>
        <?php if ($p['closed_at']): ?>
            <div class="col-md-4"><dt>Closed by</dt><dd><?= e($p['closed_username'] ?? '—') ?> · <?= e(substr((string) $p['closed_at'], 0, 16)) ?></dd></div>
        <?php endif; ?>
        <?php if (!empty($p['description'])): ?><div class="col-12"><dt>Scope</dt><dd><?= nl2br(e($p['description'])) ?></dd></div><?php endif; ?>
    </dl>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-danger"><i class="fas fa-triangle-exclamation me-1"></i>Hazards</h6>
        <p class="small mb-0"><?= nl2br(e($p['hazards'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-success"><i class="fas fa-shield me-1"></i>Controls</h6>
        <p class="small mb-0"><?= nl2br(e($p['controls'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2"><i class="fas fa-user-shield me-1"></i>PPE</h6>
        <p class="small mb-0"><?= e($p['ppe'] ?? '') ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2"><i class="fas fa-lock me-1"></i>Lockout tags</h6>
        <p class="small mb-0"><?= e($p['lockout_tags'] ?? '') ?: '<span class="text-muted">None issued.</span>' ?></p>
    </div></div>
</div>

<?php if ($p['status'] === 'rejected' && !empty($p['rejection_note'])): ?>
    <div class="alert alert-danger py-2 small"><strong>Rejection note:</strong> <?= e($p['rejection_note']) ?></div>
<?php endif; ?>

<?php if ($canApprove && $isPending): ?>
    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2"><i class="fas fa-gavel me-1"></i>Review this permit</h6>
        <div class="row g-2">
            <div class="col-md-6">
                <form method="post" action="<?= url('/Permits/approve/' . (int) $p['permit_id']) ?>">
                    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                    <button type="submit" class="btn btn-sm btn-success w-100"><i class="fas fa-check me-1"></i>Approve</button>
                </form>
            </div>
            <div class="col-md-6">
                <form method="post" action="<?= url('/Permits/reject/' . (int) $p['permit_id']) ?>" class="d-flex gap-2">
                    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                    <input type="text" class="form-control form-control-sm" name="rejection_note" placeholder="Reason for rejection (required)" required>
                    <button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-ban me-1"></i>Reject</button>
                </form>
            </div>
        </div>
    </div>
<?php endif; ?>

<?php if ($isApproved || $isActive): ?>
    <div class="rs-card p-3">
        <div class="d-flex gap-2">
            <?php if ($isApproved): ?>
                <form method="post" action="<?= url('/Permits/activate/' . (int) $p['permit_id']) ?>">
                    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-play me-1"></i>Activate on site</button>
                </form>
            <?php endif; ?>
            <form method="post" action="<?= url('/Permits/close/' . (int) $p['permit_id']) ?>"
                  class="ms-auto rs-confirm-submit"
                  data-confirm-title="Close permit?"
                  data-confirm-body="Mark this permit as closed? Any WO gate lifts once closed."
                  data-confirm-ok="Close">
                <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
                <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="fas fa-flag-checkered me-1"></i>Close permit</button>
            </form>
        </div>
    </div>
<?php endif; ?>
