<?= $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'  => 'HRM',
    'workspaceCrumbCurrent' => 'Leave Request #' . $request['id'],
    'workspaceTitle'      => 'Leave Request Detail',
    'workspaceDesc'       => 'Review request details and process approval workflow actions.',
]) ?>

<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-lines"></i>
                <h2 class="enterprise-settings-card__title">Request Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Employee</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($employee['full_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Leave Type</dt>
                <dd class="col-sm-8"><?= esc($type['name'] ?? '—') ?> <code><?= esc($type['code'] ?? '') ?></code></dd>
                <dt class="col-sm-4 text-muted">Period</dt>
                <dd class="col-sm-8"><?= esc($request['start_date']) ?> → <?= esc($request['end_date']) ?></dd>
                <dt class="col-sm-4 text-muted">Days</dt>
                <dd class="col-sm-8"><?= number_format((float) $request['days'], 1) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($request['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Reason</dt>
                <dd class="col-sm-8"><?= esc($request['reason'] ?: '—') ?></dd>
                <?php if ($request['status'] === 'rejected' && $request['rejection_reason']): ?>
                    <dt class="col-sm-4 text-muted">Rejection Reason</dt>
                    <dd class="col-sm-8 text-danger"><?= esc($request['rejection_reason']) ?></dd>
                <?php endif; ?>
                <?php if ($approver): ?>
                    <dt class="col-sm-4 text-muted">Processed By</dt>
                    <dd class="col-sm-8"><?= esc($approver->getFullName()) ?> on <?= esc($request['approved_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-gavel"></i>
                <h2 class="enterprise-settings-card__title">Approval Workflow</h2>
            </div>

            <?php if ($request['status'] === 'pending'): ?>
                <form action="<?= base_url('leave/requests/' . $request['id'] . '/approve') ?>" method="post" class="mb-3">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success w-100"><i class="fa-solid fa-check me-1"></i> Approve</button>
                </form>
                <form action="<?= base_url('leave/requests/' . $request['id'] . '/reject') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-2">
                        <textarea name="rejection_reason" class="form-control form-control-sm" rows="2" placeholder="Rejection reason (optional)"></textarea>
                    </div>
                    <button type="submit" class="btn btn-outline-danger w-100"><i class="fa-solid fa-xmark me-1"></i> Reject</button>
                </form>
            <?php elseif (in_array($request['status'], ['approved'], true)): ?>
                <form action="<?= base_url('leave/requests/' . $request['id'] . '/cancel') ?>" method="post" onsubmit="return confirm('Cancel this leave request?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-warning w-100"><i class="fa-solid fa-ban me-1"></i> Cancel Request</button>
                </form>
            <?php else: ?>
                <p class="text-muted small mb-0">No actions available for this request.</p>
            <?php endif; ?>

            <hr>
            <a href="<?= base_url('leave/requests') ?>" class="btn btn-outline-secondary w-100">Back to Requests</a>

            <?php if (in_array($request['status'], ['rejected', 'cancelled'], true)): ?>
                <form action="<?= base_url('leave/requests/' . $request['id'] . '/delete') ?>" method="post" class="mt-2" onsubmit="return confirm('Delete this request permanently?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger w-100 btn-sm">Delete</button>
                </form>
            <?php endif; ?>
        </article>
    </div>
</div>

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