<?php 
include "include/header.php";

$sql = "SELECT btc, eth, trx, ltc, bnb, doge, usdterc20, usdttrc20 FROM wallets LIMIT 1";
$result = mysqli_query($myConn, $sql);

if (!$result) {
    die("Query failed: " . mysqli_error($myConn));
}

$wallets = mysqli_fetch_assoc($result);

// Handle form submit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    $username = $_SESSION['user_name'];

    $transaction_id = strtoupper(uniqid('TXN'));
    $amount         = $_POST['amount'] ?? '';
    $wallet_address = $_POST['wallet_address'] ?? '';
    $fullname       = $_POST['fullname'] ?? '';
    $email          = $_POST['email'] ?? '';
    $phone          = $_POST['phone'] ?? '';
    $address        = $_POST['address'] ?? '';

    $currency = $_POST['currency'];

    $type   = 'Receive';
    $status = 'pending';
    $date   = date('Y-m-d H:i:s');
    $crypto_amount = $amount;

    $details = "Name: $fullname | Email: $email | Phone: $phone | Address: $address";

    if (empty($wallet_address) || empty($amount)) {
        echo "<script>alert('Wallet address and amount are required');</script>";
        exit;
    }

    // Upload proof
    $proofPath = '';
    if (!empty($_FILES['Poof_of_payment']['name'])) {

        $uploadDir = 'uploads/';
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . '_' . basename($_FILES['Poof_of_payment']['name']);
        $filePath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES['Poof_of_payment']['tmp_name'], $filePath)) {
            $proofPath = $filePath;
        } else {
            echo "<script>alert('Failed to upload proof');</script>";
            exit;
        }
    }

    // Insert
    $sql = "INSERT INTO transactions
    (transaction_id, username, amount, crypto_amount, currency, type, wallet_address, proof, details, date, status)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = mysqli_prepare($myConn, $sql);

    mysqli_stmt_bind_param(
        $stmt,
        "ssddsssssss",
        $transaction_id,
        $username,
        $amount,
        $crypto_amount,
        $currency,
        $type,
        $wallet_address,
        $proofPath,
        $details,
        $date,
        $status
    );

    if (mysqli_stmt_execute($stmt)) {
        echo "<script>
            alert('Deposit submitted successfully. Transaction ID: $transaction_id');
            window.location.href = window.location.href;
        </script>";
    } else {
        $dbError = mysqli_error($myConn);

    echo "<script>
        alert('Deposit failed. Database error: ' + " . json_encode($dbError) . ");
    </script>";
    }

    mysqli_stmt_close($stmt);
}
?>

<div class="page-content footer-clear">

            <!-- Page Title-->
            <div class="pt-3">
                <div class="page-title d-flex">
                    <div class="align-self-center">
                        <a href="#"
                            data-back-button
                            class="me-3 ms-0 icon icon-xxs bg-theme rounded-s shadow-m">
                            <i class="bi bi-chevron-left color-theme font-14"></i>

                        </a>
                    </div>
                    <div class="align-self-center me-auto">
                        <h1 class="color-theme mb-0 font-18">Order Qphone</h1>
                    </div>
                    <div class="align-self-center ms-auto">
                        <a href="#" data-bs-toggle="offcanvas" data-bs-target="#menu-sidebar"
                            class="icon icon-xxs gradient-highlight color-white shadow-bg shadow-bg-xs rounded-s">
                            <i class="bi bi-list font-20"></i>
                        </a>
                    </div>
                </div>
            </div>




            <link rel="stylesheet" href="card/style.css">
            <script src="card/costom.js"></script>



            <div class="card card-style">
                <div class="content">

                    <div class="text-center">
                        <img src="images/qq.jpg" class="img-fluid rounded-s">
                    </div>


                    <br>
                    <div class="col-12">
                        <a href="dashboard.php" class="w-100 btn-full btn gradient-green shadow-bg shadow-bg-m">Back to Dashboard</a>
                    </div>
                    <br>
                    <div>
                        <a class="btn btn-full bg-highlight btn-s font-11 text-uppercase rounded-xs shadow-m text-uppercase font-700" data-bs-toggle="collapse" href="#collapse1" aria-controls="collapse1">
                            Proceed
                        </a>
                    </div>
                    <div class="collapse" id="collapse1"> 
                        <div class="card card-style">
                            <div class="content">

                                <div id="barscan" style="display: none;">
                                    <div class="col-12 text-center">
                                        <div id="accordion6-3" class="text-center accordion-collapse collapse show" data-bs-parent="#accordion-group-6">
                                            <div class="row text-center">
                                                <div class="col-12 text-center"><img id="qrCodeImage" style="max-width: 50% !important;" src="https://api.qrserver.com/v1/create-qr-code/?data=Ask Admin" class="imghere img-fluid rounded-xxs"></div>
                                            </div>
                                            <h2 class="mb-2">Scan the QR Code above to get Address</h2>
                                        </div>
                                    </div>
                                    <div class="input-group mb-2" style="display: none;" id="copyaddress">
                                        <input value="Contact Admin for Wallet Address" readonly="" style="cursor: pointer;" type="text" class="form-control" id="inlineFormInputGroup" placeholder="Address" fdprocessedid="yqq2pg">
                                        <div class="input-group-prepend">
                                            <div id="copyaddress" class="input-group-text" style="margin-left: 5px;"><i class="bi bi-copy"></i></div>
                                        </div>


                                    </div>

                                    <div class="input-wrapper text-center">
                                        <label class="label" for="amount3">
                                            <h4 class="m-0">Transaction ID:</h4>
                                        </label>
                                        <p style="color:green">
                                            QFS48674776                                        </p>
                                    </div>
                                    <input type="hidden" value="QFS48674776" name="TransactionID">

                                </div>

                                <form id="qphoneform" method="post" enctype="multipart/form-data">
                                    <input type="hidden" name="currency" id="currency">
                                    <br>
                                    <div class="form-custom form-label form-icon mb-3">
                                        <i class="bi bi-currency-bitcoin font-13"></i>
                                        <select name="wallet_address" required class="form-control" id="wallet_address">
                                        <option value="" disabled selected>Select a wallet address</option>

                                        <?php if (!empty($wallets['btc'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['btc']) ?>" data-wallet-name="bitcoin">
                                                Bitcoin (<?= htmlspecialchars($wallets['btc']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['eth'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['eth']) ?>" data-wallet-name="ethereum">
                                                Ethereum (<?= htmlspecialchars($wallets['eth']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['trx'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['trx']) ?>" data-wallet-name="tron">
                                                Tron (<?= htmlspecialchars($wallets['trx']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['ltc'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['ltc']) ?>" data-wallet-name="litecoin">
                                                Litecoin (<?= htmlspecialchars($wallets['ltc']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['bnb'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['bnb']) ?>" data-wallet-name="bnb">
                                                BNB (<?= htmlspecialchars($wallets['bnb']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['doge'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['doge']) ?>" data-wallet-name="dogecoin">
                                                Dogecoin (<?= htmlspecialchars($wallets['doge']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['usdterc20'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['usdterc20']) ?>" data-wallet-name="usdt-erc20">
                                                USDT ERC20 (<?= htmlspecialchars($wallets['usdterc20']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['usdttrc20'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['usdttrc20']) ?>" data-wallet-name="usdt-trc20">
                                                USDT TRC20 (<?= htmlspecialchars($wallets['usdttrc20']) ?>)
                                            </option>
                                        <?php } ?>
                                    
                                        <?php if (!empty($wallets['paypal'])) { ?>
                                            <option value="<?= htmlspecialchars($wallets['paypal']) ?>" data-wallet-name="paypal">
                                                PayPal (<?= htmlspecialchars($wallets['paypal']) ?>)
                                            </option>
                                        <?php } ?>
                                    </select>

                                        <label for="wallet_address" class="color-highlight form-label-always-active">Select Wallet</label>
                                    </div>

                                    <div class="col-12">
                                        <div class="form-custom form-label mb-3 form-icon">
                                            <i class="bi bi-cash font-14"></i>
                                            <input name="amount" type="number" class="form-control rounded-xs" id="amount" value="" placeholder="" />
                                            <label for="amount" class="color-highlight form-label-always-active">Amount</label>
                                            <!-- <span>(required)</span> -->
                                        </div>
                                    </div>

                                    <div class="divider mt-2 mb-4"></div>
                                    <h4 style="margin-bottom: 17px;">Requirement Details for Deposit</h4>

                                    <div class="divider mt-2 mb-4"></div>


                                    <div class="form-custom form-label form-icon mb-3 mt-5">
                                        <i class="bi bi-person-circle font-14"></i>
                                        <input type="text" name="fullname" class="form-control rounded-xs" id="cardholdersname" placeholder="John Doe" />
                                        <label for="cardholdersname" class="color-highlight form-label-always-active">Full Name</label>
                                    </div>



                                    <div class="col-12">
                                        <div class="form-custom form-label mb-3 form-icon">
                                            <i class="bi bi-envelope-open-fill font-14"></i>
                                            <input name="email" type="email" class="form-control rounded-xs" id="email" placeholder="doe@gmail.com" />
                                            <label for="email" class="color-highlight form-label-always-active">Email</label>
                                        </div>
                                    </div>

                                    <div class="col-12">
                                        <div class="form-custom form-label mb-3 form-icon">
                                            <i class="bi bi-telephone-fill font-14"></i>
                                            <input name="phone" type="tel" class="form-control rounded-xs" id="phone" value="" placeholder="+1 234 567 809" />
                                            <label for="phone" class="color-highlight form-label-always-active">Phone Number</label>
                                            <!-- <span>(required)</span> -->
                                        </div>
                                    </div>


                                    <div class="col-12">
                                        <div class="form-custom form-label mb-3 form-icon">
                                            <i class="bi bi-geo-alt-fill font-12"></i>
                                            <textarea name="address" class="form-control rounded-xs" placeholder="Detail your address" id="address"></textarea>
                                            <label for="address" class="form-label-always-active color-highlight font-11">Address</label>
                                        </div>
                                    </div>


                                    <div class="file-data">
                                        <div class="form-custom form-label mb-3 form-icon">
                                            <!-- Dropify File Input -->
                                            <input type="file" id="Poof_of_payment" name="Poof_of_payment" class="dropify" data-height="50" data-default-file="" />
                                            <label for="Poof_of_payment" class="color-highlight form-label-always-active">Attach Prove Of Payment Here</label>
                                        </div>
                                    </div>

                                    <div style="margin-top: 15px; display: none;" class="notes">
                                        <h4 style="color: red;">Note:</h4>
                                        <div class="col-12">
                                            <ul class="mb-0 ps-3">
                                                <li>Send only <b class="texzt"></b> to this wallet address. Sending any other coins may result in permanent loss.</li>
                                                <li>Minimum deposit: 10,000 USD worth of <b class="texzt"></b>.</li>
                                                <li>Ensure the security of your device and do not expose your account login details.</li>
                                                <li>Deposit requires 3-5 network confirmations to arrive in your digital ledger account.</li>
                                            </ul>
                                        </div>
                                    </div>
                                    <button type="submit" name="submit_transaction" class="btn btn-full gradient-highlight">
                                        Pre-Order
                                    </button>

                                </form>

                                <script>
                                    $(document).ready(function() {
                                        $("#inlineFormInputGroup").click(function() {
                                            // Select the text inside the input
                                            $(this).select();

                                            // Copy the selected text to the clipboard
                                            document.execCommand("copy");

                                            // Deselect the text (optional)
                                            $(this).blur();

                                            toastr["success"]("Address successfully copied!");
                                        });

                                        $("#copyaddress").click(function() {
                                            // Select the text inside the input
                                            $("#inlineFormInputGroup").select();

                                            // Copy the selected text to the clipboard
                                            document.execCommand("copy");

                                            // Deselect the text (optional)
                                            $("#inlineFormInputGroup").blur();
                                            toastr["success"]("Address successfully copied!");
                                        });
                                    });
                                </script>
                                <script>
                                    $('#wallet_address').on('change', function() {
                                        var walletAddress = $(this).val();
                                        var selectedWallet = $(this).find('option:selected').text();

                                        // Update QR Code
                                        $('#qrCodeImage').attr('src', 'https://api.qrserver.com/v1/create-qr-code/?data=' + encodeURIComponent(walletAddress));
                                        $('#barscan').css('display', 'block');
                                        $('#copyaddress').css('display', 'flex');

                                        // Update Input Field
                                        $('#inlineFormInputGroup').val(walletAddress);

                                        $('.notes').css('display', 'block');

                                        $('.texzt').text(selectedWallet);
                                    });
                                    
                                    
                                </script>

                        <script>
                        document.getElementById('wallet_address').addEventListener('change', function () {
                            var selected = this.options[this.selectedIndex];
                            var currency = selected.getAttribute('data-wallet-name');
                        
                            document.getElementById('currency').value = currency;
                        });
                        </script>







                            </div>
                        </div>

                        <!-- <div>
                        <br>
                        <h4>User Verification</h4>
                        <h5>Upload your ID card</h5>
                        <p style="margin-bottom: 20px !important;" class="mb-0">
                            (Driving License or Government ID card)<br>
                            Uploading your ID helps as ensure the safety and security of your funds
                        </p>
                    </div> -->
                    </div>



                </div>

            </div>

        </div>
        

<script src="scripts/bootstrap.min.js"></script>
<script src="scripts/custom.js"></script>
</body>