<?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 recharge_requests WHERE id='$id' AND status='pending'");

    if ($res && $row = $res->fetch_assoc()) {
        $userMobile = $row['mobile'];
        $amount = $row['amount'];

        if ($action == 'approve') {
            $conn->query("UPDATE recharge_requests SET status='approved' WHERE id='$id'");
            echo "<h1 style='color:green; text-align:center;'>✅ Recharge Approved!</h1>";

        } else if ($action == 'cancel') {
            // টাকা ফেরত দেওয়া
            $conn->query("UPDATE signup_table SET balance = balance + $amount WHERE mobile='$userMobile'");
            $conn->query("UPDATE recharge_requests SET status='cancelled' WHERE id='$id'");
            echo "<h1 style='color:red; text-align:center;'>❌ Recharge Cancelled & Money Refunded!</h1>";
        }
    } else {
        echo "<h1 style='text-align:center;'>Already processed!</h1>";
    }
}
?>