<?php
$conn = mysqli_connect('localhost', 'mfudevlo_batuawallet', 'batua@wallet6602#', 'mfudevlo_batuawalletdb');

if (isset($_GET['id']) && isset($_GET['action'])) {
    $id = mysqli_real_escape_string($conn, $_GET['id']);
    $action = $_GET['action'];

    // রিকোয়েস্ট চেক করা
    $res = $conn->query("SELECT * FROM offer_requests WHERE id='$id' AND status='pending'");

    if ($res && $row = $res->fetch_assoc()) {
        $sender = $row['sender_mobile'];
        $amount = $row['amount'];
        $offer = $row['offer_name'];

        if ($action == 'approve') {
            // স্ট্যাটাস আপডেট
            $conn->query("UPDATE offer_requests SET status='Approved' WHERE id='$id'");
            echo "<h1 style='color:green; text-align:center;'>✅ Offer Approved Successfully!</h1>";
            echo "<p style='text-align:center;'>User: $sender | Offer: $offer</p>";

        } else if ($action == 'cancel') {
            // টাকা রিফান্ড করা এবং স্ট্যাটাস আপডেট
            $conn->query("UPDATE signup_table SET balance = balance + $amount WHERE mobile='$sender'");
            $conn->query("UPDATE offer_requests SET status='Cancelled' WHERE id='$id'");
            echo "<h1 style='color:red; text-align:center;'>❌ Offer Cancelled & Refunded!</h1>";
            echo "<p style='text-align:center;'>$amount TK has been returned to $sender</p>";
        }
    } else {
        echo "<h1 style='text-align:center;'>Already processed or invalid request!</h1>";
    }
}
?>