<?= $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' => 'My Requests',
    'workspaceTitle'        => 'My Requests',
    'workspaceDesc'         => 'Approval requests you sent to someone else.',
    'workspaceAction'       => [
        'href'  => base_url('operations/approvals/requests/create'),
        'label' => 'New Request',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Sent Requests</h2>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/approvals/my-requests') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Number, title"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['pending', 'approved', 'rejected', 'cancelled'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= esc(ucfirst($s)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Title</th>
                        <th>Type</th>
                        <th>Sent To</th>
                        <th>Amount</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <tr>
                            <td><a href="<?= base_url('operations/approvals/requests/' . $e['id']) ?>"><code><?= esc($e['number']) ?></code></a></td>
                            <td><span class="enterprise-data-table__primary"><?= esc($e['title']) ?></span></td>
                            <td><?= esc(ucfirst($e['request_type'])) ?></td>
                            <td><?= esc($e['approver_name'] ?? '—') ?></td>
                            <td><?= $e['amount'] !== null ? number_format((float) $e['amount'], 2) : '—' ?></td>
                            <td><?= $statusPill($e['status']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('operations/approvals/requests/' . $e['id']) ?>" class="btn btn-sm btn-outline-secondary">View</a>
                                <?php if ($e['status'] === 'pending'): ?>
                                    <form action="<?= base_url('operations/approvals/requests/' . $e['id'] . '/cancel') ?>" method="post" class="d-inline"
                                          onsubmit="return confirm('Cancel this request?');">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Cancel</button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">You have not sent any approval requests yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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