<?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;



if (isset($_POST['action'])) {

    // Sanitize and get form inputs
    $cname = $_POST['cname'];
    $cphone = $_POST['cphone'];
    $cemail = $_POST['cemail'];
    $caddress = $_POST['caddress'];

    $trader = $_POST['trader'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $amount = $_POST['amount'];
    $user_rate = $_POST['user_rate'];
    $user_bal = $_POST['user_bal'];
    $user_cur = $_POST['user_cur'];
    $returnr = $_POST['returnr'];

    $log_id = $_POST['log_id'];

    // Calculate the return value
    $retur = 0 + $returnr;

    try {
        // Start a transaction for better data integrity
        $pdo->beginTransaction();

        // Update the user's balance
        $stmt = $pdo->prepare("UPDATE logins SET balance = balance + :retur, icopy = '0' WHERE id = :log_id");
        $stmt->execute([
            'retur' => $retur,
            'log_id' => $log_id
        ]);

        // Commit the transaction
        $pdo->commit();

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

    } catch (PDOException $e) {
        // Rollback the transaction if an error occurs
        $pdo->rollBack();
        $error = "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
    }
}



 // Send email to user after deposit 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 = 'COPY 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">DEPOSIT 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="120" 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 credited with '.$user_cur2.''.number_format($amount, 2).' . You can now login to confirm your transaction and start trading</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>
    </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>
    </td>
    </tr>
    </tbody>
    </table>
    </td>
    </tr>
    </tbody>
    </table>
    </div>
    </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 ID from the URL
$id = $_GET['id'];

// Fetch the row from the 'copied' table based on the ID
$stmt = $pdo->prepare("SELECT * FROM copied WHERE id = :id");
$stmt->execute(['id' => $id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

// Check if the form is submitted
if (isset($_POST['action'])) {
    // Get the posted data
    $returnr = $_POST['returnr'];
    $user_rate = $_POST['user_rate'];

    // Update the 'copied' table
    $stmt = $pdo->prepare("UPDATE copied SET status = 1, returnr = :returnr WHERE id = :id");
    $stmt->execute([
        'returnr' => $returnr * $user_rate,
        'id' => $id
    ]);
}

// Fetch the updated data for the 'copied' table
$stmt = $pdo->prepare("SELECT * FROM copied WHERE id = :id");
$stmt->execute(['id' => $id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

// Check the status before displaying the form
if ($ordrow['status'] != 3) {
    echo "
    <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='' 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/>
                            
                            <label class='form-control'>Trader</label>
                            <input type='text' class='form-control' name='trader' value='" . htmlspecialchars($ordrow['trader']) . "' readonly/><br><br>
                            
                            <label class='form-control'>E-mail</label>
                            <input type='text' class='form-control' name='email' value='" . htmlspecialchars($ordrow['email']) . "' readonly/><br><br>
                            
                            <label class='form-control'>CopyTrade Amount</label>
                            <input type='text' class='form-control' name='amount' value='" . htmlspecialchars($ordrow['amount']) . "' readonly/><br><br>
                            
                            <label class='form-control'>CopyTrade Return</label>
                            <input type='text' class='form-control' name='returnr' value='' required/><br><br>
                            
                            <label class='form-control'>Balance</label>
                            <input type='text' class='form-control' name='user_bal' value='" . htmlspecialchars($ordrow['user_bal']) . "' readonly/><br><br>
                            
                            <input type='hidden' class='form-control' name='user_cur' value='" . htmlspecialchars($ordrow['user_cur']) . "' readonly/>
                            <input type='hidden' class='form-control' name='user_rate' value='" . htmlspecialchars($ordrow['user_rate']) . "' 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' id='btn-mlogin' >CONFIRM COPY </button>

        
     
     
              
   <?php

// Check if the form has been submitted
if (isset($_POST['decline'])) {
    // Retrieve form data safely using PDO
    $declineDEP = $_POST['declineDEP'];
    $log_id = $_POST['log_id'];

    // // Update the 'logins' table, set 'confirm' to the value of 'declineDEP'
    // $stmt = $pdo->prepare("UPDATE logins SET confirm = :declineDEP WHERE id = :log_id");
    // $stmt->execute(['declineDEP' => $declineDEP, 'log_id' => $log_id]);

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

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

// // Update the deposit status to 'declined' (status = 3) if the form is submitted
// if (isset($_POST['decline'])) {
//     $updateStmt = $pdo->prepare("UPDATE deposit SET status = 3 WHERE id = :id");
//     $updateStmt->execute(['id' => $id]);
// }

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

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

// Get the ID from the URL
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Fetch the copy trade data by ID
$stmt = $pdo->prepare("SELECT * FROM copied WHERE id = :id");
$stmt->execute(['id' => $id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

// Handle decline form submission
if (isset($_POST['decline'])) {
    $declineStmt = $pdo->prepare("UPDATE copied SET status = 2 WHERE id = :id");
    $declineStmt->execute(['id' => $id]);
    echo "<div class='alert alert-success'>Trade declined successfully.</div>";
    // Refresh the page or redirect
    $ordrow['status'] = 2; // reflect new status in current session
}

// Only show the form if trade is not already declined
if ($ordrow && $ordrow['status'] != 2) {
    ?>
    <form action="" 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 />
        <button type="submit" name="decline" class="btn btn-danger" id="btn-mlogin">DECLINE TRADE</button>
    </form>
    <?php
}
?>

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