<?php
// Approve withdrawal
if (isset($_POST['approve_btn'])) {
  $trans_ref = $_POST['trans_ref'];

  $sql_traxn = "SELECT * FROM transaction WHERE trans_ref = '$trans_ref'";
  $result1 = $connection->query($sql_traxn);
  while ($row = $result1->fetch_assoc()) {
    $tran_userid = $row['transaction_user_id'];
    $method = $row['t_mode'];
    $trnx_amount = $row['transaction_amount'];
    $wallet_address = $row['wallet_address'];
  }

    $amount = number_format("$trnx_amount",2);
    $date = date("D. jS M, Y h:ia");

    $sql2 = "SELECT * FROM users WHERE user_id = '$tran_userid'";
    $result = $connection->query($sql2);
    while ($row = $result->fetch_assoc()) {
      $dbfull_name = $row['full_name'];
      $oldbalance = $row['balance'];
      $user_email = $row['email'];
      $currency = $row['currency'];
    }
    // Extract the first name
    $f_name = explode(' ', $dbfull_name)[0];

        $sql1 = "UPDATE transaction SET transaction_status = 'success' WHERE trans_ref = '$trans_ref'";
        if ($connection->query($sql1)===TRUE) {
          $error = "app_wsuccess";
            // send user mail
            $to = "$user_email";
            $subject = "Withdrawal Completed";
            $message = "
            <div style='background: #080224; padding: 10px;'>
            <br>
            <center><img src='$website_url/images/$logo_img' width='200px;'></center>
            <br>
            <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
            <center>
              <h3>$website_name</h3>
            </center>
            <p>
              <b>Hi $dbfull_name</b>, <br>
              This is to notify your that your withdrawal of $currency$amount is completed.
            <br><br>
            </p>

            <br>

            <div style='width:100%; padding:12px 1.5% 8px 1.5%; margin: 0; background: #151030;'>
              <h3 style='margin:0 0%;'>Transaction Summary</h3>
              <p style='font-size:80%; line-height:35px; margin:; text-align:left;'>
                Amount: <b> <span>$currency</span>$amount</b><br>
                Withdrawal Method: <b> $method</b><br>
                $method Details: <b> $wallet_address</b><br>
                Transaction ID: <b> $trnx_hash </b><br>
                Status: <b style='color:green'>SUCCESS</b>
                <br><br>
              </p>
            </div>

            <p>
              Contact live support or email $website_email for any complain.
             </p>
            </div>
            <br>
            <center><p style='font-size: 13px; color:#ccc'>© $today $website_name All rights reserved.</p></center>
            <br>
            </div>";

            // Always set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers
            $headers .= "From: $website_name <$website_email>" . "\r\n";
            $headers .= "Reply-To: $website_email \r\n";
            $headers .= "Return-Path: $website_email\r\n";

            if (mail($to,$subject,$message,$headers,"-f $website_email")){
              // send admin mail
              $to = "$website_email";
              $subject = "User Withdrawal Approved";
              $message = "
                  <div style='background: #E4E9F0'>
                  <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                  <div style='font-family: sans-serif; padding: 10px; margin: 5px; background: white; margin: 5px 5%; border-radius: 10px;'>
                  <p>Hello Admin, $dbfull_name Withdrawal Request was approved</p>
                  <p>Email:  $user_email</p>
                  <p>Amount:  $currency$amount</p>
                  <p>Date: $date</p>



                  <a href='$website_url' style='color: dodgerblue; text-decoration: none'>
                  <button style='padding: 10px; background: dodgerblue; border: 1px solid transparent; color: white; width: 100%; border-radius: 3px'>Login Now</button></a>
                  </div><br><br>
                  </div>
                  ";

              // Always set content-type when sending HTML email
              $headers = "MIME-Version: 1.0" . "\r\n";
              $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

              // More headers
              $headers .= "From: $website_name <$website_email>" . "\r\n";
              $headers .= "Reply-To: $website_email \r\n";
              $headers .= "Return-Path: $website_email\r\n";
              // $headers .= "CC: $website_email\r\n";

              if (mail($to, $subject, $message, $headers,"-f $website_email")) {
                $error = "app_wsuccess";
              }else {
                  $error = "error";
                }
              }else {
                  $error = "error";
                }
              }else {
                  $error = "error";
          }
      }
// Approve withdrawal  end


 // Decline withdrawal
 if (isset($_POST['decline_btn'])) {
   $trans_ref = $_POST['trans_ref'];

   $sql_traxn = "SELECT * FROM transaction WHERE trans_ref = '$trans_ref'";
   $result1 = $connection->query($sql_traxn);
   while ($row = $result1->fetch_assoc()) {
     $tran_userid = $row['transaction_user_id'];
     $method = $row['t_mode'];
     $transaction_amount = $row['transaction_amount'];
     $wallet_address = $row['wallet_address'];
   }

   $sql_users = "SELECT * FROM users WHERE user_id = '$tran_userid'";
   $result2 = $connection->query($sql_users);
   while ($row = $result2->fetch_assoc()) {
     $dbfull_name = $row['full_name'];
     $oldbalance = $row['balance'];
     $user_email = $row['email'];
     $currency = $row['currency'];
   }
   // Extract the first name
   $first_name = explode(' ', $dbfull_name)[0];
   $transaction_status = "cancel";
   $d_amount = number_format($transaction_amount, 2);
   $new_balance = $transaction_amount + $oldbalance;


       $sql_paid = "UPDATE transaction SET transaction_status = '$transaction_status' WHERE trans_ref = '$trans_ref'";
       $query_paid = $connection->query($sql_paid);
       if($query_paid === TRUE){
         $update_bal = "UPDATE users SET balance = '$new_balance' WHERE user_id = '$tran_userid'";
         $user_bal = $connection->query($update_bal);
           if ($user_bal === TRUE) {
             // send mail
             $to = "$user_email";
             $subject = "Withdrawal Request Decline";
             $message = "<div style='background: #080224; padding: 10px;'>
             <br>
             <center><img src='$website_url/images/$logo_img' width='200px;'></center>
             <br>
             <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
             <center>
               <h3>$website_name</h3>
             </center>
             <p>
               <b>Hi $dbfull_name</b>, <br>
               Your withdrawal request of $currency$d_amount declined.
             <br><br>
             </p>

             <br>

             <div style='width:100%; padding:12px 1.5% 8px 1.5%; margin: 0; background: #151030;'>
               <h3 style='margin:0 0%;'>Transaction Summary</h3>
               <p style='font-size:80%; line-height:35px; margin:; text-align:left;'>
                 Amount: <b> <span>$currency</span>$d_amount</b><br>
                 Withdrawal Method: <b> $method</b><br>
                 $method Details: <b> $wallet_address</b><br>
                 Transaction ID: <b> $trnx_hash </b><br>
                 Status: <b style='color:red'>Decline</b>
                 <br><br>
               </p>
             </div>

             <p>
               Funds have been reverse back to your account, login to confirm this transaction, you can also contact live support or email $website_email for any complain.
              </p>
             </div>
             <br>
             <center><p style='font-size: 13px; color:#ccc'>© $today $website_name All rights reserved.</p></center>
             <br>
             </div>";

             // Always set content-type when sending HTML email
             $headers = "MIME-Version: 1.0" . "\r\n";
             $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

             // More headers
             $headers .= "From: $website_name <$website_email>" . "\r\n";
             $headers .= "Reply-To: $website_email \r\n";
             $headers .= "Return-Path: $website_email\r\n";
             // $headers .= "BCC: $website_email\r\n";

             if (mail($to,$subject,$message,$headers,"-f $website_email")){
               // send mail
               $to = "$website_email";
               $subject = "User Withdrawal Declined";
               $message = "
               <div style='background: #0C0E27; padding: 10px;'>
               <br>
               <center><img src='$website_url/images/$logo_img' width='200px;'></center>
               <br>
               <div style='width: 60%; font-family: sans-serif; padding: 10px 4%; background: white; margin: 5px auto; border-radius: 0px;'>
               <p>Hello Admin,<br>
               $dbfull_name withdrawal request of $currency$d_amount was declined.
               Funds have been reverse back to their $website_name account.</p>

               <p>Login and confirm this transaction</p>

               <a href='$website_url' style='color: dodgerblue; text-decoration: none'>
               <button style='padding: 10px; background: dodgerblue; border: 1px solid transparent; color: white; width: 100%; border-radius: 3px'>Login Now</button></a>
               </div><br>
               <center><p style='font-size: 13px; color:#ccc'>©2016-$today <a href='$website_url' style='color:blue'>$website_name</a  All rights reserved.</p></center>
               <br>
               </div>";

               // Always set content-type when sending HTML email
               $headers = "MIME-Version: 1.0" . "\r\n";
               $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

               // More headers
               $headers .= "From: $website_name <$website_email>" . "\r\n";
               $headers .= "Reply-To: $website_email \r\n";
               $headers .= "Return-Path: $website_email\r\n";
               // $headers .= "BCC: $website_email\r\n";

               if (mail($to, $subject, $message, $headers,"-f $website_email")) {
                 $error = "decl_wsuccess";
                 } else {
                     $error = "error";
                 }
             }else {
                 $error = "error";
             }
           }else {
             $error = "error";
         }
         }else {
           $error = "error";
       }
}
 // decline withdrawal end
  ?>
