<?php

$pageName  = "Withdrawal";
include($_SERVER['DOCUMENT_ROOT'] . "/account/layout/header2.php");

// Ofofonobs Developer WhatsAPP +2348114313795

// Other scripts Available

// Bank Script Developer - Use For Educational Purpose Only

// Other scripts Available


if (isset($_POST['withdraw'])) {
    $amount = $_POST['amount'];
    $crypto_name = $_POST['crypto_name'];
    $wallet_address = $_POST['wallet_address'];
    $acct_email = $_SESSION['acct_email'];

    

    if (empty($amount) || empty($crypto_name) || empty($wallet_address)) {
        toast_alert('danger', 'Fill Required Form');
    } elseif ($amount > $row['acct_balance']) {
        toast_alert('error', 'Insufficient Balance');
    } else {




        $available_balance = ($row['acct_balance'] - $amount);

        $sql = "UPDATE users SET acct_balance=:available_balance WHERE acct_email=:acct_email";
        $addUp = $conn->prepare($sql);
        $addUp->execute([
            'available_balance' => $available_balance,
            'acct_email' => $acct_email
        ]);



        $refrence_id = uniqid();
        $trans_type = "Crypto Withdrawal";
        $transaction_type = "debit";
        $trans_status = "processing";
        
        $sql = "INSERT INTO transactions (amount,refrence_id,acct_email,crypto_id,wallet_address,trans_type,transaction_type,trans_status,image) VALUES(:amount,:refrence_id,:acct_email,:crypto_id,:wallet_address,:trans_type,:transaction_type,:trans_status,:image)";
        $tranfered = $conn->prepare($sql);
        $tranfered->execute([
            'amount' => $amount,
            'refrence_id' => $refrence_id,
            'acct_email' => $acct_email,
            'crypto_id' => $crypto_name,
            'wallet_address' => $wallet_address,
            'trans_type' => $trans_type,
            'transaction_type' => $transaction_type,
            'trans_status' => $trans_status,
            'image' => $n
        ]);



        if (true) {
           
            
            // if ($page['padiwise_sms'] == '1') {
            //     $messageText = "New Crypto Withdrawal";
            //     $recipient = $row['acct_phone'];

            //     $responseBody = send_bulk_sms(array(
            //         'sender_name'=>get_setting('display_name'),
            //         'recipient'=>$recipient,
            //         'reference'=>date('Y').uniqid().rand(1,9),
            //         'message'=>$messageText
            //     ));
            // }

            // $full_name = $row['firstname'] . " " . $row['lastname'];
            // $APP_NAME = WEB_TITLE;
            // $APP_URL = WEB_URL;
            // $SITE_ADDRESS = $page['url_address'];
            // $user_email = $row['acct_email'];
            // $message = $sendMail->WithdrawMsg($full_name, $amount, $trans_type, $trans_status, $refrence_id, $APP_NAME, $APP_URL, $SITE_ADDRESS);
            // // User Email
            // $subject = "Crypto Withdrawal" . "-" . $APP_NAME;
            // $email_message->send_mail($user_email, $message, $subject);

             $msg1 = "
                <div class='alert alert-warning'>
                
                <script type='text/javascript'>
                     
                        function Redirect() {
                        window.location='./wallet.php';
                        }
                        document.write ('');
                        setTimeout('Redirect()', 2080);
                     
                        </script>
                        
                <center><img src='../assets/images/loading.gif' width='180px'  /></center>
                 
                <center>	<strong style='color:black;'>Your withdrawal is processing... Please wait while we redirect you!
                       </strong></center>
                
                  </div>
                ";
        } else {
            toast_alert("error", "Sorry Something Went Wrong !");
        }
    }
}





?>


 <div class="container anuphan bottom-padding">
        <!-- --- -->
        <!-- --- -->
        <div class="d-block d-md-none mt-3">
            <i class="material-icons grey-color-2 add-radius" style="font-size: 22px !important;" onclick="history.go(-1)">keyboard_arrow_left</i>
        </div>
        <!-- --- -->
        <!-- --- -->

        <div class="row">
            <div class="col-lg-6">
                <form  method="POST" id="depositForm" enctype="multipart/form-data">
                    
                    <div class="mx-auto add-radius p-md-4 p-0 mt-4 shadow-none">
                        <h4 class="anuphan-3"><?= $page['url_name'] ?> - Withdraw Crypto</h4>
                        
                        <center> <?php if (isset($msg1)) echo $msg1; ?></center>
                        
                        <div class="form-group mt-4">
                            <div class="d-flex">
                                <label class="grey-color anuphan-2 xsmall flex-fill">Amount (USD)</label>
                                <small class="xsmall anuphan-2 grey-color-3">
                                    Total Balance : 
                                    <span class="wa-bal roboto grey-color-3"><?= $page['currency'] ?><?php echo number_format($acct_balance, 2, '.', ','); ?></span>
                                    <span class="wst roboto grey-color-3">..</span>
                                </small>
                            </div>
                            <div class="form-wrapper m-0">
                                <input type="number" name="amount" id="withdrawAmount" class="has-icon roboto" style="font-weight: bold;" placeholder="0.00" step="any" required>
                                <span class="input-icon-wrapper pointer">
                                    <small class="input-icon py-2 primary-color bold-text" onclick="setMax()">Max</small>
                                </span>
                            </div>
                           
                        </div>
                        
                        
                        <label class="xsmall anuphan-2 mt-4 grey-color-2">Select Coin</label>
    
                        <div class="form-wrapper m-0">
                       <select name="crypto_name" required class="add-radius-sm" style="font-weight: bold; font-family: 'roboto' !important;"  >
                            <option value="">Select Crypto Type</option>
                            <?php
                            $sql = $conn->query("SELECT * FROM crypto_currency ORDER BY crypto_name");
                            while ($rs = $sql->fetch(PDO::FETCH_ASSOC)) {
                                $data[] = array(
                                    'id' => $rs['id'],
                                    'wallet_address' => $rs['wallet_address']
                                );
                            ?>
                                <option value="<?= $rs['id'] ?>"><?= ucwords($rs['crypto_name']) ?></option>
                            <?php
                            }
                            ?>
                        </select>
                        
                        </div>
                        
                        <p class="xsmall grey-color-2 anuphan-2 mt-2 mb-0">
                            Cryptocurrency not listed? 
                            <a href="mailto:<?= $page['url_email']?>" target="_blank" class="primary-color" style="text-decoration: underline !important;">Request currency listing here</a>
                        </p>
            
                        
                        
                        
                       
                           
                           
                           
                           
                           <div class="form-group mt-4">
                            <label class="anuphan-2 grey-color xsmall">Withdrawal Address</label>
                            <div class="form-wrapper m-0">
                                <input type="text" id="beneficiary" name="wallet_address" placeholder="Enter crypto address" step="any" required>
                            </div>
                        </div>
                           
                           
                          
                             
                             
                             
                             
                        <button type="submit" name="withdraw" class="xsmall mt-4 anuphan-2 fit">Withdraw Funds</button>
                        
                         <p class="xsmall grey-color-2 anuphan-2 mt-2 mb-0">
                           Crypto withdrawl processing might be longer than 45 mins.
                        </p>
                        
                    </div>
                </form>


               
            </div>

            <div class="col-md-2 d-none d-md-block"></div>

            <div class="col-lg-4">
                <p class="anuphan-3 grey-color mt-5 mb-4">Recent Withdrawal</p>
                
                 <?php

            $sql2 = "SELECT * FROM transactions LEFT JOIN crypto_currency ON transactions.crypto_id = crypto_currency.id WHERE trans_type='Crypto Withdrawal' AND acct_email =:acct_email AND NOT (trans_type='Crypto Deposit') ORDER BY trans_id DESC LIMIT 5";
         
            $wire = $conn->prepare($sql2);
            $wire->execute([
                'acct_email' => $_SESSION['acct_email']
            ]);



            $sn = 1;

            while ($result = $wire->fetch(PDO::FETCH_ASSOC)) {


               


            ?>
            
            
            
             <a href="#">
                    <div class="post-box mb-3 pointer px-3 add-radius-sm orders-border" style="border: none;">
                        <div class="row">
                            <div class="col-12 border-bottom pb-2">
                                <p class="xsmall2 m-0">Status : <span class="badge badge-dark"><?= $result['trans_status'] ?></span></p>
                            </div>
                            <div class="col-4">
                                <small class="xsmall2 bold-text">Ref</small>
                                <p class="m-0 xsmall2 grey-color-3">#<?= $result['refrence_id'] ?></p>
                            </div>
                            <div class="col-4">
                                <small class="xsmall2 bold-text">Date</small>
                                <p class="m-0 xsmall2 grey-color"><?= $result['created_at'] ?></p>
                            </div>
                            <div class="col-4">
                                <small class="xsmall2 bold-text">Value</small>
                                <p class="m-0 xsmall2 roboto bold-text grey-color"><?= $page['currency'] ?><?= $result['amount'] ?> <?= $result['short_name'] ?></p>
                            </div>
                        </div>
                    </div>
                </a>
            
             <?php

            }



            ?>
                
                
                
                
                
                
                
                
                <?php
            $sql2 = "SELECT * FROM transactions WHERE acct_email =:acct_email  AND NOT (trans_type='Crypto Deposit')";
            $stmt = $conn->prepare($sql2);
            $stmt->execute([
                'acct_email' => $_SESSION['acct_email']
            ]);

            $rowCheck = $stmt->fetch(PDO::FETCH_ASSOC);

            if ($stmt->rowCount() == 0) {
            ?>
            
            <div class="py-5 text-center">
                        <div>
                            <svg height="90px" width="90px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" fill="none" class="mirror css-z12rr9"><path fill-rule="evenodd" clip-rule="evenodd" d="M64 8H26v80h58V28H64V8zM36 37h38v4H36v-4zm0 9h38v4H36v-4zm38 9H36v4h38v-4zm-8 12l4 4-4 4-4-4 4-4zM50 18l-3 3 3 3 3-3-3-3z" fill="url(#not-found-data_svg__paint0_linear_22059_32288)"></path><path opacity="0.3" d="M86 50l3-3 3 3-3 3-3-3zM47 21l3-3 3 3-3 3-3-3zM84 28H64V8l20 20z" fill="#929AA5"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M4.172 73.171l14.5-14.5 5.656 5.658-14.5 14.5-5.656-5.657z" fill="url(#not-found-data_svg__paint1_linear_22059_32288)"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M51 48c0-8.837-7.163-16-16-16s-16 7.163-16 16 7.163 16 16 16 16-7.163 16-16zm4 0c0-11.046-8.954-20-20-20s-20 8.954-20 20 8.954 20 20 20 20-8.954 20-20z" fill="url(#not-found-data_svg__paint2_linear_22059_32288)"></path><defs><linearGradient id="not-found-data_svg__paint0_linear_22059_32288" x1="55" y1="8" x2="55" y2="88" gradientUnits="userSpaceOnUse"><stop stop-color="#929AA5" stop-opacity="0.1"></stop><stop offset="1" stop-color="#929AA5" stop-opacity="0.25"></stop></linearGradient><linearGradient id="not-found-data_svg__paint1_linear_22059_32288" x1="4.172" y1="68.75" x2="24.328" y2="68.75" gradientUnits="userSpaceOnUse"><stop stop-color="#929AA5"></stop><stop offset="1" stop-color="#76808F"></stop></linearGradient><linearGradient id="not-found-data_svg__paint2_linear_22059_32288" x1="15" y1="48" x2="55" y2="48" gradientUnits="userSpaceOnUse"><stop stop-color="#929AA5"></stop><stop offset="1" stop-color="#76808F"></stop></linearGradient></defs></svg>
                        </div>
                        <p class="xsmall mt-2 grey-color-3">
                            No recent record.
                        </p>
                    </div>
            
            
              <?php
            } else {

            ?>


            <?php
            }
            ?>
                
               
               
                
              
            </div>
        </div>

    </div>


<br><br>

    <!-- ==== -->
    

 

    </div>
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/account/layout/footer2.php");

?>