<?php
if (isset($_POST['customerID'])) {
require 'connect.php';
$customerID = $_POST['customerID'];
$newAccountBal =  $_POST['Balance'];
$senderName =  $_POST['senderName'];


if ($newAccountBal == '' || $senderName == '') {
echo "<p style='color: red; text-align: center;'>Both fields are required</p>";
}else {
if (is_numeric($newAccountBal)) {
//=================== GET CURRENT ACCOUNT BALANCE ==========================================
$getAccQuery = mysqli_query($connect, "SELECT * FROM customers WHERE ID = '$customerID'");
$fetchDataBal = mysqli_fetch_assoc($getAccQuery);
$currentAccBal = $fetchDataBal['AccBal'];
$Curr = $fetchDataBal['Curr'];
//======================= ADD UP BALANCE =================================
$curBalEx = explode('.', $currentAccBal);
$currentBalFilter = filter_var($curBalEx[0], FILTER_SANITIZE_NUMBER_INT);
$currentBalFilterCombine = $currentBalFilter.".".$curBalEx[1];
//=============== GET NEW ACCOUNT BALANCE =====================================
$newAccBal = $currentBalFilterCombine + $newAccountBal;
$newAccBal = number_format($newAccBal);
// $newAccBal = $newAccBal;
//====== FETCH ACCOUNT MAIL ===============================
$Fullname = $fetchDataBal['AccFullname'];
$AccEmailAddress = $fetchDataBal['Email'];
$Zipcode = rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 8).rand(0, 9).rand(0, 9);
$ReferenceNum = rand(0, 0).rand(60, 10003).rand(0, 201881);
$Narration = "ONL/CRE/".$ReferenceNum."/00293/CREDIT";

$myStr = $_SERVER['SERVER_NAME'];
$subStr =  substr($myStr,0,1);
$TransferNote = "NAN";
$AccNum = rand(0, 0).rand(60, 10003).rand(0, 201881);

// RANDOM NO FOR ACCOUNT TYPE 
$randAccType = rand(0, 3);
if ($randAccType == 2) {
$SelectaccountType = "Business account";
}else {
$SelectaccountType = "Personal account";
}

$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, '$customerID', '$ReferenceNum', '$SwiftCode', '$newAccountBal', '1', 'CREDIT', '$senderName', '$AccNum', '$Narration', NOW(), '$Zipcode', '$SelectaccountType', '$TransferNote', '', '', '')");
if ($InsertQuery) {
$update = mysqli_query($connect, "UPDATE customers SET AccBal = '$newAccBal' WHERE ID = '$customerID'");
if ($update) {

 if($subStr == "c") {
$newAccountBal = number_format($newAccountBal);
// //==================== SEND MAIL =====================================
// // GET CUSTOMER EMAIL AND FULLNAME
// require 'PHPMailerAutoload.php';
// $mail = new PHPMailer;
// //$mail->isSMTP();                                      // Set mailer to use SMTP
// $mail->Host = 'mail.capita14tfinancialgroup.com';  // Specify main and backup SMTP servers
// $mail->Port = 465; 
// $mail->SMTPAuth = true;                               // Enable SMTP authentication
// $mail->Username = 'info@capitalcreditpay.com';                 // SMTP username
// $mail->Password = 'MasterAccess789W';                           // SMTP password
// $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted

// $mail->setFrom('info@capitalcreditpay.com', 'Capital Credit Pay');
// $mail->addAddress($AccEmailAddress, $Fullname);     // Add a recipient   // Add a recipient
// //Content
// $mail->isHTML(true);                                  // Set email format to HTML
// $mail->Subject = 'Capital Credit Pay 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://capitalcreditpay.com/personal/images/creditB.png" alt="Capital Credit pay 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 Capital Credit Pay 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 CREDIT</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">Sender Name</td>
// <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$senderName.'</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.$newAccountBal.'</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">'.$newAccBal.'</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">Zip Code</td>
// <td style="border: 1px solid #dddddd; text-align: left; padding: 15px; box-sizing: border-box; word-wrap: break-word;" align="left">'.$Zipcode.'</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://capitalcreditpay.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@capitalcreditpay.com" style="text-decoration: none;">info@capitalcreditpay.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@capitalcreditpay.com" style="text-decoration: none;">info@capitalcreditpay.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://capitalcreditpay.com/personal/images/creditB.png" alt="CenturyBank Logo" style="width: 100%; height: auto;">
// </div>
// </div>
// </div>';
// if ($mail->send()) {
// echo "<p style='text-align: center; color: green;'>SUCCESS</p>";
// }else {
// echo "<script>alert('Note: Error with sending mail but transaction was completed. ');</script>";
// }
// //===================== SEND MAIL END =================================




}else {
    echo "<p style='text-align: center; color: green;'>SUCCESS</p>";
}

}else {
echo "<p style='text-align: center; color: green;'>Error: could not complete your request at the moment</p>";
}

}

}else {

echo "<p style='color: red; text-align: center;'>New Balance must be ONLY DIGITS</p>";
}

}







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


?>