<?php

include("includes/header.php"); 
include("includes/sidebar.php"); 

$savings = $db_handle->runBaseQuery("SELECT * FROM savings WHERE status = 'PENDING' ORDER BY id DESC");

$date = date("d-m-Y h:i:sa");

$msg ="";
if (isset($_GET['confirm'])) {
    
    $paymentID = $_GET['confirm'];
    $userIDforPayment = $_GET['user'];
    
    $row = $db_handle->runBaseQuery("SELECT * FROM savings WHERE id = $paymentID");
    
    $amount = $row[0]['amount'];
   
    
    // check if user details already exists in database
    
    $user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $userIDforPayment ");
    
    $email = $user[0]['email'];
    $fname = $user[0]['first_name']." ".$user[0]['surname'];
    $balance = $user[0]['balance'];
    
    $email_message = '<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Deposit Confirmation - '.$company_name.'</title>
  <style>
    body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f9f9f9;
      margin: 0;
      padding: 0;
    }
    .container {
      max-width: 600px;
      margin: 20px auto;
      background: #ffffff;
      padding: 20px;
      border-radius: 6px;
      border: 1px solid #e0e0e0;
      color: #333333;
    }
    .logo {
      text-align: center;
      margin-bottom: 15px;
    }
    .logo img {
      max-width: 180px;
      height: auto;
    }
    h2 {
      color: #2c3e50;
      text-align: center;
    }
    p {
      line-height: 1.5;
    }
    .details {
      background: #f4f4f4;
      padding: 10px;
      border-radius: 4px;
      margin: 15px 0;
      font-size: 14px;
    }
    .footer {
      font-size: 12px;
      color: #999999;
      text-align: center;
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="logo">
      <img src="https://app.ovohinitiative.com//asset/images/logo.png" alt="Ovoh Initiative Logo">
    </div>
    <h2>Deposit Received</h2>
    <p>Hi '.$fname.',</p>
    <p>We are pleased to confirm that your recent deposit has been successfully received and credited to your account.</p>
    
    <div class="details">
      <p><strong>Amount:</strong> NGN'.number_format($amount).'</p>
      <p><strong>Date:</strong> '.$date.'</p>
      
    </div>

    <p>You can view your updated balance and transaction history by logging into your dashboard:</p>
    <p><a href="https://app.ovohinitiative.com/user/dashboard" style="color: #2c3e50;">https://app.ovohinitiative.com/user/dashboard</a></p>

    <p>If you did not authorize this transaction or if you have any questions, please contact our support team immediately:</p>
    <p>Email: <a href="mailto:support@ovohinitiative.com">support@ovohinitiative.com</a></p>

    <div class="footer">
      <p>This is an automated message. Please do not reply directly to this email.</p>
      <p>'.$company_name.' | Warri, Delta State, Nigeria.</p>
    </div>
  </div>
</body>
</html>';
$subject = "Successful Deposit";
    
    
    
//$db_handle->send_mail($email,$email_message,$subject);

        
       $upd =  $db_handle->update("UPDATE savings SET status = 'SUCCESSFUL' WHERE id= ?", "i",array($paymentID));

       if($upd){
           
          // $balance += $amount;
          // $db_handle->update("UPDATE users SET balance = ? WHERE id= ?", "ii",array($balance,$userIDforPayment));
        $url  = "pending-transactions?page=pending-transactions";
        echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';  
        $msg = "<div class='alert alert-success text-center'> Payment verified successfully</div>";
        # code...
       }
        
    }

if(isset($_GET['decline'])){
    $id = $_GET['decline'];
    
    $row = $db_handle->runBaseQuery("SELECT * FROM savings WHERE id = $id");
    
    $amount = $row[0]['amount'];
    $userID = $row[0]['userID'];
   

    $user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $userID ");
    
    $email = $user[0]['email'];
    $fname = $user[0]['first_name']." ".$user[0]['surname'];
    $currency = $user[0]['currency'];
    
    $email_message = '<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Deposit Declined - Ovoh Initiative</title>
  <style>
    body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f9f9f9;
      margin: 0;
      padding: 0;
    }
    .container {
      max-width: 600px;
      margin: 20px auto;
      background: #ffffff;
      padding: 20px;
      border-radius: 6px;
      border: 1px solid #e0e0e0;
      color: #333333;
    }
    .logo {
      text-align: center;
      margin-bottom: 15px;
    }
    .logo img {
      max-width: 180px;
      height: auto;
    }
    h2 {
      color: #d9534f;
      text-align: center;
    }
    p {
      line-height: 1.5;
    }
    .details {
      background: #f4f4f4;
      padding: 10px;
      border-radius: 4px;
      margin: 15px 0;
      font-size: 14px;
    }
    .footer {
      font-size: 12px;
      color: #999999;
      text-align: center;
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="logo">
      <img src="https://app.ovohinitiative.com/asset/images/logo.png" alt="Ovoh Initiative Logo">
    </div>
    <h2>Deposit Declined</h2>
    <p>Hi '.$fname.',</p>
    <p>We regret to inform you that your recent deposit attempt was not successful and has been declined due to some reasons:</p>
    
    <div class="details">
      <p><strong>Amount:</strong> NGN'.number_format($amount).'</p>
       <p><strong>Transaction ID:</strong> '.$transaction_id.'</p>
      <p><strong>Date:</strong> '.$date.'</p>
     
      
    </div>

    <p>Please review your payment details and try again. If you require any assistance, do not hesitate to contact our support team.</p>

    <p>You can also check your account status by logging into your dashboard:</p>
    <p><a href="https://app.ovohinitiative.com/user/dashboard" style="color: #2c3e50;">https://app.ovohinitiative.com/user/dashboard</a></p>

    <p>For any questions or concerns, reach out to us:</p>
    <p>Email: <a href="mailto:support@ovohinitiative.com">support@ovohinitiative.com</a></p>

    <div class="footer">
      <p>This is an automated message. Please do not reply directly to this email.</p>
      <p>Ovohs Initiative | Warri, Delta State, Nigeria.</p>
    </div>
  </div>
</body>
</html>
';
$subject = "Deposit Request Declined";
//$db_handle->send_mail($email,$email_message,$subject);
   
    
    $upd =  $db_handle->update("UPDATE savings SET status = 'UNSUCCESSFUL' WHERE id= ?", "i",array($id));
    $url  = "pending-transactions?page=pending-transactions";
    echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';  
    $msg = "<div class='alert alert-success text-center'> Deposit has been declined.</div>";
}


 ?>
 <div class="main-content app-content">
    <div class="container-fluid">
<div class="row mt-3">
            <div class="col-xl-12">
                <div class="card custom-card">
                    <div class="card-header justify-content-between border-bottom-0">
                        <div class="card-title"> Your Transactions </div>
                        
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table text-nowrap table-bordered" id="TableTab">
                                <thead>
                                    <tr>
                                        <th scope="col">#</th>
                                        <th scope="col">Transaction Id</th>
                                        <th scope="col">Name</th>
                                        <th scope="col">Amount</th>
                                        <th scope="col">Payment Type</th>
                                        
                                        <th scope="col">Date</th>
                                        <th scope="col">Status</th>
                                        <th scope="col">View Proof</th>
                                        <th scope="col">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        if($savings){
                                            
                                            $i = 1;
                                            foreach($savings as $saving){
                                                $userID = $saving['userID'];

                                                    $user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $userID");

                                                    $name = $user[0]['first_name']." ".$user[0]['surname'];
                                                
                                   echo "<tr>";
                                   echo '<td> <span class="fw-medium">'.$i.'</span> </td>';
                                       echo '<td> <span class="fw-medium">'.$saving['transactionID'].'</span> </td>';
                                        
                                        echo '<td> <span class="fw-medium">'.$name.'</span> </td>';
                                        echo '<td class="fw-medium">₦'.number_format($saving['amount']).'</td>';
                                        echo '<td> <span class="fw-medium">'.$saving['method'].'</span> </td>';
                                       
                                       echo '<td> <span class="">'.$saving['status'].'</span> </td>';
                                        echo '<td> <span class="">'.$saving['date'].'</span> </td>';
                                        echo "<td class='tdSpace'>";
                                  
                                    
                                    echo "<a href=". "pending-transactions.php?view=". $saving['id'] ."&page=pending-transactions> ";
                                    echo "<button  class='bg-primary text-white' data-toggle='' data-target='#staticBackdrop' 
                                        style='font-size:0.85em;padding:5px 30px 2px 30px;outline:none;border:none'>";
                                    echo "<i class='fa fa-eye'></i> View Info</button>";
                                    echo "</a>";
                                    echo "</td>";
                                    
                                         echo "<td class='tdSpace'>";
                                    echo "<a href=". "pending-transactions?confirm=". $saving['id'] ."&user=".$userID."&page=pending-transactions>";
                                    echo "<button class='bg-secondary text-white' style='font-size:0.85em;padding:5px 30px 2px 30px;outline:none;border:none'>";
                                    echo "<i class='fa fa-check-circle'></i> Confirm</button>";
                                    echo "</a>";
                                    echo " ";
                                    echo "<a href=". "pending-transactions?decline=". $saving['id']."&page=pending-transactions>";
                                    echo "<button class='bg-danger text-white' style='font-size:0.85em;padding:5px 30px 2px 30px;outline:none;border:none'>";
                                    echo "<i class='fa fa-times-circle'></i> Decline</button>";
                                    echo "</td>";
                                    echo '</tr>';
                                    
                                    $i++;
                                    
                                    
                                    }

                                }

                                ?>
                                   
                                   
                                </tbody>
                            </table>
                        </div>
                    </div>
                   
                </div>
            </div>
        </div>
    </div>
 </div>
 
 <?php

if(isset($_GET['view'])){
    $chk = $_GET['view'];
    
    

    $row = $db_handle->runBaseQuery("SELECT * FROM savings WHERE id = '$chk' ");
            
    $pic = "../proofs/".$row[0]['proof'];
   
    $amount = $row[0]['amount'];
        
?>


<div class="modal fade" id="staticBackdrop" 
     data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
    <div class="modal-content">
        <div class="modal-header">

            <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="outline:none;" onclick="reload()">
              <i aria-hidden="true" class="fa fa-window-close" style="color:#9e0000"></i>
          </button>
        </div>
        <div class="modal-body">


            <?php 
            
            echo "<img class='img-fluid' src='". $pic. "' alt='img'/>";     
            ?>
            
            <h5 class="hashIDDisplay">
                <?php  echo "Amount Deposited: "."$". number_format($amount);?>
                
            </h2>
        </div>
        <div class="modal-footer" style="margin-top:30px;">
            
        </div>
    </div>
  </div>
</div>


<?php } ?>

<?php

include("includes/footer.php"); 


 ?>
<?php 
        if (isset($_GET['view'])) {
            ?>
            <script type='text/javascript'>
                $(function () {
                    $('#staticBackdrop').modal('show');
                });
            </script>
            <?php
        }
    ?>
    
    <script>
        function reload() {
            window.location.href = "pending-transactions?page=pending-transactions";
        }
    </script>
  
 