<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
?>

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">All Transactions</div>
        <div class="right">
            <a href="<?php echo base_url('admin/dashboard'); ?>" class="headerButton">
                <ion-icon name="home-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Search & Filter -->
        <div class="section mt-2">
            <div class="card">
                <div class="card-body">
                    <form method="GET" action="">
                        <div class="row">
                            <div class="col-8">
                                <div class="form-group basic">
                                    <input type="text" name="search" class="form-control" placeholder="Search by reference..." value="<?php echo htmlspecialchars($filters['search'] ?? ''); ?>">
                                </div>
                            </div>
                            <div class="col-4">
                                <button type="submit" class="btn btn-primary w-100">
                                    <ion-icon name="search-outline"></ion-icon>
                                </button>
                            </div>
                        </div>
                        <div class="row mt-1">
                            <div class="col-6">
                                <select name="type" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Types</option>
                                    <option value="transfer" <?php echo ($filters['type'] ?? '') == 'transfer' ? 'selected' : ''; ?>>Transfer</option>
                                    <option value="deposit" <?php echo ($filters['type'] ?? '') == 'deposit' ? 'selected' : ''; ?>>Deposit</option>
                                    <option value="withdrawal" <?php echo ($filters['type'] ?? '') == 'withdrawal' ? 'selected' : ''; ?>>Withdrawal</option>
                                    <option value="payment" <?php echo ($filters['type'] ?? '') == 'payment' ? 'selected' : ''; ?>>Payment</option>
                                </select>
                            </div>
                            <div class="col-6">
                                <select name="status" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Status</option>
                                    <option value="completed" <?php echo ($filters['status'] ?? '') == 'completed' ? 'selected' : ''; ?>>Completed</option>
                                    <option value="pending" <?php echo ($filters['status'] ?? '') == 'pending' ? 'selected' : ''; ?>>Pending</option>
                                    <option value="failed" <?php echo ($filters['status'] ?? '') == 'failed' ? 'selected' : ''; ?>>Failed</option>
                                    <option value="reversed" <?php echo ($filters['status'] ?? '') == 'reversed' ? 'selected' : ''; ?>>Reversed</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Transactions List -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Transactions (<?php echo number_format($total); ?>)</div>
            
            <?php if (empty($transactions)): ?>
            <div class="card">
                <div class="card-body text-center py-4">
                    <ion-icon name="receipt-outline" style="font-size: 48px; color: #ccc;"></ion-icon>
                    <p class="text-muted mt-2 mb-0">No transactions found</p>
                </div>
            </div>
            <?php else: ?>
            
            <ul class="listview image-listview">
                <?php foreach ($transactions as $txn): ?>
                <?php 
                $is_debit = !empty($txn->source_account_id);
                $icon_color = $is_debit ? 'danger' : 'success';
                $icon_name = $is_debit ? 'arrow-up-outline' : 'arrow-down-outline';
                ?>
                <li>
                    <a href="<?php echo base_url('admin/transaction/' . $txn->id); ?>" class="item">
                        <div class="icon-box bg-<?php echo $icon_color; ?>">
                            <ion-icon name="<?php echo $icon_name; ?>"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo ucfirst($txn->type); ?></strong>
                                <p class="mb-0 small text-muted">
                                    <?php echo $txn->reference; ?><br>
                                    <?php echo date('M d, Y H:i', strtotime($txn->created_at)); ?>
                                </p>
                            </div>
                            <div class="text-end">
                                <span class="text-<?php echo $icon_color; ?>" style="font-weight: 600;">
                                    <?php echo ($is_debit ? '- ' : '+ ') . format_money($txn->amount, $txn->currency); ?>
                                </span>
                                <p class="mb-0">
                                    <span class="badge badge-<?php 
                                        echo $txn->status == 'completed' ? 'success' : 
                                            ($txn->status == 'failed' ? 'danger' : 
                                            ($txn->status == 'reversed' ? 'warning' : 'secondary')); 
                                    ?>">
                                        <?php echo ucfirst($txn->status); ?>
                                    </span>
                                </p>
                            </div>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <!-- Pagination -->
            <?php if ($pages > 1): ?>
            <div class="mt-2 text-center">
                <div class="btn-group">
                    <?php if ($page > 1): ?>
                    <a href="?page=<?php echo $page - 1; ?>&type=<?php echo urlencode($filters['type'] ?? ''); ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-back-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                    
                    <button class="btn btn-secondary btn-sm" disabled>
                        Page <?php echo $page; ?> of <?php echo $pages; ?>
                    </button>
                    
                    <?php if ($page < $pages): ?>
                    <a href="?page=<?php echo $page + 1; ?>&type=<?php echo urlencode($filters['type'] ?? ''); ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-forward-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <?php endif; ?>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

<?php $this->load->view('layouts/footer'); ?>
