<?php session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("../j_conn/url.php");
require_once("../j_conn/dbc.php");
require_once("../j_incs/functions.php");
if(isset($_POST['old_password'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
?>
<script>
$('.rMe').each(function() {
	 if ($(this).val() == "") {
 	   $(this).css({"border-color": "brown", 
		 "border-width":"1px", 
		 "border-style":"solid"});
	 }else{
		$(this).css({"border-color": "#efefef", 
		 "border-width":"1px", 
		 "border-style":"solid"});	 
	 }
});
</script>
<?php 
//
$old_password = RMC($_POST['old_password']); 
check_length ($old_password,"4","40","Password","old_password");
//

$old_password = base64_encode("$hash$old_password"); 
//check password
$sql4="SELECT count(*) as sum from j_users WHERE j_id='".$id."' AND j_password='".$old_password."' AND  j_blocked < 1 AND j_deleted < 1"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0){
}else{
    die("<div class='alert alert-danger'> Invalid Current Password. Try again.</div>
    <script> $('[name=old_password]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

$re_username = $_SESSION['re_username'];
$amount = $_SESSION['amount'];
$wallet_type = $_SESSION['wallet_type'];




if(empty($re_username)){ 
    die("<div class='alert alert-danger'> Please provide Recipient's Username. Try again.</div>
    <script> $('[name=re_username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

if($amount > 0){ }else{
    die("<div class='alert alert-danger'> Amount must be greater than zero. Try again.</div>
    <script> $('[name=amount]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

$sql25="SELECT * from j_users WHERE 
j_username='".$re_username."' AND j_blocked < 1 AND j_deleted < 1 AND j_status < 1
|| j_email='".$re_username."' AND j_blocked < 1 AND j_deleted < 1  AND j_status < 1
|| j_phone='".$re_username."' AND j_blocked < 1 AND j_deleted < 1  AND j_status < 1
"; //  
$result25 = runSQL($sql25);
$data25 = mysqli_fetch_array($result25);
$re_id = $data25['j_id'];
$re_currency = $data25['j_currency'];
$re_wallet = $data25['j_wallet'];
$re_wallet_usd = $data25['j_wallet_usd'];
$re_wallet_btc = $data25['j_wallet_btc'];
$re_wallet_eth = $data25['j_wallet_eth'];
$re_wallet_ltc = $data25['j_wallet_ltc'];
$re_username = $data25['j_username'];
$re_passport = $data25['j_passport'];
$re_email = $data25['j_email'];
$re_phone = $data25['j_phone'];
$re_firstname = $data25['j_firstname'];
$re_lastname = $data25['j_lastname'];
$re_name = "$re_firstname $re_lastname"; $re_name = trim($re_name);
//
$cd = get_cd($re_currency);
$currency_in_use = $cd[0];
$currency_code_in_use = $cd[1];
$currency_sort_in_use = $cd[2];

if(empty($re_firstname)){ 
    die("<div class='alert alert-danger'> Invalid  Recipient's Username. Try another.</div>
    <script> $('[name=re_username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}
//
$timex = time();
//$bal = TWO_DEC($wallet);
$sql4="SELECT count(*) as sum from j_pays"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum']; $sum4 = $sum4+1;
$transact_id = "$acr-$sum4-$timex";
//record transaction as unpaid
/*
if($wallet_type==$currency_in_use){  $wallet = $re_wallet; $wallet_db = "j_wallet";}
if($wallet_type=="USD"){ $wallet = $re_wallet_usd; $wallet_db = "j_wallet_usd"; }
if($wallet_type=="BTC"){  $wallet = $re_wallet_btc; $wallet_db = "j_wallet_btc"; }
if($wallet_type=="ETH"){ $wallet = $re_wallet_eth; $wallet_db = "j_wallet_eth"; }
if($wallet_type=="LTC"){ $wallet = $re_wallet_ltc; $wallet_db = "j_wallet_ltc"; }
*/

 $wallet = $re_wallet; 
 $wallet_db = "j_wallet";

$re_wallet = $wallet;


//$r_bonus = 0;
$re_bal = ($re_wallet);
//
$ip = getIPAddress();  
//
$paid = 0; 
$type = "credit";
$kind = "A-Credit";
$reason = "Credit alert from $company : $transact_id";
$gateway = "Wallet"; //Paystack Rave
//insert 
$sql = "INSERT INTO j_pays 
(j_transact_id,j_ref,j_user_id,j_currency,j_amount,j_bal,j_type,j_kind,j_desc,j_gateway,j_paid,j_date,j_ip
)
VALUES
('".$transact_id."','".$id."','".$re_id."','".$wallet_type."','".$amount."','".$re_bal."','".$type."','".$kind."','".$reason."','".$gateway."','".$paid."','".$CurrentDate."','".$ip."'
)
";
$result = runSQL($sql);
if(!$result){
   die("
	<div class='alert alert-danger'> Error. Code 104. Try again.</div>
	"
	); 
}

//credit the user wallet
$re_bal = TWO_DEC_CRYPTO($re_wallet+$amount,$wallet_type);
//
$sql = "UPDATE j_users SET
$wallet_db ='".$re_bal."'
WHERE j_id='".$re_id."'";
$result = runSQL($sql);
if(!$result){
	die("Error. Code 103.");
}
//update record
$sql = "UPDATE j_pays SET
j_paid = '1', j_disburse = '1',j_bal ='".$re_bal."'
WHERE j_transact_id='".$transact_id."' AND j_deleted < 1
";
$result = runSQL($sql);
if(!$result){
	die("<div class='alert alert-danger'> Error. Try again.</div>
	");
}

//send a mail for user
$u_email = $re_email;
$u_subject ="WALLET TOP-UP";
$u_subject_m ="WALLET TOP-UP";
/*$u_message ="<p>Dear $re_name;</p> <p>We received your payment and we are glad to inform you that 
your $wallet_type wallet with us has been credited with <b>".amount_w_c($amount,$wallet_type)."</b>
and your new balance is <b>".amount_w_c($re_bal,$wallet_type)."</b>
<br/><br/>
Thank you for choosing <b>$company</b>.
</p>";
*/
$u_message ="
Your excellency you have been credited ".amount_w_c($amount,$wallet_type)." to produce your samples.
Congratulations 👏";
$uu_message = $u_message;
require("../j_incs/mail.php");
//send notify
send_notify("",$re_id,$u_subject_m,$u_message);
?>
<script>
    $(".form_hide").hide();
    $("#form2").remove();
    $("#submit2").attr('disabled', '');
 </script>

 <div class="w-100" style="text-align:center;">
	<div class="responseBox success">
		<i class="anticon anticon-check"></i>
	</div>
	 <p>
		<?php //echo $u_message; ?>
		 Successful
		 <br/>
	</p>
	<a class="btn btn-primary m-b-20 m-r-20" href="<?php echo $dir; ?>receipt/?ref=<?php echo $transact_id; ?>"> Receipt</a>
	<a class="btn btn-success m-b-20 m-r-20" href="<?php echo $dir; ?>payments/"> View Transactions</a>
	<a class="btn btn-danger m-b-20 m-r-20" href=""> Credit Another</a>
 </div>
