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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Operations',
    'workspaceCrumbCurrent' => $crumb,
    'workspaceTitle'        => $heroTitle,
    'workspaceDesc'         => $heroDesc,
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Pending Approval</h2>
        <p class="enterprise-data-panel__subtitle"><?= esc($heroDesc) ?></p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url($filterAction) ?>" 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, detail"
                           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>Type</th>
                        <th>Number</th>
                        <th>Detail</th>
                        <th>Date</th>
                        <?php if (! empty($showAmount)): ?>
                            <th class="text-end"><?= esc($amountLabel ?? 'Amount') ?></th>
                        <?php endif; ?>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item): ?>
                        <?php $modalId = 'rejectDoc' . preg_replace('/[^a-zA-Z0-9]/', '', $item['type']) . $item['id']; ?>
                        <tr>
                            <td><?= esc($item['type_label']) ?></td>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($item['number']) ?></code></span></td>
                            <td><?= esc($item['detail']) ?></td>
                            <td><?= esc($item['date'] ?: '—') ?></td>
                            <?php if (! empty($showAmount)): ?>
                                <td class="text-end">
                                    <?php if ($item['amount'] !== null): ?>
                                        <?= number_format((float) $item['amount'], $amountLabel === 'Days' ? 1 : 2) ?>
                                    <?php else: ?>
                                        —
                                    <?php endif; ?>
                                </td>
                            <?php endif; ?>
                            <td class="text-end">
                                <form action="<?= base_url($item['approve_path']) ?>" method="post" class="d-inline" data-tpin-confirm>
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="type" value="<?= esc($item['type']) ?>">
                                    <input type="hidden" name="id" value="<?= (int) $item['id'] ?>">
                                    <button type="submit" class="btn btn-sm btn-success" title="Approve"><i class="fa-solid fa-check"></i></button>
                                </form>
                                <?php if (! empty($item['can_reject']) && ! empty($item['reject_path'])): ?>
                                    <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($item['reject_path']) ?>" method="post" data-tpin-confirm>
                                                    <?= csrf_field() ?>
                                                    <input type="hidden" name="type" value="<?= esc($item['type']) ?>">
                                                    <input type="hidden" name="id" value="<?= (int) $item['id'] ?>">
                                                    <div class="modal-header">
                                                        <h6 class="modal-title">Reject <?= esc($item['type_label']) ?></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>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php
                    $colspan = 5 + (! empty($showAmount) ? 1 : 0);
                    ?>
                    <?php if (count($items) === 0): ?>
                        <tr><td colspan="<?= (int) $colspan ?>" class="text-center text-muted py-5"><?= esc($emptyMessage) ?></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>

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