<?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'])) {
    // Sanitize input (assuming already validated)
    $ceo = $_POST['ceo'];
    $cname = $_POST['cname'];
    $cphone = $_POST['cphone'];
    $cemail = $_POST['cemail'];
    $caddress = $_POST['caddress'];
    $email = $_POST['email'];
    $user_cur = $_POST['user_cur'];
    $log_id = $_POST['log_id'];

    $renterr = rand(100000, 150000) / 100000;
    $optionr = $_POST['optionr'];
    $rrate = rand(100000, 150000) / 100000;
    $entryt = $_POST['entryt'];
    $entrytime = $_POST['entrytime'];
    $expirytime = $_POST['expirytime'];
    $status = $_POST['status'];
    $signa = $_POST['signa'];
    $asset = $_POST['asset'];
    $duratn = $_POST['duratn'];
    $returnr = $_POST['returnr'];
    $invested = $_POST['invested'];
    $plan = $_POST['plan'];
    $lot = $_POST['lot'];
    $time = $_POST['time'];

    $user_rate2 = $_POST['user_rate2'];
    $user_bal2 = $_POST['user_bal2'];
    $user_iprofit = $_POST['user_iprofit'];

    // Perform actual math
    $ammt = $returnr / $user_rate2;
    $invted = $invested;
    $balll = $user_bal2 + $invted + $ammt;
    $ipro = $user_iprofit + $ammt;

    // PDO update
    $updateQuery = "UPDATE logins SET balance = :balance, iprofit = :iprofit WHERE id = :id";
    $updateStmt = $pdo->prepare($updateQuery);

    $updateStmt->execute([
        ':balance' => $balll,
        ':iprofit' => $ipro,
        ':id' => $log_id
    ]);

    // Result handling
    if ($updateStmt->rowCount() > 0) {
        echo "<div class='alert alert-success'>User balance and profit updated successfully.</div>";
    } else {
        echo "<div class='alert alert-warning'>No rows updated. Check if user ID exists or values are the same.</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 = 'TRADE 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">TRADE 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="140" 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 asset ('.$asset.') of '.number_format($invested,2).' '.$user_cur.' has been successfully traded <i class="fa fa-long-arrow-up" style="font-size:48px;color:green"></i> and your account was credited with '.$user_cur.''.number_format($returnr,2).' . You can login to confirm your transaction and place another 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}";
    }


?>  

              
                          
                    
                    
               


<?php
  $id = $_GET['id'];
  
if (isset($_POST['action'])) {
    $signa = $_POST['signa'];
    $returnr = $_POST['returnr'];
    $status = $_POST['status'];

    $update = $pdo->prepare("UPDATE trade SET signa = :signa, returnr = :returnr, status = :status WHERE id = :id");
    $update->execute([
        'signa' => $signa,
        'returnr' => $returnr,
        'status' => $status,
        'id' => $id
    ]);


    // Prepare success message
    $success = "<div class='alert alert-success' style='margin-bottom:20px;'>trade Confirmed Successfully. <meta http-equiv='refresh' content='3; url=adminverify.php' /></div>";
}

// Fetch the trade data from the database using the provided 'id' from the URL
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$stmt = $pdo->prepare("SELECT * FROM trade WHERE id = :id");
$stmt->execute(['id' => $id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

// Fetch user trade details if available
$usersql = $pdo->prepare("SELECT * FROM trade WHERE id = :id");
$usersql->execute(['id' => $id]);

?>

<hr>

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

<script>
function goBack() {
  window.history.go(-2);
}
</script>

<?php if ($ordrow): ?>



              <div class='main-panel'>
        <div class='content bg-light'>
            <div class='page-inner'>
                
                                <div class='table-responsive'>
             <table  id='myTable' class='table table-striped table-bordered'>




<form action="" style="color:black;" class="reservation-form mb-0 bg-silver-deep p-30" method="post">

    <input type="hidden" class="form-control" name="log_id" value="<?= htmlspecialchars($ordrow['log_id']) ?>" readonly>

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

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

    <input type="hidden" class="form-control" name="user_iprofit" value="<?= htmlspecialchars($ordrow['user_iprofit']) ?>" readonly>

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

    <input type="hidden" class="form-control" name="email" value="<?= htmlspecialchars($ordrow['email']) ?>" readonly>

    <p style="color: black;">Invested Amount</p>
    <input type="" class="form-control" name="invested" value="<?= htmlspecialchars($ordrow['invested']) ?>" readonly><br>

    <p style="color: black;">Return Profit</p>
    <input type="number" class="form-control" name="returnr" required><br>

    <input type="hidden" class="form-control" name="asset" value="<?= htmlspecialchars($ordrow['asset']) ?>">

    <p>Trade Status</p>
    <select name="status" class="form-control" required>
        <option value="">SELECT</option> 
        <option value="1">WIN</option> 
        <option value="2">LOSE</option>
        <option value="0">TRADING</option>
    </select><br>

    <input type="hidden" class="form-control" name="optionr" value="<?= htmlspecialchars($ordrow['optionr']) ?>" readonly>

    <input type="hidden" class="form-control" name="signa" value="<?= htmlspecialchars($ordrow['signa']) ?>" readonly>

             
                     <?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" class="btn btn-success">CONFIRM TRADE</button>
</form>

        
        
<?php endif; ?>
            
       
       
           
    
   
  
  
  
  
  
        
        
        <br> <br> <hr>
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
            
    
              
              
           
             
             
       </div> </div>
            
        
        
        </div></div></div></div>
<br>
            <br>
        </div></div></div></div></div></div>
        
        
        
        <?php
require ("footer.php")
?>
