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

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

    $res = $conn->query("SELECT * FROM pay_bill_requests WHERE id='$id' AND status='Pending'");

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

        if ($action == 'approve') {
            $conn->query("UPDATE pay_bill_requests SET status='Approved' WHERE id='$id'");
            echo "<h2 style='color:green; text-align:center;'>✅ Bill Approved Successfully!</h2>";
        } else if ($action == 'cancel') {
            // টাকা রিফান্ড করা
            $conn->query("UPDATE signup_table SET balance = balance + $amount WHERE mobile='$user'");
            $conn->query("UPDATE pay_bill_requests SET status='Cancelled' WHERE id='$id'");
            echo "<h2 style='color:red; text-align:center;'>❌ Bill Cancelled & Refunded!</h2>";
        }
    } else {
        echo "<h2 style='text-align:center;'>Invalid Request or Already Processed!</h2>";
    }
}
?>