<?php
// list_bulk_payments.php - Bulk Payments from External Providers
include 'layouts/session.php';
include 'layouts/dbconn.php';

// Get filter parameters
$status_filter = isset($_GET['status']) ? $_GET['status'] : 'all';
$provider_filter = isset($_GET['provider']) ? intval($_GET['provider']) : 0;
$reconciliation_filter = isset($_GET['reconciliation']) ? $_GET['reconciliation'] : 'all';
$date_from = isset($_GET['date_from']) ? $_GET['date_from'] : '';
$date_to = isset($_GET['date_to']) ? $_GET['date_to'] : '';
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

// Pagination
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$records_per_page = 20;
$offset = ($page - 1) * $records_per_page;

// Build WHERE clause
$where_conditions = [];
$params = [];
$types = '';

if ($status_filter !== 'all') {
    $where_conditions[] = "bp.allocation_status = ?";
    $params[] = $status_filter;
    $types .= 's';
}

if ($provider_filter > 0) {
    $where_conditions[] = "bp.provider_id = ?";
    $params[] = $provider_filter;
    $types .= 'i';
}

if ($reconciliation_filter !== 'all') {
    $where_conditions[] = "bp.reconciliation_status = ?";
    $params[] = $reconciliation_filter;
    $types .= 's';
}

if (!empty($date_from)) {
    $where_conditions[] = "bp.payment_date >= ?";
    $params[] = $date_from;
    $types .= 's';
}

if (!empty($date_to)) {
    $where_conditions[] = "bp.payment_date <= ?";
    $params[] = $date_to;
    $types .= 's';
}

if (!empty($search)) {
    $where_conditions[] = "(bp.payment_number LIKE ? OR bp.transaction_reference LIKE ? OR esp.provider_name LIKE ?)";
    $search_term = "%{$search}%";
    $params[] = $search_term;
    $params[] = $search_term;
    $params[] = $search_term;
    $types .= 'sss';
}

$where_sql = count($where_conditions) > 0 ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

// Get statistics
$stats_sql = "SELECT 
    COUNT(*) as total_payments,
    SUM(bp.total_amount) as total_received,
    SUM(bp.allocated_amount) as total_allocated,
    SUM(bp.unallocated_amount) as total_unallocated,
    SUM(CASE WHEN bp.allocation_status = 'PENDING' THEN 1 ELSE 0 END) as pending_count,
    SUM(CASE WHEN bp.allocation_status = 'PARTIAL' THEN 1 ELSE 0 END) as partial_count,
    SUM(CASE WHEN bp.allocation_status = 'COMPLETED' THEN 1 ELSE 0 END) as completed_count,
    SUM(CASE WHEN bp.reconciliation_status = 'PENDING' THEN 1 ELSE 0 END) as pending_reconciliation
FROM bulk_payments bp
JOIN external_service_providers esp ON bp.provider_id = esp.provider_id
$where_sql";

if (count($params) > 0) {
    $stmt = $conn->prepare($stats_sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $stats = $stmt->get_result()->fetch_assoc();
} else {
    $stats = $conn->query($stats_sql)->fetch_assoc();
}

// Get total records for pagination
$count_sql = "SELECT COUNT(*) as total 
FROM bulk_payments bp
JOIN external_service_providers esp ON bp.provider_id = esp.provider_id
$where_sql";

if (count($params) > 0) {
    $stmt = $conn->prepare($count_sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $total_records = $stmt->get_result()->fetch_assoc()['total'];
} else {
    $total_records = $conn->query($count_sql)->fetch_assoc()['total'];
}

$total_pages = ceil($total_records / $records_per_page);

// Get bulk payments
$payments_sql = "SELECT 
    bp.bulk_payment_id,
    bp.payment_number,
    bp.payment_date,
    bp.payment_period_start,
    bp.payment_period_end,
    bp.total_amount,
    bp.allocated_amount,
    bp.unallocated_amount,
    bp.payment_method,
    bp.transaction_reference,
    bp.allocation_status,
    bp.reconciliation_status,
    bp.currency,
    esp.provider_name,
    esp.provider_code,
    u.full_name as received_by_name,
    COUNT(pa.allocation_id) as allocation_count,
    bp.created_at
FROM bulk_payments bp
JOIN external_service_providers esp ON bp.provider_id = esp.provider_id
JOIN users u ON bp.received_by = u.user_id
LEFT JOIN payment_allocations pa ON bp.bulk_payment_id = pa.bulk_payment_id 
    AND pa.allocation_status NOT IN ('CANCELLED', 'REJECTED')
$where_sql
GROUP BY bp.bulk_payment_id
ORDER BY bp.payment_date DESC, bp.created_at DESC
LIMIT ? OFFSET ?";

$params[] = $records_per_page;
$params[] = $offset;
$types .= 'ii';

$stmt = $conn->prepare($payments_sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$payments = $stmt->get_result();

// Get providers for filter
$providers_sql = "SELECT provider_id, provider_name, provider_code 
                  FROM external_service_providers 
                  WHERE is_active = 1 
                  ORDER BY provider_name";
$providers = $conn->query($providers_sql);
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Bulk Payments')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet">
    <link href="assets/libs/flatpickr/flatpickr.min.css" rel="stylesheet">
    
    <style>
        .page-header {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            padding: 2rem;
            border-radius: 12px;
            color: white;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            border-left: 4px solid #10b981;
            transition: transform 0.2s;
        }
        
        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .stat-value {
            font-size: 1.75rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 0.25rem;
        }
        
        .stat-label {
            font-size: 0.875rem;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .filter-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 1.5rem;
        }
        
        .payment-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1rem;
            border: 1px solid #e2e8f0;
            transition: all 0.2s;
        }
        
        .payment-card:hover {
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            border-color: #10b981;
        }
        
        .payment-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 1rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #f1f5f9;
        }
        
        .payment-number {
            font-size: 1.125rem;
            font-weight: 600;
            color: #1e293b;
        }
        
        .payment-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
            margin-bottom: 1rem;
        }
        
        .info-item {
            display: flex;
            flex-direction: column;
        }
        
        .info-label {
            font-size: 0.75rem;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 0.25rem;
        }
        
        .info-value {
            font-size: 0.95rem;
            color: #1e293b;
            font-weight: 500;
        }
        
        .allocation-progress {
            background: #f8fafc;
            border-radius: 8px;
            padding: 1rem;
            margin-top: 1rem;
        }
        
        .progress {
            height: 8px;
            border-radius: 4px;
            margin-bottom: 0.5rem;
        }
        
        .progress-labels {
            display: flex;
            justify-content: space-between;
            font-size: 0.875rem;
        }
        
        .badge {
            padding: 0.375rem 0.75rem;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .badge-pending { background: #fef3c7; color: #92400e; }
        .badge-partial { background: #dbeafe; color: #1e40af; }
        .badge-completed { background: #d1fae5; color: #065f46; }
        .badge-reconciled { background: #d1fae5; color: #047857; }
        
        .badge-reconciliation-pending { background: #fef3c7; color: #92400e; }
        .badge-reconciliation-in-progress { background: #dbeafe; color: #1e40af; }
        .badge-reconciliation-completed { background: #d1fae5; color: #065f46; }
        .badge-reconciliation-discrepancy { background: #fee2e2; color: #991b1b; }
        
        .empty-state {
            text-align: center;
            padding: 3rem 1rem;
            color: #64748b;
        }
        
        .empty-state i {
            font-size: 4rem;
            color: #cbd5e1;
            margin-bottom: 1rem;
        }
    </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="row align-items-center">
                            <div class="col">
                                <h3 class="mb-2"><i class="ri-bank-line me-2"></i>Bulk Payments</h3>
                                <p class="mb-0 opacity-75">Payments received from external service providers</p>
                            </div>
                            <div class="col-auto">
                                <a href="list_external_providers.php" class="btn btn-light me-2">
                                    <i class="ri-building-line me-1"></i>Providers
                                </a>
                                <a href="record_bulk_payment.php" class="btn btn-success">
                                    <i class="ri-add-line me-1"></i>Record Payment
                                </a>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card">
                                <div class="stat-value"><?php echo number_format($stats['total_payments']); ?></div>
                                <div class="stat-label">Total Payments</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #059669;">
                                <div class="stat-value">KES <?php echo number_format($stats['total_received'] ?? 0, 2); ?></div>
                                <div class="stat-label">Total Received</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #3b82f6;">
                                <div class="stat-value">KES <?php echo number_format($stats['total_allocated'] ?? 0, 2); ?></div>
                                <div class="stat-label">Total Allocated</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #f59e0b;">
                                <div class="stat-value">KES <?php echo number_format($stats['total_unallocated'] ?? 0, 2); ?></div>
                                <div class="stat-label">Unallocated</div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Additional Stats Row -->
                    <div class="row mb-4">
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #fbbf24;">
                                <div class="stat-value"><?php echo number_format($stats['pending_count']); ?></div>
                                <div class="stat-label">Pending Allocation</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #3b82f6;">
                                <div class="stat-value"><?php echo number_format($stats['partial_count']); ?></div>
                                <div class="stat-label">Partially Allocated</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #10b981;">
                                <div class="stat-value"><?php echo number_format($stats['completed_count']); ?></div>
                                <div class="stat-label">Fully Allocated</div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="stat-card" style="border-left-color: #ef4444;">
                                <div class="stat-value"><?php echo number_format($stats['pending_reconciliation']); ?></div>
                                <div class="stat-label">Pending Reconciliation</div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Filters -->
                    <div class="filter-card">
                        <form method="GET" action="" id="filterForm">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <label class="form-label">Search</label>
                                    <input type="text" class="form-control" name="search" 
                                           placeholder="Payment #, Reference..." 
                                           value="<?php echo htmlspecialchars($search); ?>">
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Allocation Status</label>
                                    <select class="form-select" name="status">
                                        <option value="all" <?php echo $status_filter === 'all' ? 'selected' : ''; ?>>All Status</option>
                                        <option value="PENDING" <?php echo $status_filter === 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                        <option value="PARTIAL" <?php echo $status_filter === 'PARTIAL' ? 'selected' : ''; ?>>Partial</option>
                                        <option value="COMPLETED" <?php echo $status_filter === 'COMPLETED' ? 'selected' : ''; ?>>Completed</option>
                                        <option value="RECONCILED" <?php echo $status_filter === 'RECONCILED' ? 'selected' : ''; ?>>Reconciled</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Reconciliation</label>
                                    <select class="form-select" name="reconciliation">
                                        <option value="all" <?php echo $reconciliation_filter === 'all' ? 'selected' : ''; ?>>All</option>
                                        <option value="PENDING" <?php echo $reconciliation_filter === 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                        <option value="IN_PROGRESS" <?php echo $reconciliation_filter === 'IN_PROGRESS' ? 'selected' : ''; ?>>In Progress</option>
                                        <option value="COMPLETED" <?php echo $reconciliation_filter === 'COMPLETED' ? 'selected' : ''; ?>>Completed</option>
                                        <option value="DISCREPANCY" <?php echo $reconciliation_filter === 'DISCREPANCY' ? 'selected' : ''; ?>>Discrepancy</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">Provider</label>
                                    <select class="form-select" name="provider">
                                        <option value="0">All Providers</option>
                                        <?php while ($provider = $providers->fetch_assoc()): ?>
                                            <option value="<?php echo $provider['provider_id']; ?>" 
                                                    <?php echo $provider_filter == $provider['provider_id'] ? 'selected' : ''; ?>>
                                                <?php echo htmlspecialchars($provider['provider_name']); ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label">Date From</label>
                                    <input type="text" class="form-control datepicker" name="date_from" 
                                           placeholder="Select date" value="<?php echo htmlspecialchars($date_from); ?>">
                                </div>
                            </div>
                            
                            <div class="row g-3 mt-2">
                                <div class="col-md-2">
                                    <label class="form-label">Date To</label>
                                    <input type="text" class="form-control datepicker" name="date_to" 
                                           placeholder="Select date" value="<?php echo htmlspecialchars($date_to); ?>">
                                </div>
                                <div class="col-md-10 d-flex align-items-end justify-content-end gap-2">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="ri-search-line me-1"></i>Filter
                                    </button>
                                    <a href="list_bulk_payments.php" class="btn btn-secondary">
                                        <i class="ri-refresh-line me-1"></i>Reset
                                    </a>
                                    <button type="button" class="btn btn-success" onclick="exportPayments()">
                                        <i class="ri-file-excel-line me-1"></i>Export
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    
                    <!-- Payments List -->
                    <?php if ($payments->num_rows > 0): ?>
                        <?php while ($payment = $payments->fetch_assoc()): 
                            $allocation_percentage = $payment['total_amount'] > 0 
                                ? ($payment['allocated_amount'] / $payment['total_amount']) * 100 
                                : 0;
                        ?>
                            <div class="payment-card">
                                <div class="payment-header">
                                    <div>
                                        <div class="payment-number">
                                            <i class="ri-hashtag"></i><?php echo htmlspecialchars($payment['payment_number']); ?>
                                        </div>
                                        <div class="text-muted small mt-1">
                                            <?php echo htmlspecialchars($payment['provider_name']); ?>
                                            <span class="badge bg-secondary ms-2"><?php echo htmlspecialchars($payment['provider_code']); ?></span>
                                        </div>
                                    </div>
                                    <div class="text-end">
                                        <span class="badge badge-<?php echo strtolower($payment['allocation_status']); ?>">
                                            <?php echo str_replace('_', ' ', $payment['allocation_status']); ?>
                                        </span>
                                        <div class="mt-2">
                                            <span class="badge badge-reconciliation-<?php echo strtolower(str_replace('_', '-', $payment['reconciliation_status'])); ?>">
                                                Reconciliation: <?php echo str_replace('_', ' ', $payment['reconciliation_status']); ?>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="payment-info">
                                    <div class="info-item">
                                        <div class="info-label">Payment Date</div>
                                        <div class="info-value"><?php echo date('d M Y', strtotime($payment['payment_date'])); ?></div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Service Period</div>
                                        <div class="info-value">
                                            <?php echo date('d M', strtotime($payment['payment_period_start'])); ?> - 
                                            <?php echo date('d M Y', strtotime($payment['payment_period_end'])); ?>
                                        </div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Payment Method</div>
                                        <div class="info-value"><?php echo str_replace('_', ' ', $payment['payment_method']); ?></div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Transaction Reference</div>
                                        <div class="info-value"><?php echo htmlspecialchars($payment['transaction_reference'] ?: 'N/A'); ?></div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Total Amount</div>
                                        <div class="info-value">
                                            <strong class="text-success">
                                                <?php echo htmlspecialchars($payment['currency']); ?> 
                                                <?php echo number_format($payment['total_amount'], 2); ?>
                                            </strong>
                                        </div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Allocations</div>
                                        <div class="info-value"><?php echo number_format($payment['allocation_count']); ?> items</div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Received By</div>
                                        <div class="info-value"><?php echo htmlspecialchars($payment['received_by_name']); ?></div>
                                    </div>
                                    <div class="info-item">
                                        <div class="info-label">Recorded On</div>
                                        <div class="info-value"><?php echo date('d M Y H:i', strtotime($payment['created_at'])); ?></div>
                                    </div>
                                </div>
                                
                                <!-- Allocation Progress -->
                                <div class="allocation-progress">
                                    <div class="progress-labels mb-2">
                                        <span><strong>Allocation Progress:</strong></span>
                                        <span class="text-muted"><?php echo number_format($allocation_percentage, 1); ?>%</span>
                                    </div>
                                    <div class="progress">
                                        <div class="progress-bar bg-success" role="progressbar" 
                                             style="width: <?php echo $allocation_percentage; ?>%"
                                             aria-valuenow="<?php echo $allocation_percentage; ?>" 
                                             aria-valuemin="0" aria-valuemax="100">
                                        </div>
                                    </div>
                                    <div class="progress-labels mt-2">
                                        <span class="text-success">
                                            <strong>Allocated:</strong> KES <?php echo number_format($payment['allocated_amount'], 2); ?>
                                        </span>
                                        <span class="text-warning">
                                            <strong>Unallocated:</strong> KES <?php echo number_format($payment['unallocated_amount'], 2); ?>
                                        </span>
                                    </div>
                                </div>
                                
                                <!-- Actions -->
                                <div class="mt-3 d-flex justify-content-end gap-2">
                                    <a href="view_bulk_payment.php?id=<?php echo $payment['bulk_payment_id']; ?>" 
                                       class="btn btn-sm btn-primary">
                                        <i class="ri-eye-line me-1"></i>View Details
                                    </a>
                                    <?php if ($payment['allocation_status'] !== 'COMPLETED'): ?>
                                    <a href="create_payment_allocation.php?bulk_id=<?php echo $payment['bulk_payment_id']; ?>" 
                                       class="btn btn-sm btn-success">
                                        <i class="ri-add-line me-1"></i>Create Allocation
                                    </a>
                                    <?php endif; ?>
                                    <?php if ($payment['reconciliation_status'] === 'PENDING'): ?>
                                    <a href="reconcile_bulk_payment.php?id=<?php echo $payment['bulk_payment_id']; ?>" 
                                       class="btn btn-sm btn-info">
                                        <i class="ri-check-double-line me-1"></i>Reconcile
                                    </a>
                                    <?php endif; ?>
                                    <button onclick="printPayment(<?php echo $payment['bulk_payment_id']; ?>)" 
                                            class="btn btn-sm btn-secondary">
                                        <i class="ri-printer-line me-1"></i>Print
                                    </button>
                                </div>
                            </div>
                        <?php endwhile; ?>
                        
                        <!-- Pagination -->
                        <?php if ($total_pages > 1): ?>
                        <div class="row mt-4">
                            <div class="col-12">
                                <nav>
                                    <ul class="pagination justify-content-center">
                                        <?php if ($page > 1): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?php echo ($page - 1); ?>&status=<?php echo $status_filter; ?>&provider=<?php echo $provider_filter; ?>&reconciliation=<?php echo $reconciliation_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>&search=<?php echo urlencode($search); ?>">
                                                Previous
                                            </a>
                                        </li>
                                        <?php endif; ?>
                                        
                                        <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                                        <li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
                                            <a class="page-link" href="?page=<?php echo $i; ?>&status=<?php echo $status_filter; ?>&provider=<?php echo $provider_filter; ?>&reconciliation=<?php echo $reconciliation_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>&search=<?php echo urlencode($search); ?>">
                                                <?php echo $i; ?>
                                            </a>
                                        </li>
                                        <?php endfor; ?>
                                        
                                        <?php if ($page < $total_pages): ?>
                                        <li class="page-item">
                                            <a class="page-link" href="?page=<?php echo ($page + 1); ?>&status=<?php echo $status_filter; ?>&provider=<?php echo $provider_filter; ?>&reconciliation=<?php echo $reconciliation_filter; ?>&date_from=<?php echo $date_from; ?>&date_to=<?php echo $date_to; ?>&search=<?php echo urlencode($search); ?>">
                                                Next
                                            </a>
                                        </li>
                                        <?php endif; ?>
                                    </ul>
                                </nav>
                                <div class="text-center text-muted">
                                    Showing <?php echo $offset + 1; ?> to <?php echo min($offset + $records_per_page, $total_records); ?> of <?php echo number_format($total_records); ?> payments
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                    <?php else: ?>
                        <div class="payment-card">
                            <div class="empty-state">
                                <i class="ri-bank-line"></i>
                                <h5>No Bulk Payments Found</h5>
                                <p>No bulk payments match your current filters.</p>
                                <a href="list_bulk_payments.php" class="btn btn-primary mt-2">
                                    <i class="ri-refresh-line me-1"></i>Reset Filters
                                </a>
                            </div>
                        </div>
                    <?php endif; ?>
                    
                </div>
            </div>
            
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/sweetalert2/sweetalert2.min.js"></script>
    <script src="assets/libs/flatpickr/flatpickr.min.js"></script>
    
    <script>
        // Initialize Flatpickr
        document.addEventListener('DOMContentLoaded', function() {
            flatpickr('.datepicker', {
                dateFormat: 'Y-m-d',
                allowInput: true
            });
        });
        
        // Print payment
        function printPayment(paymentId) {
            window.open('print_bulk_payment.php?id=' + paymentId, '_blank');
        }
        
        // Export payments
        function exportPayments() {
            const form = document.getElementById('filterForm');
            const formData = new FormData(form);
            const params = new URLSearchParams(formData).toString();
            window.location.href = 'export_bulk_payments.php?' + params;
        }
    </script>
</body>
</html>