<?php
include 'config.php';

// নির্দিষ্ট আইডির ডেটা তুলে আনা
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $sql = "SELECT * FROM transactions WHERE id = $id";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
}

// আপডেট সাবমিট করা
if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $customer_id = $_POST['customer_id'];
    $amount = $_POST['amount'];
    $payment_method = $_POST['payment_method'];
    $trx_id = $_POST['trx_id'];

    $update_sql = "UPDATE transactions SET 
                    customer_id='$customer_id', 
                    amount='$amount', 
                    payment_method='$payment_method', 
                    trx_id='$trx_id' 
                  WHERE id=$id";

    if ($conn->query($update_sql) === TRUE) {
        echo "<script>alert('Data Updated Successfully!'); window.location.href='index.php';</script>";
    } else {
        echo "Error updating record: " . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Transaction</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 50px; background: #f4f4f9; }
        .form-container { background: white; padding: 20px; max-width: 400px; margin: 0 auto; border-radius: 5px; box-shadow: 0px 0px 10px #ccc; }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; margin-bottom: 5px; }
        .form-group input { width: 100%; padding: 8px; box-sizing: border-box; }
        .btn-submit { background: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; width: 100%; }
    </style>
</head>
<body>

<div class="form-container">
    <h2>Edit Transaction (ID: <?php echo $row['id']; ?>)</h2>
    <form action="" method="POST">
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
        
        <div class="form-group">
            <label>Customer ID</label>
            <input type="text" name="customer_id" value="<?php echo $row['customer_id']; ?>" required>
        </div>
        
        <div class="form-group">
            <label>Amount</label>
            <input type="text" name="amount" value="<?php echo $row['amount']; ?>" required>
        </div>
        
        <div class="form-group">
            <label>Payment Method</label>
            <input type="text" name="payment_method" value="<?php echo $row['payment_method']; ?>" required>
        </div>
        
        <div class="form-group">
            <label>Trx ID</label>
            <input type="text" name="trx_id" value="<?php echo $row['trx_id']; ?>">
        </div>
        
        <button type="submit" name="update" class="btn-submit">Update Data</button>
    </form>
    <br>
    <a href="index.php" style="display:block; text-align:center; color:#2196F3;">Back to List</a>
</div>

</body>
</html>
