<?php
session_start();
include 'layouts/dbconn.php';

// Set timezone to East Africa Time
date_default_timezone_set('Africa/Nairobi');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Get contributor ID
$contributor_id = $_GET['id'] ?? '';
if (!$contributor_id) {
    header('Location: list_contributors.php');
    exit();
}

// Fetch user details
$user_id = $_SESSION['user_id'];
$sql_user_details = "SELECT u.*, ur.role_name 
                     FROM users u 
                     INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                     WHERE u.user_id = ?";
$stmt_user = $conn->prepare($sql_user_details);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$user_details = $stmt_user->get_result()->fetch_assoc();

// Fetch contributor details
$contributor_query = "SELECT c.* FROM contributors c WHERE c.contributor_id = ?";
$stmt = $conn->prepare($contributor_query);
$stmt->bind_param("i", $contributor_id);
$stmt->execute();
$contributor = $stmt->get_result()->fetch_assoc();

if (!$contributor) {
    header('Location: list_contributors.php?error=contributor_not_found');
    exit();
}

// Get filter parameters
$transaction_type_filter = $_GET['transaction_type'] ?? '';
$fund_filter = $_GET['fund_filter'] ?? '';
$status_filter = $_GET['status_filter'] ?? '';
$date_from = $_GET['date_from'] ?? '';
$date_to = $_GET['date_to'] ?? '';
$page = $_GET['page'] ?? 1;
$limit = 20;
$offset = ($page - 1) * $limit;

// Build query with filters
$where_conditions = ["ft.contributor_id = ?"];
$params = [$contributor_id];
$param_types = "i";

if (!empty($transaction_type_filter)) {
    $where_conditions[] = "ft.transaction_type = ?";
    $params[] = $transaction_type_filter;
    $param_types .= "s";
}

if (!empty($fund_filter)) {
    $where_conditions[] = "ft.fund_id = ?";
    $params[] = $fund_filter;
    $param_types .= "i";
}

if (!empty($status_filter)) {
    $where_conditions[] = "ft.status = ?";
    $params[] = $status_filter;
    $param_types .= "s";
}

if (!empty($date_from)) {
    $where_conditions[] = "ft.transaction_date >= ?";
    $params[] = $date_from;
    $param_types .= "s";
}

if (!empty($date_to)) {
    $where_conditions[] = "ft.transaction_date <= ?";
    $params[] = $date_to;
    $param_types .= "s";
}

$where_clause = implode(" AND ", $where_conditions);

// Get total count for pagination
$count_query = "SELECT COUNT(*) as total FROM fund_transactions ft WHERE $where_clause";
$count_stmt = $conn->prepare($count_query);
if (!empty($params)) {
    $count_stmt->bind_param($param_types, ...$params);
}
$count_stmt->execute();
$total_transactions = $count_stmt->get_result()->fetch_assoc()['total'];
$total_pages = ceil($total_transactions / $limit);

// Get transactions with pagination
$query = "SELECT ft.*, f.fund_name, u.full_name as processed_by_name
          FROM fund_transactions ft
          LEFT JOIN funds f ON ft.fund_id = f.fund_id
          LEFT JOIN users u ON ft.processed_by = u.user_id
          WHERE $where_clause
          ORDER BY ft.transaction_date DESC, ft.created_at DESC
          LIMIT ? OFFSET ?";

$stmt = $conn->prepare($query);
$params[] = $limit;
$params[] = $offset;
$param_types .= "ii";

if (!empty($params)) {
    $stmt->bind_param($param_types, ...$params);
}
$stmt->execute();
$transactions = $stmt->get_result();

// Get funds for filter dropdown
$funds_query = "SELECT DISTINCT f.fund_id, f.fund_name 
                FROM fund_transactions ft 
                JOIN funds f ON ft.fund_id = f.fund_id 
                WHERE ft.contributor_id = ? 
                ORDER BY f.fund_name";
$stmt_funds = $conn->prepare($funds_query);
$stmt_funds->bind_param("i", $contributor_id);
$stmt_funds->execute();
$funds = $stmt_funds->get_result();

// Get transaction summary
$summary_query = "SELECT 
    COUNT(*) as total_count,
    SUM(CASE WHEN transaction_type = 'CONTRIBUTION' THEN amount ELSE 0 END) as total_contributions,
    SUM(CASE WHEN transaction_type = 'WITHDRAWAL' THEN amount ELSE 0 END) as total_withdrawals,
    SUM(CASE WHEN transaction_type = 'CONTRIBUTION' THEN 1 ELSE 0 END) as contribution_count,
    SUM(CASE WHEN transaction_type = 'WITHDRAWAL' THEN 1 ELSE 0 END) as withdrawal_count,
    MIN(transaction_date) as first_transaction,
    MAX(transaction_date) as last_transaction
    FROM fund_transactions ft 
    WHERE ft.contributor_id = ?";
$stmt_summary = $conn->prepare($summary_query);
$stmt_summary->bind_param("i", $contributor_id);
$stmt_summary->execute();
$transaction_summary = $stmt_summary->get_result()->fetch_assoc();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Transaction History - Fund Management System')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <style>
        body {
            background-color: #f8fafc;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .page-content {
            padding: 1.5rem;
        }
        
        .page-header {
            background: linear-gradient(135deg, #059669 0%, #10b981 100%);
            color: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .page-title {
            font-size: 1.75rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }
        
        .page-subtitle {
            opacity: 0.9;
            margin: 0;
        }
        
        .filter-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-bottom: 2rem;
        }
        
        .stats-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            text-align: center;
        }
        
        .stat-value {
            font-size: 1.5rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 0.5rem;
        }
        
        .stat-label {
            font-size: 0.875rem;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        .table-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            overflow: hidden;
            margin-bottom: 2rem;
        }
        
        .table-header {
            padding: 1.5rem;
            border-bottom: 1px solid #e2e8f0;
            background: #f8fafc;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .table-title {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
            margin: 0;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin: 0;
        }
        
        .data-table th {
            background: #f8fafc;
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            font-size: 0.875rem;
            color: #64748b;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .data-table td {
            padding: 1rem;
            border-bottom: 1px solid #f1f5f9;
            font-size: 0.875rem;
        }
        
        .data-table tbody tr:hover {
            background: #f8fafc;
        }
        
        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        .status-completed { background: #dcfce7; color: #166534; }
        .status-pending { background: #fef3c7; color: #92400e; }
        .status-cancelled { background: #fee2e2; color: #991b1b; }
        .status-failed { background: #fee2e2; color: #991b1b; }
        
        .transaction-badge {
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }
        
        .transaction-contribution { background: #dcfce7; color: #166534; }
        .transaction-withdrawal { background: #fee2e2; color: #991b1b; }
        .transaction-transfer-in { background: #ddd6fe; color: #5b21b6; }
        .transaction-transfer-out { background: #fef3c7; color: #92400e; }
        .transaction-adjustment { background: #f0f9ff; color: #0369a1; }
        
        .form-control, .form-select {
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 0.75rem;
            font-size: 0.875rem;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
        }
        
        .btn {
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            font-weight: 600;
            font-size: 0.875rem;
            border: none;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-primary {
            background: #3b82f6;
            color: white;
        }
        
        .btn-primary:hover {
            background: #1d4ed8;
            color: white;
        }
        
        .btn-outline-secondary {
            background: transparent;
            border: 1px solid #e2e8f0;
            color: #64748b;
        }
        
        .btn-outline-secondary:hover {
            background: #f8fafc;
            border-color: #64748b;
            color: #64748b;
        }
        
        .btn-danger {
            background: #dc2626;
            color: white;
        }
        
        .btn-danger:hover {
            background: #b91c1c;
            color: white;
        }
        
        .btn-success {
            background: #059669;
            color: white;
        }
        
        .btn-success:hover {
            background: #047857;
            color: white;
        }
        
        .pagination-wrapper {
            padding: 1.5rem;
            background: #f8fafc;
            border-top: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
        }
        
        .pagination-info {
            color: #64748b;
            font-size: 0.875rem;
        }
        
        .pagination {
            display: flex;
            gap: 0.5rem;
            margin: 0;
        }
        
        .page-link {
            padding: 0.5rem 0.75rem;
            background: white;
            border: 1px solid #e2e8f0;
            color: #374151;
            text-decoration: none;
            border-radius: 6px;
            font-size: 0.875rem;
        }
        
        .page-link:hover {
            background: #f8fafc;
            border-color: #3b82f6;
            color: #3b82f6;
        }
        
        .page-link.active {
            background: #3b82f6;
            border-color: #3b82f6;
            color: white;
        }
        
        .contributor-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #059669;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 0.875rem;
        }
        
        .quick-actions {
            display: flex;
            gap: 1rem;
            margin-bottom: 1rem;
            flex-wrap: wrap;
        }
        
        @media (max-width: 768px) {
            .page-content {
                padding: 1rem;
            }
            
            .stats-row {
                grid-template-columns: 1fr;
            }
            
            .table-header {
                flex-direction: column;
                gap: 1rem;
                align-items: stretch;
            }
            
            .data-table {
                font-size: 0.75rem;
            }
            
            .data-table th,
            .data-table td {
                padding: 0.5rem;
            }
            
            .quick-actions {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Header -->
                    <div class="page-header">
                        <div class="d-flex align-items-center">
                            <div class="contributor-avatar me-3">
                                <?php echo strtoupper(substr($contributor['first_name'], 0, 1) . substr($contributor['last_name'], 0, 1)); ?>
                            </div>
                            <div>
                                <h1 class="page-title">Transaction History</h1>
                                <p class="page-subtitle">
                                    <?php echo htmlspecialchars($contributor['first_name'] . ' ' . $contributor['last_name']); ?> • 
                                    <?php echo htmlspecialchars($contributor['contributor_number']); ?>
                                </p>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Actions -->
                    <div class="quick-actions">
                        <a href="process_withdrawal.php?contributor_id=<?php echo $contributor['contributor_id']; ?>" class="btn btn-danger">
                            <i class="ph-arrow-down-left me-2"></i>Process Withdrawal
                        </a>
                        <a href="record_contribution.php?contributor_id=<?php echo $contributor['contributor_id']; ?>" class="btn btn-success">
                            <i class="ph-arrow-up-right me-2"></i>Record Contribution
                        </a>
                        <a href="view_contributor.php?id=<?php echo $contributor['contributor_id']; ?>" class="btn btn-outline-secondary">
                            <i class="ph-arrow-left me-2"></i>Back to Profile
                        </a>
                    </div>

                    <!-- Summary Statistics -->
                    <div class="stats-row">
                        <div class="stat-card">
                            <div class="stat-value"><?php echo $transaction_summary['total_count']; ?></div>
                            <div class="stat-label">Total Transactions</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value"><?php echo $transaction_summary['contribution_count']; ?></div>
                            <div class="stat-label">Contributions</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value"><?php echo $transaction_summary['withdrawal_count']; ?></div>
                            <div class="stat-label">Withdrawals</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value">KES <?php echo number_format($transaction_summary['total_contributions']); ?></div>
                            <div class="stat-label">Total Contributed</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value">KES <?php echo number_format($transaction_summary['total_withdrawals']); ?></div>
                            <div class="stat-label">Total Withdrawn</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value">KES <?php echo number_format($transaction_summary['total_contributions'] - $transaction_summary['total_withdrawals']); ?></div>
                            <div class="stat-label">Net Contribution</div>
                        </div>
                    </div>

                    <!-- Filter Card -->
                    <div class="filter-card">
                        <form method="GET" class="row g-3">
                            <input type="hidden" name="id" value="<?php echo $contributor_id; ?>">
                            
                            <div class="col-md-2">
                                <label class="form-label">Transaction Type</label>
                                <select class="form-select" name="transaction_type">
                                    <option value="">All Types</option>
                                    <option value="CONTRIBUTION" <?php echo $transaction_type_filter == 'CONTRIBUTION' ? 'selected' : ''; ?>>Contribution</option>
                                    <option value="WITHDRAWAL" <?php echo $transaction_type_filter == 'WITHDRAWAL' ? 'selected' : ''; ?>>Withdrawal</option>
                                    <option value="TRANSFER_IN" <?php echo $transaction_type_filter == 'TRANSFER_IN' ? 'selected' : ''; ?>>Transfer In</option>
                                    <option value="TRANSFER_OUT" <?php echo $transaction_type_filter == 'TRANSFER_OUT' ? 'selected' : ''; ?>>Transfer Out</option>
                                    <option value="ADJUSTMENT" <?php echo $transaction_type_filter == 'ADJUSTMENT' ? 'selected' : ''; ?>>Adjustment</option>
                                </select>
                            </div>
                            
                            <div class="col-md-2">
                                <label class="form-label">Fund</label>
                                <select class="form-select" name="fund_filter">
                                    <option value="">All Funds</option>
                                    <?php $funds->data_seek(0); ?>
                                    <?php while ($fund = $funds->fetch_assoc()): ?>
                                        <option value="<?php echo $fund['fund_id']; ?>" <?php echo $fund_filter == $fund['fund_id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($fund['fund_name']); ?>
                                        </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                            
                            <div class="col-md-2">
                                <label class="form-label">Status</label>
                                <select class="form-select" name="status_filter">
                                    <option value="">All Status</option>
                                    <option value="COMPLETED" <?php echo $status_filter == 'COMPLETED' ? 'selected' : ''; ?>>Completed</option>
                                    <option value="PENDING" <?php echo $status_filter == 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                    <option value="CANCELLED" <?php echo $status_filter == 'CANCELLED' ? 'selected' : ''; ?>>Cancelled</option>
                                    <option value="FAILED" <?php echo $status_filter == 'FAILED' ? 'selected' : ''; ?>>Failed</option>
                                </select>
                            </div>
                            
                            <div class="col-md-2">
                                <label class="form-label">Date From</label>
                                <input type="date" class="form-control" name="date_from" value="<?php echo htmlspecialchars($date_from); ?>">
                            </div>
                            
                            <div class="col-md-2">
                                <label class="form-label">Date To</label>
                                <input type="date" class="form-control" name="date_to" value="<?php echo htmlspecialchars($date_to); ?>">
                            </div>
                            
                            <div class="col-md-2">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-flex gap-2">
                                    <button type="submit" class="btn btn-primary">Filter</button>
                                    <a href="contributor_transactions.php?id=<?php echo $contributor_id; ?>" class="btn btn-outline-secondary">Clear</a>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- Transactions Table -->
                    <div class="table-card">
                        <div class="table-header">
                            <h3 class="table-title">Transaction History (<?php echo $total_transactions; ?> total)</h3>
                            <div class="d-flex gap-2">
                                <button onclick="exportTransactions()" class="btn btn-primary">
                                    <i class="ph-download me-1"></i> Export CSV
                                </button>
                            </div>
                        </div>
                        
                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>Date & Time</th>
                                    <th>Transaction #</th>
                                    <th>Fund</th>
                                    <th>Type</th>
                                    <th>Amount</th>
                                    <th>Payment Method</th>
                                    <th>Status</th>
                                    <th>Balance After</th>
                                    <th>Reference</th>
                                    <th>Processed By</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($transactions->num_rows > 0): ?>
                                    <?php while ($transaction = $transactions->fetch_assoc()): ?>
                                    <tr>
                                        <td>
                                            <div style="font-weight: 500; color: #1e293b;">
                                                <?php echo date('M j, Y', strtotime($transaction['transaction_date'])); ?>
                                            </div>
                                            <div style="color: #64748b; font-size: 0.75rem;">
                                                <?php echo date('g:i A', strtotime($transaction['created_at'])); ?>
                                            </div>
                                        </td>
                                        <td>
                                            <span style="font-family: monospace; font-weight: 600; color: #1e293b;">
                                                <?php echo htmlspecialchars($transaction['transaction_number']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="font-weight: 500; color: #1e293b;">
                                                <?php echo htmlspecialchars($transaction['fund_name']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php 
                                            $type_class = match($transaction['transaction_type']) {
                                                'CONTRIBUTION' => 'transaction-contribution',
                                                'WITHDRAWAL' => 'transaction-withdrawal',
                                                'TRANSFER_IN' => 'transaction-transfer-in',
                                                'TRANSFER_OUT' => 'transaction-transfer-out',
                                                'ADJUSTMENT' => 'transaction-adjustment',
                                                default => 'transaction-contribution'
                                            };
                                            ?>
                                            <span class="transaction-badge <?php echo $type_class; ?>">
                                                <?php echo ucfirst(str_replace('_', ' ', strtolower($transaction['transaction_type']))); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="font-weight: 600; color: <?php echo in_array($transaction['transaction_type'], ['WITHDRAWAL', 'TRANSFER_OUT']) ? '#dc2626' : '#059669'; ?>;">
                                                <?php echo in_array($transaction['transaction_type'], ['WITHDRAWAL', 'TRANSFER_OUT']) ? '-' : '+'; ?>KES <?php echo number_format($transaction['amount']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="color: #64748b;">
                                                <?php echo ucfirst(str_replace('_', ' ', strtolower($transaction['payment_method']))); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php 
                                            $status_class = match($transaction['status']) {
                                                'COMPLETED' => 'status-completed',
                                                'PENDING' => 'status-pending',
                                                'CANCELLED' => 'status-cancelled',
                                                'FAILED' => 'status-failed',
                                                default => 'status-pending'
                                            };
                                            ?>
                                            <span class="status-badge <?php echo $status_class; ?>">
                                                <?php echo ucfirst(strtolower($transaction['status'])); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="font-weight: 600; color: #1e293b;">
                                                KES <?php echo number_format($transaction['balance_after']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php if ($transaction['reference_number']): ?>
                                                <span style="font-family: monospace; font-size: 0.75rem; color: #64748b;">
                                                    <?php echo htmlspecialchars($transaction['reference_number']); ?>
                                                </span>
                                            <?php else: ?>
                                                <span style="color: #9ca3af;">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <span style="color: #64748b;">
                                                <?php echo htmlspecialchars($transaction['processed_by_name'] ?? 'System'); ?>
                                            </span>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="10" class="text-center" style="padding: 3rem;">
                                            <div style="color: #64748b;">
                                                <i class="ph-currency-circle-dollar" style="font-size: 3rem; margin-bottom: 1rem; display: block;"></i>
                                                <h4>No transactions found</h4>
                                                <p>No transactions match your current filter criteria.</p>
                                                <a href="contributor_transactions.php?id=<?php echo $contributor_id; ?>" class="btn btn-outline-secondary mt-2">
                                                    Clear Filters
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>

                        <!-- Pagination -->
                        <?php if ($total_transactions > 0): ?>
                            <div class="pagination-wrapper">
                                <div class="pagination-info">
                                    Showing <?php echo $offset + 1; ?> to <?php echo min($offset + $limit, $total_transactions); ?> of <?php echo $total_transactions; ?> entries
                                </div>
                                
                                <?php if ($total_pages > 1): ?>
                                    <div class="pagination">
                                        <?php if ($page > 1): ?>
                                            <a href="?id=<?php echo $contributor_id; ?>&page=<?php echo $page - 1; ?>&transaction_type=<?php echo $transaction_type_filter; ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>" class="page-link">
                                                <i class="ph-caret-left"></i>
                                            </a>
                                        <?php endif; ?>
                                        
                                        <?php 
                                        $start = max(1, $page - 2);
                                        $end = min($total_pages, $page + 2);
                                        
                                        for ($i = $start; $i <= $end; $i++): 
                                        ?>
                                            <a href="?id=<?php echo $contributor_id; ?>&page=<?php echo $i; ?>&transaction_type=<?php echo $transaction_type_filter; ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>" 
                                               class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                                                <?php echo $i; ?>
                                            </a>
                                        <?php endfor; ?>
                                        
                                        <?php if ($page < $total_pages): ?>
                                            <a href="?id=<?php echo $contributor_id; ?>&page=<?php echo $page + 1; ?>&transaction_type=<?php echo $transaction_type_filter; ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>" class="page-link">
                                                <i class="ph-caret-right"></i>
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script>
        $(document).ready(function() {
            // Auto-submit form on filter change
            $('.form-select').on('change', function() {
                $(this).closest('form').submit();
            });
            
            // Date filter change
            $('input[type="date"]').on('change', function() {
                $(this).closest('form').submit();
            });
        });

        function exportTransactions() {
            const params = new URLSearchParams(window.location.search);
            params.set('export', 'csv');
            window.open('export_contributor_transactions.php?' + params.toString(), '_blank');
        }
    </script>

    <script src="assets/js/app.js"></script>
</body>
</html>

<?php $conn->close(); ?>