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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => 'Requests for Me',
    'workspaceTitle'        => 'Requests for Me',
    'workspaceDesc'         => 'People sent these requests to you. Approve or reject them here.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Pending Inbox</h2>
        <p class="enterprise-data-panel__subtitle">Only requests assigned to you are listed.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/approvals/inbox') ?>" 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 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>From</th>
                        <th>Title</th>
                        <th>Type</th>
                        <th class="text-end">Amount</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($entries as $e): ?>
                        <?php $modalId = 'rejectReq' . $e['id']; ?>
                        <tr>
                            <td><a href="<?= base_url('operations/approvals/requests/' . $e['id']) ?>"><code><?= esc($e['number']) ?></code></a></td>
                            <td><?= esc($e['requester_name'] ?? '—') ?></td>
                            <td><span class="enterprise-data-table__primary"><?= esc($e['title']) ?></span></td>
                            <td><?= esc(ucfirst($e['request_type'])) ?></td>
                            <td class="text-end"><?= $e['amount'] !== null ? number_format((float) $e['amount'], 2) : '—' ?></td>
                            <td class="text-end">
                                <form action="<?= base_url('operations/approvals/requests/' . $e['id'] . '/approve') ?>" method="post" class="d-inline" data-tpin-confirm>
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-success" title="Approve"><i class="fa-solid fa-check"></i></button>
                                </form>
                                <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#<?= esc($modalId) ?>" title="Reject">
                                    <i class="fa-solid fa-xmark"></i>
                                </button>
                                <div class="modal fade" id="<?= esc($modalId) ?>" tabindex="-1">
                                    <div class="modal-dialog modal-sm">
                                        <div class="modal-content">
                                            <form action="<?= base_url('operations/approvals/requests/' . $e['id'] . '/reject') ?>" method="post" data-tpin-confirm>
                                                <?= csrf_field() ?>
                                                <div class="modal-header">
                                                    <h6 class="modal-title">Reject Request</h6>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <label class="enterprise-field-label">Rejection Reason</label>
                                                    <textarea name="rejection_reason" class="form-control form-control-sm" rows="3" placeholder="Reason (optional)"></textarea>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-sm btn-danger">Reject</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($entries) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No requests waiting for you.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>

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