<?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 ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    // Sanitize inputs using PDO prepared statements
    $cname      = $_POST['cname'];
    $cphone     = $_POST['cphone'];
    $cemail     = $_POST['cemail'];
    $caddress   = $_POST['caddress'];

    $email      = $_POST['email'];
    $cur        = $_POST['cur'];
    $coin       = $_POST['coin'];
    $methid     = $_POST['methid'];
    $username   = $_POST['username'];
    $amount     = $_POST['amount'];
    $amt         = $_POST['amt'];
    $user_rate2 = $_POST['user_rate2'];
    $user_bal2  = $_POST['user_bal2'];
    $user_cur2  = $_POST['user_cur2'];
    $log_id     = $_POST['log_id'];

    // Calculate withdrawal in default currency
    // $amt = $amount / $user_rate2;

    // Update the user’s withdrawal in the database
    $stmt = $pdo->prepare("UPDATE logins 
        SET balance = balance - :amt,
            tot_withdraw = tot_withdraw + :amt,
            withdraw = :amt,
            cwithdraw = cwithdraw + 1,
            confirmPAY = 2 
        WHERE id = :log_id");

    $stmt->execute([
        ':amt'   => $amt,
        ':log_id' => $log_id
    ]);

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

    
    
    
    
    // ============= SEND EMAIL NOTIFICATION =============
    
 // 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 = 'WITHDRAWAL 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">WITHDRAWAL 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 check your account summary.</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

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

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

// Check if record exists
// if (!$ordrow) {
//     echo "<div class='alert alert-danger'>Withdrawal record not found.</div>";
//     exit;
// }

// Process the status update if form is submitted
if (isset($_POST['action'])) {
    $stmt = $pdo->prepare("UPDATE withdraw SET status = 2 WHERE id = :id");
    if ($stmt->execute([':id' => $id])) {
        echo "<div class='alert alert-success'>Withdrawal marked as processed.</div>";
    } else {
        // echo "<div class='alert alert-danger'>Failed to update withdrawal status.</div>";
    }
}

// Only show form if status is not '10'
if ($ordrow['status'] != 10) {
?>




<hr>

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

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


<div class="main-panel">
    <div class="content bg-light">
        <div class="page-inner">
            <div class="table-responsive">
                <form action="" method="post" class="reservation-form mb-0 bg-silver-deep p-30" >
                   
           <input type="hidden" name="log_id" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['log_id']) ?>" readonly>
<input type="hidden" name="email" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['email']) ?>" readonly>
 <input type="hidden" name="wallet" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['wallet']) ?>" readonly>
<input type="hidden" name="method" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['method']) ?>" readonly>
 
 <input type="hidden" name="amount" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['amount']) ?>" readonly>
                   
                   
<input type="hidden" name="amt" class="form-control" 
                   style="color: blue;" 
                   value="<?= htmlspecialchars($ordrow['amt']) ?>" readonly>
                   
             

    <!-- Hidden values -->
    <input type="hidden" name="user_cur2" value="<?= htmlspecialchars($ordrow['user_cur']) ?>">
    <input type="hidden" name="user_rate2" value="<?= htmlspecialchars($ordrow['user_rate']) ?>">
    <input type="hidden" name="user_bal2" value="<?= htmlspecialchars($ordrow['user_bal']) ?>">

      
                      
  

<?php } ?>

              
                      
                        <?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 btn-block" id='btn-mlogin' >CONFIRM WITHDRAWAL </button>
                    </form>
                






<?php

if (isset($_POST['decline'])) {
    $declinePAY = 3; // Declined status
    $log_id = intval($_POST['log_id']); // Sanitize ID

    // Update the confirmPAY field in logins table
    $stmt = $pdo->prepare("UPDATE logins SET confirmPAY = :declinePAY WHERE id = :log_id");
    if ($stmt->execute([':declinePAY' => $declinePAY, ':log_id' => $log_id])) {
        echo "
        <div class='alert alert-success' style='margin-bottom:20px;'>
            Withdrawal Declined Successfully!
        </div>
        <meta http-equiv='refresh' content='3; url=adminverify.php' />
        ";
    } else {
        echo "
        <div class='alert alert-danger'>
            Error declining withdrawal. Please try again.
        </div>
        ";
    }
}
?>




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

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







<?php

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

if ($id <= 0) {
    echo "<script>alert('Invalid withdrawal ID.'); window.location.href='adminverify.php';</script>";
    exit;
}

// Fetch withdrawal info
$stmt = $pdo->prepare("SELECT * FROM withdraw WHERE id = ?");
$stmt->execute([$id]);
$ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$ordrow) {
    echo "<script>alert('Withdrawal not found.'); window.location.href='adminverify.php';</script>";
    exit;
}

// Handle Decline
if (isset($_POST['decline'])) {
    $update = $pdo->prepare("UPDATE withdraw SET status = 3 WHERE id = ?");
    if ($update->execute([$id])) {
        echo "<script>alert('Withdrawal Declined Successfully.'); window.location.href='adminverify.php';</script>";
        exit;
    } else {
        echo "<script>alert('Failed to decline withdrawal.');</script>";
    }
}

// Show form only if not already declined
if ($ordrow['status'] != 3) {
    ?>
    
    
    
    <form action="" class="reservation-form mb-0 bg-silver-deep p-30" method="post">
        <input type="hidden" name="declinePAY" value="3">
        <input type="hidden" name="log_id" value="<?= htmlspecialchars($ordrow['log_id']) ?>">
        <button type="submit" name="decline" class="btn btn-danger btn-block" onclick="return confirm('Are you sure you want to decline this withdrawal?');">
            DECLINE WITHDRAWAL
        </button>
    </form>
    <?php
} else {
    echo "<div class='alert alert-warning'>Withdrawal has already been declined.</div>";
}
?>

<br><br><hr>

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