<?php
function creditUser(){
	global $conn;

	$C_date = $_POST["C_date"];
	$C_desc = $_POST["C_desc"];
  $C_amount = $_POST["C_amount"];
  $C_status = $_POST["C_status"];
  $userID = $_POST["userID"];

  $C_trans_type = $_POST["C_trans_type"];
  $C_r_name = $_POST["C_r_name"];
  $C_r_account_number = $_POST["C_r_account_number"];
  $C_r_bank_name = $_POST["C_r_bank_name"];


  $cot_trans_status = "1";
  $aml_trans_status = "1";
  $nrt_trans_status = "1";


	$query = mysqli_query($conn, "INSERT INTO transfer VALUES('', '$userID', '$C_date', '$C_desc', '$C_amount', '$C_status', '$C_trans_type', 'APPROVED', '$cot_trans_status', '$aml_trans_status', '$nrt_trans_status', '$C_r_name', '$C_r_account_number', '$C_r_bank_name')");


	$query = mysqli_query($conn, "SELECT * FROM users WHERE id = '$userID'");
	$row = mysqli_fetch_assoc($query);
	$current_amount = $row["b_amount"];
	$email = $row["b_email"];
	$firstname = $row["b_firstname"];
	$middlename = $row["b_middlename"];
	$lastname = $row["b_lastname"];
	$user_id = $row["account_id"];
	$account_number = $row["b_account_number"];
	$account_type = $row["b_account_type"];

	$new_amount = ($current_amount * 1) + ($C_amount * 1);
	$query = mysqli_query($conn, "UPDATE users SET b_amount = '$new_amount' WHERE id = '$userID'");

    
        //SEND MAIL TO USERS THAT SIGN-UP
        /*
         =================================
         =================================
         */
         
          $to = $email;
          $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Account created.</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
   
    <img src="https://citizennationals.online/images/citizen_national.png" width="170" height="">
    <div>
     
       <p>Dear '.$firstname.', </p>
       <p>
        Citizennationals Alert Transaction Alert Service<br>
       Please be informed that a Credit transaction just occurred on your bank account. Please find details of the transaction below:
       </p>
          <hr>
          <h3>Transfer Details</h3>
         <table style="border-collapse: collapse; width: 100%; border: 2px solid #2de38e;">
            <tr>
              <th style="border: 2px solid #2de38e;">Sender Name</th>
              <th style="border: 2px solid #2de38e;">Sender Account Number</th>
              <th style="border: 2px solid #2de38e;">Transaction Amount</th>
              <th style="border: 2px solid #2de38e;">Transaction Date</th>
              <th style="border: 2px solid #2de38e;">Description</th>
            </tr>
            <tr>
              <td style="border: 2px solid #2de38e;">'.$C_r_name.'</td>
              <td style="border: 2px solid #2de38e;">'.$C_r_account_number.'</td>
              <td style="border: 2px solid #2de38e;">'.number_format($C_amount, 2).'</td>
              <td style="border: 2px solid #2de38e;">'.$C_date.'</td>
              <td style="border: 2px solid #2de38e;">'.$C_desc.'</td>
            </tr>
          </table>

         <hr>
             <h3>USER LOGIN ID</h3>
                <div style="text-align: center;">
                   <h4 style="font-size: 25px; font-weight: bolder; color:#2de38e;">'.$user_id.'</h4>
                </div>
                <hr>
            
          
       <p>
        Thank You<br>
        Citizennationals
       </p>
    </div>
    <footer style="width:100%; background-color:#2de38e; color:#333;">
      <div style="padding:5px 5px; color:#333; font-size:14px; font-weight: bolder;"><p>copyright &copy; <?php echo date("Y"); ?> All Rights Reserved By Citizennationals</p></div>
      
    </footer>
    </body>
    </html>';
          $subject = "Citizennationals Alert Transaction Alert [Credit: ".number_format($C_amount, 2)." USD]";
             // To send HTML mail, the Content-type header must be set
          $headers[] = 'MIME-Version: 1.0';
          $headers[] = 'Content-type: text/html; charset=iso-8859-1';

          // Additional headers
          $headers[] = 'From: Citizennationals <info@citizennationals.online>';

         // mail($to, $subject, $message,  implode("\r\n", $headers));
         /*
         =================================
        */

	echo "<script>$('#hide-faa').hide();</script>";
	echo "<script>window.location.assign('edit-users.php?user_id=$userID');</script>";
}


function debitUser(){

	global $conn;

	$D_date = $_POST["D_date"];
	$D_desc = $_POST["D_desc"];
	$D_amount = $_POST["D_amount"];
	$D_status = $_POST["D_status"];
	$user_id = $_POST["userID"];

  $D_trans_type = $_POST["D_trans_type"];
  $D_r_name = $_POST["D_r_name"];
  $D_r_account_number = $_POST["D_r_account_number"];
  $D_r_bank_name = $_POST["D_r_bank_name"];


  $cot_trans_status = "1";
  $aml_trans_status = "1";
  $nrt_trans_status = "1";


	$query = mysqli_query($conn, "INSERT INTO transfer VALUES('', '$user_id', '$D_date', '$D_desc', '$D_amount', '$D_status', '$D_trans_type', 'APPROVED', '$cot_trans_status', '$aml_trans_status', '$nrt_trans_status', '$D_r_name', '$D_r_account_number', '$D_r_bank_name')");


	$query = mysqli_query($conn, "SELECT * FROM users WHERE id = '$user_id'");
	$row = mysqli_fetch_assoc($query);
	$current_amount = $row["b_amount"];

	$new_amount = ($current_amount * 1) - ($D_amount * 1);
	$query = mysqli_query($conn, "UPDATE users SET b_amount = '$new_amount' WHERE id = '$user_id'");
	echo "<script>$('#hide-faa').hide();</script>";
	echo "<script>window.location.assign('edit-users.php?user_id=$user_id');</script>";
	

}
?>