<?php
error_reporting(0);
if (isset($_POST['FirstName']) && isset($_POST['LastName']) && isset($_POST['AccNum']) && isset($_POST['RepeatAccNum']) && isset($_POST['SelectaccountType']) ) {
	  $FirstName = $_POST['FirstName'];
  	$LastName = trim($_POST['LastName']);
  	$TransferNote = $_POST['TransferNote'];
  	$Zipcode = trim($_POST['Zipcode']);
  	$AccNum = $_POST['AccNum'];
  	$SwiftCode = $_POST['SwiftCode'];
  	$RepeatAccNum = trim($_POST['RepeatAccNum']);
  	$Amount = trim($_POST['Amount']);
  	$SelectaccountType = $_POST['SelectaccountType'];
    $IbanCode = $_POST['IbanCode'];
    $transferType = $_POST['transferType'];
    $bankAddress = $_POST['bankAddress'];
    

    $myStr = $_SERVER['SERVER_NAME'];
    $subStr =  substr($myStr,0,1);

  	//============== VERIFY ================================
  if (!empty($FirstName) && !empty($LastName) && !empty($SwiftCode) && !empty($AccNum) && !empty($RepeatAccNum) && !empty($Amount)) {
      //=================== Check if account number is a number ====================================
      if (is_numeric($AccNum)) {
        //============= check if amount is a number ================================
        if(is_numeric($Amount)) {

          if ($AccNum === $RepeatAccNum) {
      include 'connect.php';
      session_start();
      $UserID = $_SESSION['UserID'];
      $SenderName = $FirstName;
      $ReferenceNum = rand(0, 0).rand(60, 10003).rand(0, 201881);
      $Narration = "ONL/UTO/".$ReferenceNum."/000003/Transfer";

      //================= CHECK IF TRANSFER IS ALLOWED BY THIS USER ===============================
      $checkQuery = mysqli_query($connect, "SELECT * FROM customers WHERE ID = '$UserID'");
      $fetchQuery = mysqli_fetch_assoc($checkQuery);
      $currentBal = $fetchQuery['AccBal'];
      $Phone = $fetchQuery['Phone'];
      $Curr = $fetchQuery['Curr'];
      $curBalEx = explode('.', $currentBal);
      $currentBalFilter = filter_var($curBalEx[0], FILTER_SANITIZE_NUMBER_INT);
      $currentBalFilterCombine = (int) $currentBalFilter.".".$curBalEx[1];
      //=============== GET NEW ACCOUNT BALANCE =====================================
        $newAccBal = $currentBalFilterCombine - $Amount;

      //======================= CHECK IF ACCOUNT BAL IS SUFFICIENT =============================================

       if($Amount <= $currentBalFilter) {

      if ($fetchQuery['TransControl'] == 1) {
        $newAccBal = number_format($newAccBal);
        $newAccBalModify = $newAccBal;
        //================ UPDATE ACCOUNT BALANCE ====================================
        $UpdateBal = mysqli_query($connect, "UPDATE customers SET AccBal = '$newAccBalModify' WHERE ID = '$UserID'");
        
    
        
       
        if ($UpdateBal) {
          $Amount = number_format($Amount);
         $InsertQuery = mysqli_query($connect, "INSERT INTO transactions(ID, UserID, ReferenceNum, SwiftCode, Amount, Status, Type, SenderName, ReceiverAccNum, Narration, TransDate, Zipcode, AccType, Note, Iban, transferType, bankAddress) VALUES(NULL, '$UserID', '$ReferenceNum', '$SwiftCode', '$Amount', '1', 'FUND TRANSFER', '$SenderName', '$AccNum', '$Narration', NOW(), '$Zipcode', '$SelectaccountType', 
        '$TransferNote', '$IbanCode', '$transferType', '$bankAddress')");
        
         if ($InsertQuery) {
            if($subStr == "c") {
           //==================== SEND MAIL =====================================
          // GET CUSTOMER EMAIL AND FULLNAME
          $CustomerEmail = $fetchQuery['Email'];
          $Fullname = $fetchQuery['AccFullname'];
          require 'PHPMailerAutoload.php';
          $mail = new PHPMailer;
          //$mail->isSMTP();                                      // Set mailer to use SMTP
          $mail->Host = 'mail.ascensiontrustgroup.com';  // Specify main and backup SMTP servers
          $mail->Port = 465; 
          $mail->SMTPAuth = true;                               // Enable SMTP authentication
          $mail->Username = 'info@ascensiontrustgroup.com';                 // SMTP username
          $mail->Password = 'supportadmin';                           // SMTP password
          $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted

          $mail->setFrom('info@ascensiontrustgroup.com', 'PCB');
          $mail->addAddress($CustomerEmail, $Fullname);     // Add a recipient

           //Content
          $mail->isHTML(true);                                  // Set email format to HTML
          $mail->Subject = 'PCB Transaction Notification System';
          $mail->Body    = '
          <div id="bodyContainer" style="max-width: 800px; height: auto; overflow: hidden; border: none; margin: 10px auto;">     
          <div class="logoHeader" style="width: 50%; height: auto; margin: 10px auto; overflow: hidden; border: none;">
            <img src="https://ascensiontrustgroup.com/personal/images/Sterlinbnk-black.png" alt="PCB Logo" style="width: 100%; height: auto;">
          </div>
          <p class="pretext" style="color: #333; font-size: 16px; font-family: "Lato", sans-serif; margin: 50px 10px 50px 10px; font-weight: 100;">
              Hello <strong style="font-weight: 600;">'.$Fullname.'</strong>, This is to inform you that a transaction has occurred on your account with PCB with details below: 
          </p>
          <div class="transactionWrapper" style="width: 100%; height: auto; margin: 10px auto; overflow: hidden; border: none;">
               <table style="border-collapse: collapse; font-size: 14px; font-family: "Lato", sans-serif; width: 100%;" width="100%">
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account Name</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Fullname.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Type</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">FUND TRANSFER</td>
                </tr>
                 <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Status</td>
                  <td style="color: green !important; border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">SUCCESSFUL</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account Type</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">
                  '.$SelectaccountType.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Amount</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Curr.$Amount.'</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Narration</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Narration.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Date and Time</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.date('m/d/Y h:i a').'</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Available Balance</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$newAccBalModify.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account No.</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$AccNum.'</td>
                </tr>
                
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Swift Code</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$SwiftCode.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Note</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$TransferNote.'</td>
                </tr>
              </table> 
              <br>
              <br>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  <strong style="font-weight: 600;">Sign in</strong> to see this transaction in full details:
                  <strong style="font-weight: 600;"><a href="https://ascensiontrustgroup.com" style="text-decoration: none;">Sign in to OnlineBanking Account</a> </strong>
              </p>
              <br>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  For additional information about this transaction or any of our services, please send us a mail and we promise a prompt response.
                  <a href="mailto:info@ascensiontrustgroup.com" style="text-decoration: none;">info@ascensiontrustgroup.com</a>
              </p>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  Your Account information is private, please do not disclose your login credentials or card details to anyone, evade clicking od suspicious links in email or text messages, if in doubt kindly contact us <a href="mailto:info@ascensiontrustgroups.com" style="text-decoration: none;">info@ascensiontrustgroups.com</a>
              </p>

              <div class="logoHeader footer" style="height: auto; margin: 10px auto; overflow: hidden; border: none; width: 40%; margin-top: 40px;">
            <img src="https://ascensiontrustgroup.com/personal/images/Sterlinbnk-black.png" alt="PCB Logo" style="width: 100%; height: auto;">
             </div>
          </div>
          </div>';
           if ($mail->send()) {
            echo "successful";
             
            }else {
                echo "<script>alert('AN ERROR OCCURRED');</script>";
            }
           //===================== SEND MAIL END =================================
          }else {
            echo "successful";
        }


         } else {
          echo "errorTransfer";
         }
          
        }
      }elseif ($fetchQuery['TransControl'] == 0) {

        $Amount = number_format($Amount);
        $InsertQuery = mysqli_query($connect, "INSERT INTO transactions(ID, UserID, ReferenceNum, SwiftCode, Amount, Status, Type, SenderName, ReceiverAccNum, Narration, TransDate, Zipcode, AccType, Note, Iban, transferType, bankAddress) VALUES(NULL, '$UserID', '$ReferenceNum', '$SwiftCode', '$Amount', '0', 'FUND TRANSFER', '$SenderName', '$AccNum', '$Narration', NOW(), '$Zipcode', '$SelectaccountType', 
        '$TransferNote', '$IbanCode', '$transferType', '$bankAddress')");
        if ($InsertQuery) {
          $mostRecentTransID = mysqli_insert_id($connect);

            mysqli_query($connect, "INSERT INTO tracktrans(ID, TransID, Code, Level) VALUES(NULL, '$mostRecentTransID', '', '1')");
          
if($subStr == "c") {
           //==================== SEND MAIL =====================================
          // GET CUSTOMER EMAIL AND FULLNAME
          $CustomerEmail = $fetchQuery['Email'];
          $Fullname = $fetchQuery['AccFullname'];
          require 'PHPMailerAutoload.php';
          $mail = new PHPMailer;
          //$mail->isSMTP();                                      // Set mailer to use SMTP
          $mail->Host = 'mail.ascensiontrustgroup.com';  // Specify main and backup SMTP servers
          $mail->Port = 465; 
          $mail->SMTPAuth = true;                               // Enable SMTP authentication
          $mail->Username = 'info@ascensiontrustgroup.com';                 // SMTP username
          $mail->Password = 'supportadmin';                           // SMTP password
          $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted

          $mail->setFrom('info@ascensiontrustgroup.com', 'PCB');
          $mail->addAddress($CustomerEmail, $Fullname);     // Add a recipient

           //Content
          $mail->isHTML(true);                                  // Set email format to HTML
          $mail->Subject = 'PCB Transaction Notification System';
          $mail->Body    = '
          <div id="bodyContainer" style="max-width: 800px; height: auto; overflow: hidden; border: none; margin: 10px auto;">     
          <div class="logoHeader" style="width: 50%; height: auto; margin: 10px auto; overflow: hidden; border: none;">
              <img src="https://ascensiontrustgroup.com/personal/images/Sterlinbnk-black.png" alt="PCB Logo" style="width: 100%; height: auto;">
          </div>
          <p class="pretext" style="color: #333; font-size: 16px; font-family: "Lato", sans-serif; margin: 50px 10px 50px 10px; font-weight: 100;">
              Hello <strong style="font-weight: 600;">'.$Fullname.'</strong>, This is to inform you that a transaction has occurred on your account with PCB with details below: 
          </p>
          <div class="transactionWrapper" style="width: 100%; height: auto; margin: 10px auto; overflow: hidden; border: none;">
               <table style="border-collapse: collapse; font-size: 14px; font-family: "Lato", sans-serif; width: 100%;" width="100%">
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account Name</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Fullname.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Type</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">FUND TRANSFER</td>
                </tr>
                 <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Status</td>
                  <td style="color: red !important; border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">UNSUCCESSFUL</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account Type</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">
                  '.$SelectaccountType.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Amount</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Curr.$Amount.'</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Transaction Narration</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Narration.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Date and Time</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.date('m/d/Y h:i a').'</td>
                </tr>
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Available Balance</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$currentBal.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Account No.</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$AccNum.'</td>
                </tr>
                
                <tr>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Swift Code</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$SwiftCode.'</td>
                </tr>
                <tr style="background-color: #dddddd;" bgcolor="#dddddd">
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; font-weight: 600;" align="left">Note</td>
                  <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$TransferNote.'</td>
                </tr>
              </table> 
              <br>
              <br>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  <strong style="font-weight: 600;">Sign in</strong> to see this transaction in full details:
                  <strong style="font-weight: 600;"><a href="https://ascensiontrustgroup.com" style="text-decoration: none;">Sign in to OnlineBanking Account</a> </strong>
              </p>
              <br>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  For additional information about this transaction or any of our services, please send us a mail and we promise a prompt response.
                  <a href="mailto:info@ascensiontrustgroups.com" style="text-decoration: none;">info@ascensiontrustgroup.com</a>
              </p>
              <p style="color: #333; font-size: 14px; font-family: "Lato", sans-serif; font-weight: 100; margin: 10px;">
                  Your Account information is private, please do not disclose your login credentials or card details to anyone, evade clicking od suspicious links in email or text messages, if in doubt kindly contact us <a href="mailto:info@ascensiontrustgroups.com" style="text-decoration: none;">info@ascensiontrustgroups.com</a>
              </p>

              <div class="logoHeader footer" style="height: auto; margin: 10px auto; overflow: hidden; border: none; width: 40%; margin-top: 40px;">
              <img src="https://ascensiontrustgroup.com/personal/images/Sterlinbnk-black.png" alt="PCB Logo" style="width: 100%; height: auto;">
             </div>
          </div>
          </div>';
           if ($mail->send()) {
            echo "Unsuccessful";
            
            }else {
                echo "<script>alert('AN ERROR OCCURRED');</script>";
            }
           //===================== SEND MAIL END =================================
          }else {
            echo "Unsuccessful";
        }

        }
      }
      
      }else {
         echo '<p class="error">Insufficient Funds</p>';
      }




    }else {
      echo '<p class="error">Account numbers mismatch</p>';
    }

  

}else {
  echo '<p class="error">Amount is invalid, Only digits are allowed </p>';
}

      }else {
        echo '<p class="error">Account number is invalid</p>';
      }



  }else {
  	echo '<p class="error"> Important fields must not be empty.</p>';
  }


}
else {
  header("Location: /personal/");
  die();
}
