<?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();
}

// 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();

if ($user_details) {
    $username = $user_details['username'];
    $role_name = $user_details['role_name'];
    $full_name = $user_details['full_name'];
    $email = $user_details['email'];
}

// Get search parameters
$search = $_GET['search'] ?? '';
$fund_filter = $_GET['fund_filter'] ?? '';
$status_filter = $_GET['status_filter'] ?? '';
$date_filter = $_GET['date_filter'] ?? '';
$payment_method_filter = $_GET['payment_method_filter'] ?? '';
$page = $_GET['page'] ?? 1;
$limit = 20;
$offset = ($page - 1) * $limit;

// Build query with filters
$where_conditions = ["ft.transaction_type = 'WITHDRAWAL'"];
$params = [];
$param_types = "";

if (!empty($search)) {
    $where_conditions[] = "(c.first_name LIKE ? OR c.last_name LIKE ? OR ft.transaction_number LIKE ? OR ft.purpose LIKE ?)";
    $search_param = "%$search%";
    $params = array_merge($params, [$search_param, $search_param, $search_param, $search_param]);
    $param_types .= "ssss";
}

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_filter)) {
    $where_conditions[] = "DATE(ft.transaction_date) = ?";
    $params[] = $date_filter;
    $param_types .= "s";
}

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

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

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

// Get withdrawals with pagination
$query = "SELECT ft.*, c.first_name, c.last_name, c.contributor_number, f.fund_name, u.full_name as processed_by_name
          FROM fund_transactions ft 
          LEFT JOIN contributors c ON ft.contributor_id = c.contributor_id 
          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();
$withdrawals = $stmt->get_result();

// Get funds for filter dropdown
$funds_query = "SELECT f.fund_id, f.fund_name FROM funds f WHERE f.is_active = 1 ORDER BY f.fund_name";
$funds_result = $conn->query($funds_query);

// Get withdrawal statistics
$stats_query = "SELECT 
    COUNT(*) as total_withdrawals,
    SUM(CASE WHEN status = 'COMPLETED' THEN 1 ELSE 0 END) as completed_withdrawals,
    SUM(CASE WHEN status = 'PENDING' THEN 1 ELSE 0 END) as pending_withdrawals,
    SUM(CASE WHEN DATE(transaction_date) = CURDATE() THEN amount ELSE 0 END) as today_amount,
    SUM(CASE WHEN DATE(transaction_date) = CURDATE() THEN 1 ELSE 0 END) as today_count,
    SUM(amount) as total_amount
    FROM fund_transactions 
    WHERE transaction_type = 'WITHDRAWAL'";
$stats = $conn->query($stats_query)->fetch_assoc();
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'All Withdrawals - 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, #dc2626 0%, #ef4444 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;
        }

        .payment-badge {
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .payment-cash {
            background: #dcfce7;
            color: #166534;
        }

        .payment-bank {
            background: #ddd6fe;
            color: #5b21b6;
        }

        .payment-mpesa {
            background: #fef3c7;
            color: #92400e;
        }

        .payment-mobile {
            background: #e0e7ff;
            color: #3730a3;
        }

        .payment-cheque {
            background: #f0f9ff;
            color: #0369a1;
        }

        .contributor-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #dc2626;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 0.875rem;
        }

        .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-danger {
            background: #dc2626;
            color: white;
        }

        .btn-danger:hover {
            background: #b91c1c;
            color: white;
        }

        .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;
        }

        .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: #dc2626;
            box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
        }

        .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: #dc2626;
            color: #dc2626;
        }

        .page-link.active {
            background: #dc2626;
            border-color: #dc2626;
            color: white;
        }

        @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;
            }
        }
    </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">
                        <h1 class="page-title">All Withdrawals</h1>
                        <p class="page-subtitle">Monitor and track all fund withdrawals in the system</p>
                    </div>

                    <!-- Stats Row -->
                    <div class="stats-row">
                        <div class="stat-card">
                            <div class="stat-value"><?php echo number_format($stats['total_withdrawals'] ?? 0); ?></div>
                            <div class="stat-label">Total Withdrawals</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value"><?php echo number_format($stats['completed_withdrawals'] ?? 0); ?>
                            </div>
                            <div class="stat-label">Completed</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value"><?php echo number_format($stats['pending_withdrawals'] ?? 0); ?>
                            </div>
                            <div class="stat-label">Pending</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value">KES <?php echo number_format($stats['total_amount'] ?? 0); ?></div>
                            <div class="stat-label">Total Amount</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value"><?php echo number_format($stats['today_count'] ?? 0); ?></div>
                            <div class="stat-label">Today's Count</div>
                        </div>
                        <div class="stat-card">
                            <div class="stat-value">KES <?php echo number_format($stats['today_amount'] ?? 0); ?></div>
                            <div class="stat-label">Today's Amount</div>
                        </div>
                    </div>

                    <!-- Filter Card -->
                    <div class="filter-card">
                        <form method="GET" class="row g-3">
                            <div class="col-md-3">
                                <label class="form-label">Search</label>
                                <input type="text" class="form-control" name="search"
                                    value="<?php echo htmlspecialchars($search); ?>"
                                    placeholder="Search contributor, transaction number...">
                            </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 while ($fund = $funds_result->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">Payment Method</label>
                                <select class="form-select" name="payment_method_filter">
                                    <option value="">All Methods</option>
                                    <option value="CASH" <?php echo $payment_method_filter == 'CASH' ? 'selected' : ''; ?>>Cash</option>
                                    <option value="BANK_TRANSFER" <?php echo $payment_method_filter == 'BANK_TRANSFER' ? 'selected' : ''; ?>>Bank Transfer</option>
                                    <option value="MPESA" <?php echo $payment_method_filter == 'MPESA' ? 'selected' : ''; ?>>M-Pesa</option>
                                    <option value="MOBILE_MONEY" <?php echo $payment_method_filter == 'MOBILE_MONEY' ? 'selected' : ''; ?>>Mobile Money</option>
                                    <option value="CHEQUE" <?php echo $payment_method_filter == 'CHEQUE' ? 'selected' : ''; ?>>Cheque</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label">Date</label>
                                <input type="date" class="form-control" name="date_filter"
                                    value="<?php echo htmlspecialchars($date_filter); ?>">
                            </div>
                            <div class="col-md-1">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-flex gap-2">
                                    <button type="submit" class="btn btn-primary">Filter</button>
                                    <a href="list_withdrawals.php" class="btn btn-outline-secondary">Clear</a>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- Withdrawals Table -->
                    <div class="table-card">
                        <div class="table-header">
                            <h3 class="table-title">Withdrawals List (<?php echo $total_withdrawals; ?> total)</h3>
                            <div class="d-flex gap-2">
                                <a href="process_withdrawal.php" class="btn btn-danger">
                                    <i class="ph-arrow-down-left me-1"></i> Process New Withdrawal
                                </a>
                            </div>
                        </div>

                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>Date & Time</th>
                                    <th>Transaction #</th>
                                    <th>Contributor</th>
                                    <th>Fund</th>
                                    <th>Amount</th>
                                    <th>Payment Method</th>
                                    <th>Purpose</th>
                                    <th>Status</th>
                                    <th>Processed By</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($withdrawals->num_rows > 0): ?>
                                    <?php while ($withdrawal = $withdrawals->fetch_assoc()): ?>
                                        <tr>
                                            <td>
                                                <div style="font-weight: 500; color: #1e293b;">
                                                    <?php echo date('M j, Y', strtotime($withdrawal['transaction_date'])); ?>
                                                </div>
                                                <div style="color: #64748b; font-size: 0.75rem;">
                                                    <?php echo date('g:i A', strtotime($withdrawal['created_at'])); ?>
                                                </div>
                                            </td>
                                            <td>
                                                <span style="font-family: monospace; font-weight: 600; color: #1e293b;">
                                                    <?php echo htmlspecialchars($withdrawal['transaction_number']); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <div class="contributor-avatar me-3">
                                                        <?php echo strtoupper(substr($withdrawal['first_name'], 0, 1) . substr($withdrawal['last_name'], 0, 1)); ?>
                                                    </div>
                                                    <div>
                                                        <div style="font-weight: 600; color: #1e293b;">
                                                            <?php echo htmlspecialchars($withdrawal['first_name'] . ' ' . $withdrawal['last_name']); ?>
                                                        </div>
                                                        <div style="color: #64748b; font-size: 0.75rem;">
                                                            <?php echo htmlspecialchars($withdrawal['contributor_number']); ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>
                                                <span style="font-weight: 500; color: #1e293b;">
                                                    <?php echo htmlspecialchars($withdrawal['fund_name']); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <span style="font-weight: 600; color: #dc2626; font-size: 1rem;">
                                                    -KES <?php echo number_format($withdrawal['amount']); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?php
                                                $payment_class = match ($withdrawal['payment_method']) {
                                                    'CASH' => 'payment-cash',
                                                    'BANK_TRANSFER' => 'payment-bank',
                                                    'MPESA' => 'payment-mpesa',
                                                    'MOBILE_MONEY' => 'payment-mobile',
                                                    'CHEQUE' => 'payment-cheque',
                                                    default => 'payment-cash'
                                                };
                                                ?>
                                                <span class="payment-badge <?php echo $payment_class; ?>">
                                                    <?php echo ucfirst(str_replace('_', ' ', strtolower($withdrawal['payment_method']))); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?php if ($withdrawal['purpose']): ?>
                                                    <span style="color: #64748b;">
                                                        <?php echo htmlspecialchars(substr($withdrawal['purpose'], 0, 30)) . (strlen($withdrawal['purpose']) > 30 ? '...' : ''); ?>
                                                    </span>
                                                <?php else: ?>
                                                    <span style="color: #9ca3af;">-</span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <?php
                                                $status_class = match ($withdrawal['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($withdrawal['status'])); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <span style="color: #64748b;">
                                                    <?php echo htmlspecialchars($withdrawal['processed_by_name'] ?? 'System'); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <div class="d-flex gap-1">
                                                    <a href="view_transaction.php?id=<?php echo $withdrawal['transaction_id']; ?>"
                                                        class="btn btn-primary btn-sm" title="View Details">
                                                        <i class="ph-eye"></i>
                                                    </a>
                                                    <a href="view_contributor.php?id=<?php echo $withdrawal['contributor_id']; ?>"
                                                        class="btn btn-outline-secondary btn-sm" title="View Contributor">
                                                        <i class="ph-user"></i>
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="10" class="text-center" style="padding: 3rem;">
                                            <div style="color: #64748b;">
                                                <i class="ph-arrow-down-left"
                                                    style="font-size: 3rem; margin-bottom: 1rem; display: block;"></i>
                                                <h4>No withdrawals found</h4>
                                                <p>No withdrawals match your current filter criteria.</p>
                                                <a href="process_withdrawal.php" class="btn btn-danger mt-2">
                                                    <i class="ph-arrow-down-left me-1"></i> Process First Withdrawal
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>

                        <!-- Pagination -->
                        <?php if ($total_withdrawals > 0): ?>
                            <div class="pagination-wrapper">
                                <div class="pagination-info">
                                    Showing <?php echo $offset + 1; ?> to
                                    <?php echo min($offset + $limit, $total_withdrawals); ?> of
                                    <?php echo $total_withdrawals; ?> entries
                                </div>

                                <?php if ($total_pages > 1): ?>
                                    <div class="pagination">
                                        <?php if ($page > 1): ?>
                                            <a href="?page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search); ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_filter=<?php echo $date_filter; ?>&payment_method_filter=<?php echo $payment_method_filter; ?>"
                                                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="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_filter=<?php echo $date_filter; ?>&payment_method_filter=<?php echo $payment_method_filter; ?>"
                                                class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                                                <?php echo $i; ?>
                                            </a>
                                        <?php endfor; ?>

                                        <?php if ($page < $total_pages): ?>
                                            <a href="?page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search); ?>&fund_filter=<?php echo $fund_filter; ?>&status_filter=<?php echo $status_filter; ?>&date_filter=<?php echo $date_filter; ?>&payment_method_filter=<?php echo $payment_method_filter; ?>"
                                                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();
            });

            // Search on enter key
            $('input[name="search"]').on('keypress', function (e) {
                if (e.which === 13) {
                    $(this).closest('form').submit();
                }
            });

            // Tooltip initialization
            $('[title]').tooltip();
        });
    </script>

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

</html>

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