<?php

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



if (!$conn) {

    die("Connection failed");

}



$sender = $_POST['sender_mobile'];

$receiver = $_POST['receiver_mobile'];

$amount = (double)$_POST['amount'];

$pin = $_POST['pin'];

$method = $_POST['method'];

$acc_type = $_POST['acc_type']; // এটি 'Agent' অথবা 'Personal' আসবে



// ১. চার্জ ক্যালকুলেশন লজিক
$charge = 0;

if ($acc_type == "Agent") {
    // হাজারে ২০ টাকা হিসেবে নিখুঁত ক্যালকুলেশন (২১৭০ টাকায় ৪৩.৪ টাকা চার্জ হবে)
    $charge = ($amount * 20) / 1000;
} else {
    // পার্সোনাল নম্বর লজিক
    if ($amount >= 500 && $amount <= 15000) {
        $charge = 3;
    } else if ($amount > 15000) {
        $charge = 6;
    }
}

$total_deduction = $amount + $charge;



// ২. পিন এবং ব্যালেন্স চেক

$check = $conn->query("SELECT balance FROM signup_table WHERE mobile='$sender' AND pin='$pin'");



if ($check && $row = $check->fetch_assoc()) {

    if ($row['balance'] >= $total_deduction) {

        

        // ব্যালেন্স কাটা (চার্জসহ)

        $conn->query("UPDATE signup_table SET balance = balance - $total_deduction WHERE mobile='$sender'");

        

        // রিকোয়েস্ট সেভ

        $sql = "INSERT INTO send_money_requests (sender_mobile, receiver_mobile, amount, method, acc_type, status) 

                VALUES ('$sender', '$receiver', '$amount', '$method', '$acc_type', 'pending')";

        

        if ($conn->query($sql)) {

            $last_id = $conn->insert_id;



            // টেলিগ্রাম মেসেজ পাঠানো

            $botToken = "8324066534:AAFqwLw5b4IVuSIg3k39kOz8ztkeYBY5zRI";

            $chatId = "7690446447";

            

            $msg = "💸 *New Send Money Request*\n\n"

                 . "📤 Sender: `$sender`\n"

                 . "📥 Receiver: `$receiver`\n"

                 . "💵 Amount: *$amount TK*\n"

                 . "⚡ Charge: *$charge TK*\n"

                 . "🏦 Method: *$method* ($acc_type)";

            

            $approveUrl = "https://mfudevlop.top/batua/approve_send_money.php?id=" . $last_id . "&action=approve";

            $cancelUrl = "https://mfudevlop.top/batua/approve_send_money.php?id=" . $last_id . "&action=cancel";



            $keyboard = [

                'inline_keyboard' => [[

                    ['text' => '✅ Approve', 'url' => $approveUrl],

                    ['text' => '❌ Cancel', 'url' => $cancelUrl]

                ]]

            ];



            $url = "https://api.telegram.org/bot$botToken/sendMessage";

            $post_fields = [

                'chat_id' => $chatId,

                'text' => $msg,

                'parse_mode' => 'Markdown',

                'reply_markup' => json_encode($keyboard)

            ];



            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $result = curl_exec($ch);

            curl_close($ch);



            echo "Success: Request Sent. Charge Applied: $charge TK";

        }

    } else {

        echo "ব্যালেন্স পর্যাপ্ত নয়! (চার্জসহ প্রয়োজন: $total_deduction TK)";

    }

} else {

    echo "ভুল পিন!";

}

?> 