<?php
session_start();
$user = "educateb_bof-engla";
$pass = "bof-engla.com";
$dbname = "educateb_bof-engla";

$conn = mysqli_connect("localhost", $user, $pass, $dbname);
$sql = "SELECT * FROM site_details";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $email = $row["email"];
            $sitename = $row["site_name"];
            $location = $row["location"];
            $phone = $row["phone"];
        }
}

if (!isset($_SESSION["online_id"])){
    header("Location: login.php");
}
else{
    $online_id = $_SESSION["online_id"];
    $sql = "SELECT online_id, status FROM accounts WHERE online_id='$online_id'";
    $result = mysqli_query($conn, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $chk_id = $row["online_id"];
            $status = $row["status"];
        }
        if($status == "Suspended"){
            header("Location: errortransfer.php");
        }
    }
    $online_id = $_SESSION["online_id"];
    $conn = mysqli_connect("localhost", $user, $pass, $dbname);
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "SELECT * FROM accounts WHERE online_id='$online_id'";
    $result = mysqli_query($conn, $sql);
    
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $online_id = $row["online_id"];
            $balance = $row["balance"];
            $reg_firstname = $row["reg_firstname"];
            $reg_middlename = $row["reg_middlename"];
            $reg_lastname = $row["reg_lastname"];
            $reg_acc_type = $row["reg_acc_type"];
            $account_no = $row["account_no"];
            $reg_phone1 = $row["reg_phone1"];
            $passport = $row["passport"];
            $reg_nos_firstname = $row["reg_nos_firstname"];
            $reg_nos_middlename = $row["reg_nos_middlename"];
            $reg_nos_lastname = $row["reg_nos_lastname"];
            $reg_email = $row["reg_email"];
        }
    }
    if (isset($_POST["btc_confirm"])){
        $tranfer_type = "Bitcoin Transfer";
        $amount = $_POST["amount"];
        $wallet = $_POST["wallet"];
        
        $title = "Transfer Confirmation code";
        
        $pin = mt_rand(100000,999999);
        $_SESSION['vpin'] = $pin;
        $headers = "From: Bank Of England <verify@bof-engla.com>"."\r\n";
        $headers  .= "Reply-To: verify@bof-engla.com"."\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
          $msg_body = "<p style='padding:8px;background-color:#172531'><img src='https://bof-engla.com/images/logo.png' style='width:30%;height:30%'></p><br>";
        $msg_body .= "<p style='font-size:20px'>Use the Pin below to complete your transfer\n\n$pin</p>";
        mail($reg_email,"Transfer Confirmation Code",$msg_body,$headers);
        
        $content = "<h4 class='mt-5' style=''>Bitcoin Transfer</h4>";
        $content .= "<div class='container mt-5'>";
        $content .= "<form action=".htmlspecialchars($_SERVER["PHP_SELF"])." method='post' ><br>";
    $content .= "<label>Transfer Confirmation code</label>";
    $content .= "<input type='text' class='form-control form-control-lg' id='transfer_code' name='vpin' placeholder='Confirmation code'>";
        $content .= "<br>";
    
    $content .= "<input type='hidden' name='transfer_type' value='Bitcoin transfer'>
    <input type='hidden' name='amount' value='$amount'>
    <input type='hidden' name='wallet' value='$wallet'>
    
    <button type='submit' class='btn btn-lg bg-success text-light' name='pin_confirm'>Confirm</button>";
    $content .= "</form>";
    $content .= "</div>";

    $content .= "<div class='container mt-5 '>";
    $content .= " <h3 class='w3-text-yellow'>Don't have confirmation code?</h3>";
    $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Check your email to get the confirmation code</p>";
    $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Confirmation code  helps us to keep your account safe.</p>";
    $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Take note that Confirmation code is a one-time passcode.</p>";
    $content .= "</div>";
    
        

    $content .= "<script>";
    $content .= " document.getElementById('transfer_authorization_panel').style.display = 'block'; ";
    $content .= "</script>";
    }
    
    elseif(isset($_POST['pin_confirm'])){

        $vpin = $_POST["vpin"];
        $transfer_type = $_POST['transfer_type'];
        $wallet = $_POST['wallet'];
        
        $amount = $_POST['amount'];
        
        $description = " $transfer_type of $amount to $wallet/ $remark";
        
        
        $date = date("Y-m-d");
        $time = date("H:i:s");

        $opin = mt_rand(100000,999999);
        $_SESSION['opin'] = $opin;
        if ($vpin != $_SESSION['vpin']){
            $error = "Invalid Pin";
            $title = "Transfer Confirmation code";
            
            
            $content = "<h4 class='mt-4 text-success1 font-weight-bold' style=''>$transfer_type</h4>";
            
            $content .= "<div class='container mt-5'>";
            $content .= "<h4 style='' class='w3-text-red'>$error</h4>";
            $content .= "<form action=".htmlspecialchars($_SERVER["PHP_SELF"])." method='post' ><br>";
        
            $content .= "<label>Transfer Confirmation code</label>";
            $content .= "<input type='text' class='form-control form-control-lg' name='vpin' placeholder='Confirmation code'>";
            $content .= "<br>";
            $content .= "<!-- <div id='transfer_authorization_submit'>Confirm</div> -->";
            $content .= "<input type='hidden' name='transfer_type' value='$transfer_type'>
            <input type='hidden' name='amount' value='$amount'>
            <input type='hidden' name='wallet' value='$wallet'>
            
            <button type='submit' class='btn btn-lg bg-success text-light' value='' name='pin_confirm'>Confirm</button>";
            $content .= "</form>";
            $content .= "</div>";
    

            $content .= "<div class='container mt-5'>";
            $content .= " <h3 class='text-warning'>Don't have confirmation code?</h3>";
            $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Check your email to get the confirmation code</p>";
            $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Confirmation code  helps us to keep your account safe.</p>";
            $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Take note that Confirmation code is a one-time passcode.</p>";
            $content .= "</div>";
        }
        else{
            $cmoney = $balance - $amount;
            
            $sql = "UPDATE accounts SET balance='$cmoney' WHERE online_id='$online_id'";
            if (mysqli_query($conn, $sql)) {
                $amount1 = number_format($amount,2);
                $cmoney1 = number_format($cmoney,2);
                $headers = "From: Bank Of England <info@bof-engla.com>\r\n";
                $headers  .= 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
                $message = "<p style='padding:8px;background-color:#172531'><img src='https://bof-engla.com/images/logo.png' style='width:30%;height:30%'></p><br>
                <p>Hello <b>$reg_firstname</b></p>
                <p> This is a transaction notification on your account</p>
                <table width='550px'cellpadding='4px'>
    
                <tr>
                  <th colspan='2' >TRANSACTION NOTICE</th>
                </tr>

            
            


                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Transaction Type 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2' > $transfer_type
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Amount 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'>GBP $amount1
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Description 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $description 
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Balance 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'>GBP $cmoney1
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Date </td>     
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff; padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $date</td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Time </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $time</td> 
                </tr>






                            </table>
                <br>
                 
                 <br>
                 <p>To view your full account statement login to Bank Of England </p>
                 <br>
                 <br>
                 <p>For additional information about this transaction or any of our services, please contact customer care $phone, $email</p>
                 <br>
                 <br>
                 <p style='background-color:#172531;color:white;font-size:7px;padding:2%'>Your account information is private. Please do not disclose your login credentials to anyone</p>";
                 $sql = "SELECT * FROM accounts WHERE online_id='$online_id'";
                $result = mysqli_query($conn, $sql);
                
                if (mysqli_num_rows($result) > 0) {
                    while($row = mysqli_fetch_assoc($result)) {
                        $online_id = $row["online_id"];
                        $balance = $row["balance"];
                    }
                }
                
                $sql = "INSERT INTO transactions (online_id,transfer_type,description,amount,date,time) VALUES ('$online_id','$transfer_type','$description','$amount','$date','$time')";
                if (mysqli_query($conn, $sql)) {
                    $content = "<div class='container mt-5' style=''>$transfer_type Successful</div>
                    
                        <div id='tranfer_complete' class='container mt-5'>
                        <h3 class='text-success'></h3>
                    <p class='text-muted'><i class='fa fa-lightbulb text-warning'></i>&nbsp;Please note we have initiated the $transfer_type which will take 1-2 working days to credit funds in receiver's account.</p>
                    
                    <h4 class='text-success1 mt-5 font-weight-bold'>Transaction Details</h4>
                    <table class='table table-striped'>
                    <tr>
                    <td>Receivers Wallet Address</td>
                    <td>$wallet</td>
                    </tr>
                    
                    <tr>
                    <td>Fund Amount(GBP)</td>
                    <td>$amount1</td>
                    </tr>
                    <td>Transfer Type</td>
                    <td>$transfer_type</td>
                    </tr>
                    </table>
                      </div>";
                    mail($reg_email,"YOUR TRANSACTION DETAILS",$message,$headers);
                }
                else{
                    $content = "<div id='tranfer_complete' class='container mt-5'>
                        <br>
                <center><h3 style='color:error'>Error Completing Transfer</h3></center>
                  </div>";
                }
            }
            else{
                $content = "<div id='tranfer_complete' class='container mt-5'>
                <br>
                <center><h3 style='color:red'>Error Completing Transfer</h3></center>
                  </div>";
            }
                

        }
    }
    elseif (isset($_POST['otp_confirm'])){
        $pin = $_POST["opin"];
        $transfer_type = $_POST['transfer_type'];
        $wallet = $_POST['wallet'];
        
        $amount = $_POST['amount'];
        
           $description = " $transfer_type of $amount to $wallet";
        
        
        $date = date("Y-m-d");
        $time = date("H:i:s");
        if ($pin != $_SESSION['opin']){
            $error = "Invalid Pin";
        }
        if ($amount > $balance){
            $error = "insufficient Funds";
        }
        if (!isset($error)){
            $cmoney = $balance - $amount;
            
            $sql = "UPDATE accounts SET balance='$cmoney' WHERE online_id='$online_id'";
            if (mysqli_query($conn, $sql)) {
                $amount1 = number_format($amount,2);
                $cmoney1 = number_format($cmoney,2);
                $headers = "From: Bank Of England <info@bof-engla.com>\r\n";
                $headers  .= 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
                $message = "<p style='padding:8px;background-color:#172531'><img src='https://bof-engla.com/images/logo.png' style='width:30%;height:30%'></p><br>
                <p>Hello <b>$reg_firstname</b></p>
                <p> This is a transaction notification on your account</p>
                <table width='550px'cellpadding='4px'>
    
                <tr>
                  <th colspan='2' >TRANSACTION NOTICE</th>
                </tr>

            
            


                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Transaction Type 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2' > $transfer_type
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Amount 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'>GBP $amount1
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Description 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $description 
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Balance 
                  </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'>GBP $cmoney1
                  </td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Date </td>     
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff; padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $date</td>
                </tr>

                <tr>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #e7dede;padding:10px' valign='top' bgcolor='#e7dede'> Time </td>
                  <td style='font-family: sans-serif; font-size: 14px; vertical-align: top; background-color: #3498db; color:#ffffff;padding:10px' fgcolor='#ffffff' valign='top' bgcolor='#3498db' colspan='2'> $time</td> 
                </tr>






                            </table>
                <br>
                 
                 <br>
                 <p>To view your full account statement login to Bank Of England</p>
                 <br>
                 <br>
                 <p>For additional information about this transaction or any of our services, please contact customer care $phone, $email</p>
                 <br>
                 <br>
                 <p style='background-color:#172531;color:white;font-size:7px;padding:2%'>Your account information is private. Please do not disclose your login credentials to anyone</p>";
                $sql = "INSERT INTO transactions (online_id,transfer_type,description,amount,date,time) VALUES ('$online_id','$transfer_type','$description','$amount','$date','$time')";
                if (mysqli_query($conn, $sql)) {
                    $content = "<div class='container mt-5' style=''>$transfer_type Successful</div>
                    
                        <div id='tranfer_complete' class='container mt-5'>
                        <h3 class='text-success'></h3>
                    <p class='text-muted'><i class='fa fa-lightbulb text-warning'></i>&nbsp;Please note we have initiated the international transfer which will take 1-2 working days to credit funds in receiver's account.</p>
                    
                    <h4 class='text-success1 mt-5 font-weight-bold'>Transaction Details</h4>
                    <table class='table table-striped'>
                    <tr>
                    <td>Receivers Wallet Address</td>
                    <td>$wallet</td>
                    </tr>
                    
                    <tr>
                    <td>Fund Amount(GBP)</td>
                    <td>$amount1</td>
                    </tr>
                    <td>Transfer Type</td>
                    <td>$transfer_type</td>
                    </tr>
                    </table>
                      </div>";
                    mail($reg_email,"YOUR TRANSACTION DETAILS",$message,$headers);
                  
                }
                else{
                    $content = "<div id='tranfer_complete' class='container mt-5'>
                        <br>
                <center><h3 style='color:error'>Error Completing Transfer</h3></center>
                  </div>";
                }
            }
            else{
                $content = "<div id='tranfer_complete' class='container mt-5'>
                <br>
                <center><h3 style='color:red'>Error Completing Transfer</h3></center>
                  </div>";
            }   
        }
        
        else{

            if ($_POST["transfer_type"] == 'Bitcoin transfer'){
                $tranfer_type = "Bitcoin Transfer";
                $amount = $_POST["amount"];
                $wallet = $_POST["wallet"];
                
                $title = "Transfer OTP code";
            
            
                $content = "<div class='container mt-5'><h4 class=' text-success1 font-weight-bold' style=''>$transfer_type</h4></div>";
                
                $content .= "<div class='container mt-5 bg-white'>";
                $content .= "<h4 style='' class='text-danger'>$error</h4>";
                $content .= "<form action=".htmlspecialchars($_SERVER["PHP_SELF"])." method='post' ><br>";
            
                $content .= "<label>Transfer OTP code</label>";
                $content .= "<input type='text' class='form-control form-control-lg' name='opin' placeholder='OTP code'>";
                $content .= "<br>";
                $content .= "<!-- <div id='transfer_authorization_submit'>Confirm</div> -->";
                $content .= "<input type='hidden' name='transfer_type' value='$transfer_type'>
                <input type='hidden' name='amount' value='$amount'>
                <input type='hidden' name='wallet' value='$wallet'>
                <button type='submit' class='btn btn-lg bg-success text-light' value='' name='otp_confirm'>Confirm</button>";
                $content .= "</form>";
                $content .= "</div>";
        
    
                $content .= "<div class='container mt-5'>";
                $content .= " <h3 class='w3-text-yellow'>Don't have OTP code?</h3>";
                $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Contact <a style='color: green;text-decoration: none;' href='mailto:$email' target='_blank'>$email</a></p>";
                $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>OTP code  helps us to keep your account safe.</p>";
                $content .= " <p style='font-size: 14px;margin-bottom: 10px;'>Take note that OTP code is a one-time passcode.</p>";
                $content .= "</div>";
            }
            
        }
        
    }
    
    $mcontent = "<div class=\"container mt-5 \" style=\"margin-top:70px\">
            $content
        </div>";
        
    
    
}

?>
<?php
include("accountheader.php");
?>
<div class="header-bg" style="width:100%;overflow:hidden">
    <div class="row">
        <div class="col-sm-3 mob2 shadow bg-light" style="">
            <?php
            include("accountsidebar.php");
            ?>
        </div>
        <div class="col-sm-9 pb-5" style="padding:0px;margin:0px">
            <?php
            include("dashhome.php");
            ?>
            
            <div class="container mt-5">
                <div class="">
                    <? echo $mcontent; ?>
                </div>
            </div>
            
        </div>
    </div>
</div>
<?php
include("footer1.php");
?>