<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="wrapper">
        <div class="content"></div>
        <h2 class="text-center mt-4 mb-3">PayPal Transactions</h2>
        <?= $this->include('partials/academic_filter') ?>
        <!-- Export Button -->
        <div class="d-flex gap-2 mb-3 justify-content-end">
            <a href="<?= base_url('admin/paypal-transactions/export') . ($keyword ? '?q=' . urlencode($keyword) : '') ?>"
                class="btn btn-success">
                Export CSV
            </a>
        </div>
        <!-- Search Form -->
        <!--
        <form method="get" action="<?= base_url('admin/paypal-transactions') ?>" class="mb-3">
            <?= csrf_field(); ?>
            <div class="input-group">
                <input type="text" name="q" value="<?= esc($keyword) ?>" class="form-control"
                    placeholder="Search by transaction ID, email, order ID, or event type">
                <button type="submit" class="btn btn-primary">Search</button>
                <?php if ($keyword): ?>
                    <a href="<?= base_url('admin/paypal-transactions') ?>" class="btn btn-secondary">Clear</a>
                <?php endif; ?>
            </div>
        </form>
        -->

        <!-- Transactions Table -->
        <table id="myTable" class="display table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Transaction ID</th>
                    <th>Order ID</th>
                    <th>Parent School ID</th>
                    <th>Email</th>
                    <th>Amount</th>
                    <th>Net Amount</th>
                    <th>Currency</th>
                    <th>Status</th>
                    <th>Event Type</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($transactions as $t): ?>
                    <tr>
                        <td><?= esc($t['id']) ?></td>
                        <td><?= esc($t['transaction_id']) ?></td>
                        <td><?= esc($t['order_id']) ?></td>
                        <td><?= esc($t['parent_school_id']) ?></td>
                        <td><?= esc($t['payer_email']) ?></td>
                        <td>$<?= esc(number_format($t['amount'], 2)) ?></td>
                        <td>$<?= esc(number_format($t['net_amount'], 2)) ?></td>
                        <td><?= esc($t['currency']) ?></td>
                        <td><?= esc($t['status']) ?></td>
                        <td><?= esc($t['event_type']) ?></td>
                        <td><?= esc(local_datetime($t['created_at'], 'm-d-Y H:i')) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <!-- Pagination -->
        <div class="d-flex justify-content-center">
            <?= $pager->links() ?>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    $(document).ready(function() {
        $('#myTable').DataTable({
            "order": [
                [0, "desc"]
            ],
            "pageLength": 10
        });
    });
</script>
<?= $this->endSection() ?>
