<?php
include 'config.php';

// সার্চ কুয়েরি হ্যান্ডেল করা
$search_query = "";
if (isset($_GET['search']) && !empty($_GET['search'])) {
    $search = $conn->real_escape_string($_GET['search']);
    $search_query = " WHERE customer_id LIKE '%$search%'";
}

$sql = "SELECT * FROM transactions" . $search_query . " ORDER BY id DESC";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Transaction Management</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f9; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; background: #fff; }
        th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
        th { background-color: #4CAF50; color: white; }
        tr:nth-child(even) { background-color: #f2f2f2; }
        .search-box { margin-bottom: 20px; }
        .search-box input[type="text"] { padding: 8px; width: 250px; border: 1px solid #ccc; }
        .search-box button { padding: 8px 15px; background: #4CAF50; color: white; border: none; cursor: pointer; }
        .btn-edit { background: #ff9800; color: white; padding: 5px 10px; text-decoration: none; border-radius: 3px; }
    </style>
</head>
<body>

    <h2>Transaction Database</h2>

    <div class="search-box">
        <form action="" method="GET">
            <input type="text" name="search" placeholder="Enter Customer ID..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
            <button type="submit">Search</button>
            <a href="index.php"><button type="button" style="background:#2196F3;">Reset</button></a>
        </form>
    </div>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Customer ID</th>
                <th>Amount</th>
                <th>Payment Method</th>
                <th>Trx ID</th>
                <th>Payment Date</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $row['id'] . "</td>";
                    echo "<td>" . $row['customer_id'] . "</td>";
                    echo "<td>" . $row['amount'] . "</td>";
                    echo "<td>" . $row['payment_method'] . "</td>";
                    echo "<td>" . $row['trx_id'] . "</td>";
                    echo "<td>" . $row['payment_date'] . "</td>";
                    echo "<td><a class='btn-edit' href='edit.php?id=" . $row['id'] . "'>Edit</a></td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='7' style='text-align:center;'>No Data Found!</td></tr>";
            }
            ?>
        </tbody>
    </table>

</body>
</html>
