<?php
session_name("admin_session");
session_start();

include("connection.php");

if (isset($_POST['submit'])) {
    $usdt_type = $_POST['usdt_type'];
    $wallet_address = $_POST['wallet_address'];
    $network = $_POST['network'];

    $usdt_qr = $_FILES['usdt_qr']['name'];
    $usdt_qr_temp = $_FILES['usdt_qr']['tmp_name'];
    $usdt_qr_destination = 'img/' . $usdt_qr;
    
    $currentDateTime = date('Y-m-d H:i:s');
    $added_by = $_SESSION['username'];

    // Check if the combination of bank_name and added_by already exists in the database
    $checkQuery = "SELECT COUNT(*) AS count FROM usdt_payment WHERE usdt_type = '$usdt_type' AND added_by = '$added_by'";
    $result = mysqli_query($conn, $checkQuery);

    if ($result) {
        $row = mysqli_fetch_assoc($result);
        $count = $row['count'];

        if ($count > 0) {
            header("Location: usdt_payment.php?msg=3"); // Bank name already exists for the current user
            exit;
        } else {
            // Perform the insert query using prepared statements
            $insertQuery = "INSERT INTO usdt_payment (added_by, usdt_type, wallet_address, network, usdt_qr, date_time) VALUES ('$added_by', '$usdt_type', '$wallet_address', '$network', '$usdt_qr', '$currentDateTime')";
            $res = mysqli_query($conn, $insertQuery);

            if ($res) {
                move_uploaded_file($usdt_qr_temp, $usdt_qr_destination);
                header("Location: usdt_payment.php?msg=1"); // Inserted successfully
                exit;
            } else {
                header("Location: usdt_payment.php?msg=2"); // Failed to insert
                exit;
            }
        }
    } else {
        header("Location: usdt_payment.php?msg=2"); // Database error
        exit;
    }
}
?>
