<?php 

require_once("header.php");
?>




<?php


 $stmt = $pdo->query("SELECT * FROM settings ORDER BY id DESC LIMIT 1");
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($row) {
  // Extract values safely
  
$cphone = $row['phone'];
$curl = $row['url'];
$cdomain = $row['domain'];
$cname = $row['domain_name'];
$cpass = $row['password'];
$cemail = $row['email2'];
$cright = $row['copyright'];
$chost = $row['contact_email'];
$cport = $row['service1'];

    } 
    
    
    

// Include PHPMailer
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

?>


<!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">-->


<?php



    echo "<div class='main-panel'>
        <div class='content bg-light'>
            <div class='page-inner'>
            <div class='table-responsive'>";


if (isset($_POST['action_invest'])) {
    // Sanitize input values
    $ceo        = $_POST['ceo'];
    $cname      = $_POST['cname'];
    $cphone     = $_POST['cphone'];
    $cemail     = $_POST['cemail'];
    $caddress   = $_POST['caddress'];
    $email      = $_POST['email'];

    $user_rate2 = floatval($_POST['user_rate2']);
    $user_bal2  = floatval($_POST['user_bal2']);
    $user_cur2  = $_POST['user_cur2'];

    $amount     = floatval($_POST['amount']);
    $percent    = floatval($_POST['percent']);
    $count      = intval($_POST['count']);
    $log_id     = intval($_POST['log_id']);

    // Calculate new balance
    $perc = ($percent / 100) * $amount;
    $investt = (($user_bal2 + $perc) / $user_rate2);
     $investp = (($iprofit + $perc) / $user_rate2);
    
    
        // Update the balance and investment status in the database
        $stmt = $pdo->prepare("UPDATE logins SET balance = :balance, iprofit = :iprofit, ivest = 0 WHERE id = :log_id");
        $stmt->execute([
            ':balance' => $investt,
            ':iprofit' => $investp,
            ':log_id'  => $log_id
        ]);

        // Optional: Additional updates can go here (commented out in original)
        /*
        $stmt = $pdo->prepare("UPDATE logins SET tot_invest = tot_invest - :amount WHERE id = :log_id");
        $stmt->execute([
            ':amount' => $amount,
            ':log_id' => $log_id
        ]);

        $stmt = $pdo->prepare("UPDATE logins SET tot_percent = tot_percent - :percent WHERE id = :log_id");
        $stmt->execute([
            ':percent' => $percent,
            ':log_id' => $log_id
        ]);
        */

        $success = "<div class='alert alert-success' style='margin-bottom:20px;'>ROI updated Successfully<meta http-equiv='refresh' content='0; url=adminverify.php' /></div>";

  
}




 // Send email to user after trade update via PHPMailer
    $mail = new PHPMailer(true);
    try {
        // SMTP Configuration
        $mail->isSMTP();
        $mail->Host = $chost;  // Change to your email provider's SMTP
        $mail->SMTPAuth = true;
        $mail->Username = $cemail;  // Your email
        $mail->Password = $cpass;  // Your email password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = $cport;

        // Email Settings
        $mail->setFrom($cemail, $cname);
        $mail->addAddress($email);
        $mail->isHTML(true);
        $mail->Subject = 'INVESTMENT NOTIFICATION';
        $mail->Body ='<table style="border-collapse: collapse; width: 98.7903%; height: 427px;" border="1">
<tbody>
<tr style="height: 219px;">
<td style="width: 100%; height: 219px;">
<h2 style="color: #222222; font-size: 19px; font-weight: normal; text-align: center;" align="left">SUBSCRIPTION ALERT</h2>
<p style="text-align: center;"><br /><img id="alttext-image" style="text-align: -webkit-center; margin: 0px; display: inline; float: left;" src="'.$url.'/img/LOGO.png" width="100%" height="200" align="left" /><br /><br /><span id="alttext" style="text-align: -webkit-center;"></span></p>
</td>
</tr>
<tr style="height: 208px;">
<td style="width: 100%; height: 208px;">
<p style="font-family: Georgia, Time, sans-serif; font-size: 40px; color: #222222; line-height: 19px; margin: 0px; text-align: left;" align="center"><span class="specbundle2"><span class="font" style="font-size: 18px !important; line-height: 22px !important;">Your Account has been successfully debited with '.$user_cur2.''.number_format($amount,2).' . You can now login to confirm your transaction and continue with your trade</span></span></p>
<p style="font-family: Georgia, Time, sans-serif; font-size: 40px; color: #222222; line-height: 19px; margin: 0px; text-align: left;" align="center">&nbsp;</p>
<p style="font-family: Georgia, Time, sans-serif; font-size: 40px; color: #222222; line-height: 19px; margin: 0px; text-align: left;" align="center"><span class="specbundle2"><span class="font" style="font-size: 18px !important; line-height: 22px !important;">Status:&nbsp;<strong style="color: #008000; text-decoration: underline;">successfully</strong></span></span></p>
<p style="text-align: left; font-family: Georgia,Time,sans-serif; font-size: 40px; color: #222222; font-weight: normal; line-height: 19px; margin: 0;" align="center"><span class="specbundle2"><span class="font" style="font-size: 18px !important; line-height: 22px !important;">Thanks...</span></span></p>
<p style="text-align: left; font-family: Georgia,Time,sans-serif; font-size: 40px; color: #222222; font-weight: normal; line-height: 19px; margin: 0;" align="center">&nbsp;</p>
<p style="text-align: left; font-family: Georgia,Time,sans-serif; font-size: 40px; color: #222222; font-weight: normal; line-height: 19px; margin: 0;" align="center">&nbsp;</p>
</td>
</tr>
</tbody>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td height="35">
<div class="movableContent" style="padding-top: 0px; position: relative; margin: 0; border: 0px;">
<table style="height: 86px; width: 100%;" border="0" width="100%" cellspacing="0" cellpadding="0" align="center">
<tbody>
<tr style="height: 18px;">
<td style="height: 18px;" height="5">
<table style="border-collapse: collapse; width: 100%;" border="1">
<tbody>
<tr style="height: 10px;">
<td style="height: 10px; width: 99.7951%;" align="center">
<p>&nbsp;</p>
<p><span class="specbundle2"><span class="font" style="font-size: 18px !important; line-height: 22px !important;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px;">Account Support Team</span></span></span></p>
<table style="height: 10px;">
<tbody>
<tr style="height: 10px;">
<td style="background-color: #dc2828; border-radius: 4px; padding: 15px 18px; height: 10px; width: 162.531px;" align="center" bgcolor="#1A54BA">
<div class="contentEditableContainer contentTextEditable" style="margin: 0;">
<div class="contentEditable" style="margin: 0;" align="center"><a class="link2" style="color: #ffffff; font-size: 16px; text-decoration: none;" href="'.$url.'" target="_blank" rel="noopener">Read more about us</a></div>
</div>
</td>
</tr>
</tbody>
</table>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left">&nbsp;</p>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left">&nbsp;</p>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left">&nbsp;</p>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left"><span class="font" style="font-size: 18px !important; line-height: 22px !important;">'.$cname.' service</span></p>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left"><span class="font" style="font-size: 18px !important; line-height: 22px !important;">'.$copyright.'</span></p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr style="height: 18px;">
<td style="height: 18px;" height="15">&nbsp;</td>
</tr>
<tr style="height: 19px;">
<td style="height: 10px;" align="left">&nbsp;</td>
</tr>
<tr style="height: 10px;">
<td style="height: 10px;" height="55">&nbsp;</td>
</tr>
<tr style="height: 10px;">
<td style="height: 10px;" align="center">&nbsp;</td>
</tr>
<tr style="height: 20px;">
<td style="height: 20px;" height="20">
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left">&nbsp;</p>
<p style="color: #cccccc; font-size: 12px; font-weight: normal; line-height: 20px; margin: 0px; text-align: center;" align="left">&nbsp;</p>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<table style="height: 5px; width: 0%;" border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 65px;">
<td style="height: 65px; width: 100%;" height="65">&nbsp;</td>
</tr>
<tr style="height: 18px;">
<td style="border-bottom: 1px solid #dddddd; height: 18px; width: 100%;">&nbsp;</td>
</tr>
<tr style="height: 25px;">
<td style="height: 25px; width: 100%;" height="25">&nbsp;</td>
</tr>
<tr style="height: 24px;">
<td style="height: 24px; width: 100%;">&nbsp;</td>
</tr>
</tbody>
</table>
</div>';

        $mail->send();
        $_SESSION['success'] = "Reset link sent to your email!";
    } catch (Exception $e) {
        $_SESSION['error'] = "Email could not be sent. Error: {$mail->ErrorInfo}";
    }


?>  

          
                    
                    
                    
                    <hr>
      
<?php

// if(isset($success)){
//     echo $success;
// }

if(isset($success)){
    echo"<body onload='goBack()'>";
}

?>
<script>
function goBack() {
  window.history.go(-2);
}
</script>
   
        
          
          
                        
             <?php
// Get the investment ID from the GET request and sanitize it
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Fetch investment details using PDO
$stmt = $pdo->prepare("SELECT * FROM invest WHERE id = :id");
$stmt->execute([':id' => $id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

// If no record found, handle accordingly
if (!$ordrow) {
    echo "<div class='alert alert-warning'>No investment record found.</div>";
    exit;
}

// Handle form submission
if (isset($_POST['action_invest'])) {
    $amount = floatval($_POST['amount']); // Make sure amount is defined

    try {
        $stmt = $pdo->prepare("UPDATE invest SET roi_total = roi_total + :amount, status = 2 WHERE id = :id");
        $stmt->execute([
            ':amount' => $amount,
            ':id'     => $id
        ]);
        echo "<div class='alert alert-success'>ROI updated successfully!</div>";
    } catch (PDOException $e) {
        echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
    }
}

// Display form if investment is not already closed (status != 10)
if ($ordrow['status'] != 10): ?>
    <form action="" method="post" class="reservation-form mb-0 bg-silver-deep p-30" style="color: black;">
        <input type="hidden" name="log_id" class="form-control" value="<?= htmlspecialchars($ordrow['log_id']) ?>" readonly>

        <p>Percentage (%) Invest
            <input type="text" name="percent" class="form-control" value="<?= htmlspecialchars($ordrow['percent']) ?>" readonly>
        </p><br>

        <p>Invested Amount
            <input type="text" name="amount" class="form-control" value="<?= htmlspecialchars($ordrow['amount']) ?>" readonly>
        </p><br>

        <p>Min Investment
            <input type="text" name="min_inv" class="form-control" value="<?= htmlspecialchars($ordrow['min_inv']) ?>" readonly>
        </p><br>

        <p>Max Investment
            <input type="text" name="max_inv" class="form-control" value="<?= htmlspecialchars($ordrow['max_inv']) ?>" readonly>
        </p><br>

        <input type="hidden" name="user_cur2" class="form-control" value="<?= htmlspecialchars($ordrow['user_cur']) ?>" readonly>
        <input type="hidden" name="user_rate2" class="form-control" value="<?= htmlspecialchars($ordrow['user_rate']) ?>" readonly>
        <input type="hidden" name="user_bal2" class="form-control" value="<?= htmlspecialchars($ordrow['user_bal']) ?>" readonly>

        <p>Email
            <input type="text" name="email" class="form-control" value="<?= htmlspecialchars($ordrow['email']) ?>" readonly>
        </p><br>
 
<?php endif; ?>
      
       
 
 <?php

// Prepare and execute the query to fetch settings
$stmt = $pdo->prepare("SELECT * FROM settings ORDER BY id DESC");
$stmt->execute();

// Fetch all settings data
$settings = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($settings as $row) {
    echo "
        <input type='hidden' class='form-control' name='cname' id='details' value='" . htmlspecialchars($row['domain_name']) . "'>
        <input type='hidden' class='form-control' name='cemail' id='details' value='" . htmlspecialchars($row['email2']) . "'>
        <input type='hidden' class='form-control' name='cphone' id='details' value='" . htmlspecialchars($row['phone']) . "'>
        <input type='hidden' class='form-control' name='caddress' id='details' value='" . htmlspecialchars($row['address']) . "'>
    ";
}
?>
 
          
 
 

        <button type='submit' name='action_invest' style='color: black;' class='btn btn-success' id='btn-mlogin' >CONFIRM SUBSCRIPTION </button>
            </form>
            
            
            
     
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          
        
        
        
        
        
        
        
        
        
        
        <br> <br> <hr>
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
    
              
              
           
             
             
       </div> </div>
            
        
        
        </div></div></div></div>
<br>
            <br>
        </div></div></div></div></div></div>
        
        
        
        <?php
require ("footer.php")
?>
