<?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['ref'])){ }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": "#fafafa", 
		 "border-width":"0px", 
		 "border-style":"solid"});	 
	 }
});
</script>
<?php
$ref = RMC($_POST["ref"]); 
$status = RMC($_POST["status"]); 
$old_password = RMC($_POST["old_password"]); 
 if(!empty($ref) 
 && !empty($status)
 //&& !empty($old_password)
 ){ //check if required field is not empty
     
 }else{
     die(
        "
        <div class='alert alert-danger'> A required field is empty. Try again.</div>
        "
        );
 } 

/*
//hash 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>
    ");
}
*/
//
$sql = "SELECT * FROM j_pays where j_deleted < 1 AND j_kind = 'WITHDRAW' AND j_transact_id='".$ref."'";
$result = runSQL($sql);
$count = mysqli_num_rows($result);
$data = mysqli_fetch_array($result);
$transact_id = $data['j_transact_id'];
$r_currency = $data['j_currency'];
$r_amount = $data['j_amount'];
$r_crypto_amount = $data['j_amount_crypto'];
$r_crypto = $data['j_crypto'];
$r_bank = $data['j_bank'];
$r_bank_id = $data['j_bank_id'];
$r_bank_code = $data['j_bank_code'];
$r_bank_num = $data['j_bank_num'];
$r_account_no = $data['j_account_no'];
$r_account_name = $data['j_account_name'];
$user_id = $data['j_user_id'];

// 
if($count < 1){
    die("<div class='alert alert-danger'> Invalid Reference ID.</div>
    <script> $('[name=ref]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}
//check email
$sql4="SELECT * from j_users WHERE j_id='".$user_id."' AND j_blocked < 1 AND j_deleted < 1"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$user_id = $data4['j_id'];
$user_wallet = $data4['j_wallet'];
$user_email = $data4['j_email'];
$user_firstname = $data4['j_firstname'];
$user_lastname = $data4['j_lastname'];
$user_name = trim("$user_firstname $user_lastname");

if($status=="Pending"){
    $status_num = 0;
    //$msg = "This means your payment was received and disbursement to recipient is processing.";
}else{
    $status_num = 1;
    //$msg = "This means your payment was received and disbursement to recipient is successful.";
}
//
//update record
$sql = "UPDATE j_pays SET
j_disburse = '".$status_num."'
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>
    ");
}
//
$u_email = $user_email;
$u_subject ="WITHDRAWAL STATUS - $transact_id";
$u_subject_m ="WITHDRAWAL STATUS";
if($status=="Pending"){
$u_message ="<p>Dear $user_name;</p> <p>Your withdrawal request is still under review. Please be patient while we process and disburse your fund.
<br/><br/>
Reference ID: <b>$transact_id</b>
<br/><br/>
Thank you for choosing <b>$company</b>.
</p>";
}else{
$u_message ="<p>Dear $user_name;</p> <p>Congratulations! Your withdrawal request of ".amount_w_c($r_amount,$r_currency)." has been processed and disbursed.
<br/><br/>
Reference ID: <b>$transact_id</b>
<br/><br/>
Thank you for choosing <b>$company</b>.
</p>";	
}
require("../j_incs/mail.php");
//send notify
send_notify("",$user_id,$u_subject_m,$u_message);
?>
<script>
    $("#form").remove();
     $("#submit").attr('disabled', '');
</script>
<div class="card">
    <div class="card-body">
        <div class="container">
            <div class="row">
                 <div class="col-md-12" style="text-align:center;">
                    <div class="avatar avatar-image avatar-lg avatar-green">
                        <i class="anticon anticon-check"></i>
                     </div>
                     <p>
                        <?php echo $u_message; ?>
                         <br/>
                    </p>
                    <a class="btn btn-success" href="<?php echo $dir; ?>receipt/?ref=<?php echo $transact_id; ?>"> Check Transaction</a>
                    <a class="btn btn-primary" href=""> Confirm Again </a>
                 </div>
            </div>
        </div>
    </div>
</div>
<?php 
/*
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';	
*/
?>