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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Procurement',
    'workspaceCrumbCurrent' => 'Approval Queue',
    'workspaceTitle'        => 'Approval Queue',
    'workspaceDesc'         => 'Review and decide on documents waiting for approval.',
]) ?>

<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">Purchase requests, RFQs, quotations, and purchase orders awaiting review.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('procurement/approval-queue') ?>" 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, supplier, department"
                           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>Supplier / Dept</th>
                        <th>Date</th>
                        <th class="text-end">Total</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item): ?>
                        <?php $modalId = 'rejectDoc' . $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['supplier_or_dept']) ?></td>
                            <td><?= esc($item['date'] ?: '—') ?></td>
                            <td class="text-end"><?= number_format((float) ($item['total'] ?? 0), 2) ?></td>
                            <td class="text-end">
                                <form action="<?= base_url($item['approve_path']) ?>" method="post" class="d-inline">
                                    <?= 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>
                                <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">
                                                <?= 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>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($items) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No documents pending approval.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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