<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
    $map = [
        'pending'   => 'on_leave',
        'approved'  => 'active',
        'rejected'  => 'terminated',
        'cancelled' => 'inactive',
    ];
    $class = $map[$status] ?? 'inactive';

    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">'
        . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $entry['number'],
    'workspaceTitle'        => esc($entry['number']),
    'workspaceDesc'         => esc($entry['title']),
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-file-signature"></i>
                <h2 class="enterprise-settings-card__title">Request Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($entry['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Title</dt>
                <dd class="col-sm-8"><?= esc($entry['title']) ?></dd>
                <dt class="col-sm-4 text-muted">Type</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($entry['request_type'])) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($entry['status']) ?></dd>
                <dt class="col-sm-4 text-muted">From</dt>
                <dd class="col-sm-8"><?= esc($entry['requester_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Sent To</dt>
                <dd class="col-sm-8"><?= esc($entry['approver_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Amount</dt>
                <dd class="col-sm-8"><?= $entry['amount'] !== null ? number_format((float) $entry['amount'], 2) : '—' ?></dd>
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8"><?= nl2br(esc($entry['description'] ?: '—')) ?></dd>
                <?php if (! empty($entry['rejection_reason'])): ?>
                    <dt class="col-sm-4 text-muted">Rejection Reason</dt>
                    <dd class="col-sm-8"><?= nl2br(esc($entry['rejection_reason'])) ?></dd>
                <?php endif; ?>
                <?php if (! empty($entry['decided_at'])): ?>
                    <dt class="col-sm-4 text-muted">Decided At</dt>
                    <dd class="col-sm-8"><?= esc($entry['decided_at']) ?></dd>
                <?php endif; ?>
            </dl>
        </article>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <?php if ($entry['status'] === 'pending' && ! empty($isApprover)): ?>
                    <form action="<?= base_url('operations/approvals/requests/' . $entry['id'] . '/approve') ?>" method="post"
                          data-tpin-confirm data-confirm-message="Approve this request?">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-success btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Approve
                        </button>
                    </form>
                    <form action="<?= base_url('operations/approvals/requests/' . $entry['id'] . '/reject') ?>" method="post"
                          data-tpin-confirm data-confirm-message="Reject this request?">
                        <?= csrf_field() ?>
                        <textarea name="rejection_reason" class="form-control form-control-sm mb-2" rows="2" placeholder="Rejection reason (optional)"></textarea>
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                            <i class="fa-solid fa-xmark me-1"></i> Reject
                        </button>
                    </form>
                <?php endif; ?>
                <?php if ($entry['status'] === 'pending' && ! empty($isRequester)): ?>
                    <form action="<?= base_url('operations/approvals/requests/' . $entry['id'] . '/cancel') ?>" method="post"
                          onsubmit="return confirm('Cancel this request?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Cancel Request
                        </button>
                    </form>
                <?php endif; ?>
                <a href="<?= base_url(! empty($isApprover) ? 'operations/approvals/inbox' : 'operations/approvals/my-requests') ?>"
                   class="btn btn-outline-secondary btn-sm">Back</a>
            </div>
        </article>
    </div>
</div>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>

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