<?php
require "../database/connect.php";

    if (isset($_POST['approve'])) {
        $tranx_id = $_POST['tranx_id'];

        $sql_traxn = "SELECT * FROM transaction WHERE trans_ref = '$tranx_id'";
        $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'];
          $user_email = $row['email'];
          $currency = $row['currency'];
        }
        // Extract the first name
        $first_name = explode(' ', $dbfull_name)[0];
        $transaction_status = "success";
        $d_amount = number_format($transaction_amount, 2);

        $sql_paid = "UPDATE transaction SET transaction_status = '$transaction_status' WHERE trans_ref = '$tranx_id'";
        $update_bal = "UPDATE users SET acct_level = 'upgraded', upgrade = 1 WHERE user_id = '$tran_userid'";

        if ($connection->query($sql_paid)===TRUE && $connection->query($update_bal)===TRUE) {
          // send mail
          $to = "$user_email";
          $subject = "Upgrade Approved!!!";
          $message = "<div style='background: #000; 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>Mail From $website_email</h3>
            <h4>Upgrade Approval</h4>
          </center>
          <hr>
          <p>
            <b>Hi $first_name</b>, <br>
            Your payment for account upgraded have been confirm, your $website_name account have been upgraded, you can now enjoy premium features
          <br><br>

          <br><br>
           Contact live support or email $website_email for complain or more enquiries.
          </p>
          <hr>
          <br>

          </div>
          <br>
          <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </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 Upgrade Approved";
            $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, a payment was approve on the system.<br>
            $dbfull_name payment for account upgraded have been confirm
            <br>
            Therefore $dbfull_name $website_name account have been upgraded
            </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'>$website_email<sup>TM</sup> </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 = "approve_success";
              } else {
                  $error = "error";
              }
          }else {
              $error = "error";
          }
        }else {
          $error = "error";
      }
}


//decline payment
if (isset($_POST['cancel'])) {
    $tranx_id = $_POST['tranx_id'];

    $sql_traxn = "SELECT * FROM transaction WHERE trans_ref = '$tranx_id'";
    $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'];
      $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_cancel = "UPDATE transaction SET transaction_status = '$transaction_status' WHERE trans_ref = '$tranx_id'";
        $query_cancel = $connection->query($sql_cancel);
        if($query_cancel === TRUE){
          // send mail
          $to = "$user_email";
          $subject = "Upgrade Declined!!!";
          $message = "<div style='background: #000; 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>Mail From $website_email</h3>
            <h4>Upgrade Declined</h4>
          </center>
          <hr>
          <p>
            <b>Hi $first_name</b>, <br>
            This is to notify your that your request for account upgrade was declined.
          <br><br>

          <br><br>
          Contact live support or email $website_email for complain or enquiries.
          </p>
          <hr>
          <br>

          </div>
          <br>
          <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </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 Upgrade 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, an Upgrade was decline on the system.<br>
            This is to notify you that pending payment of $currency$d_amount for $dbfull_name account upgrade was declined.</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'>$website_email<sup>TM</sup> </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 = "cancel_success";
              } else {
                  $error = "error";
              }
          }else {
              $error = "error";
          }

        }else {
            $error = "error";
        }

}
?>
