<?php
require_once("../layouts/members/head.php");
?>



<?php
if(isset($_POST['submit'])){

$user_id = safe_str($_POST['user_id']);
$transfertype = strtolower(safe_str($_POST['transfertype']));
$amount = safe_str($_POST['amount']);
$ref_id = safe_str($_POST['ref_id']);

$reportData = db_fetch_array("reports", " ref_id='".$ref_id."' ");
if(empty($reportData['id'])){

$userData = db_fetch_array("members", " user_id='".$user_id."' ");
if($userData['user_id']>0){

// Credit
if($transfertype=="credit"){

$order_id = order_txn_id();
$old_balance = $userData['balance'];
$new_balance = $old_balance + $amount;

$insData = array();
$insData['site_id'] = $site_data['site_id'];
$insData['user_id'] = $userData['user_id'];
$insData['username'] = $userData['username'];
$insData['parent_id'] = $userData['parent_id'];
$insData['order_id'] = $order_id;
$insData['service'] = "WALLET";
$insData['type'] = "Credit";
$insData['account_number'] = $user_data['mobile'];
$insData['operator'] = "WalletReceived";
$insData['amount'] = $amount;
$insData['main_amount'] = $amount;
$insData['old_balance'] = $old_balance;
$insData['new_balance'] = $new_balance;
$insData['txn_id'] = $order_id;
$insData['ref_id'] = $ref_id;
$insData['description'] = "Rs.{$amount} Has Been Received Into Wallet By Admin for Wallet";
$insData['date_time'] = current_timestamp();
$insData['status'] = "Success";
if(db_insert("reports", $insData)){
db_update("members", ["balance"=>$new_balance], " user_id='".$userData['user_id']."' ");
alert_msg("Rs.{$amount} Credit Successfully","success");

$whamessage = "Dear {$userData['name']},\n" .
"Added Amount: *{$amount}.00* Has Been Received Into Wallet By Admin for Wallet.\n" .
"Old Balance: *{$old_balance}*\n" .
"Total Balance: *{$new_balance}*\n\n" .
"Best Regards,\n" .
"_*{$site_data['brand']}.*_";
send_sms_credentials($userData['mobile'],$whamessage);
}else{
alert_msg('Wallet Credit Error!','danger');  
}



}


// Debit
if($transfertype=="debit"){

$order_id = order_txn_id();
$old_balance = $userData['balance'];
$new_balance = $old_balance - $amount;

$insData = array();
$insData['site_id'] = $site_data['site_id'];
$insData['user_id'] = $userData['user_id'];
$insData['username'] = $userData['username'];
$insData['parent_id'] = $userData['parent_id'];
$insData['order_id'] = $order_id;
$insData['service'] = "WALLET";
$insData['type'] = "Debit";
$insData['account_number'] = $userData['mobile'];
$insData['operator'] = "WalletTransfer";
$insData['amount'] = $amount;
$insData['main_amount'] = $amount;
$insData['old_balance'] = $old_balance;
$insData['new_balance'] = $new_balance;
$insData['txn_id'] = $order_id;
$insData['ref_id'] = $ref_id;
$insData['description'] = "Rs.{$amount} Has Been Debit By Admin for Wallet";
$insData['date_time'] = current_timestamp();
$insData['status'] = "Success";
if(db_insert("reports", $insData)){
db_update("members", ["balance"=>$new_balance], " user_id='".$userData['user_id']."' ");
alert_msg("Rs.{$amount} Debit Successfully","success");

$whamessage = "Dear {$userData['name']},\n" .
"Added Amount: *{$amount}.00* Has Been Debit By Admin for Wallet.\n" .
"Old Balance: *{$old_balance}*\n" .
"Total Balance: *{$new_balance}*\n\n" .
"Best Regards,\n" .
"_*{$site_data['brand']}.*_";
send_sms_credentials($userData['mobile'],$whamessage);
}else{
alert_msg('Wallet Debit Error!','danger');  
}

}

}else{
alert_msg('Member Not Found!','danger');  
}

}else{
alert_msg("Duplicate Reference Number!",'danger'); 
}

}
?>

<div class="row">
  <div class="col-lg-12 col-md-12 col-12 mb-4 hand">
    <div class="card pmd-card hand">
      <div class="card-header bg-primary text-white py-2">
        <div class="d-flex align-items-center">
          <span class="mr-auto"><?=file_name()?></span>
        </div>
      </div>
      <div class="card-body p-0">
        <form class="row px-4 py-3" method="POST" action="">
          <div class="col-md-3 mb-2">
           <label>Members</label>  
           <select name="user_id" class="form-control select2 select2-hidden-accessible" aria-hidden="true" required>
             <option value="">Select Members</option>
<?php
$members = db_fetch_all("members",false," user_id, username, name ");
foreach ($members as $key => $value) {
?>             
<option value="<?=$value['user_id']?>"><?=$value['username']?> <?=$value['name']?></option>
<?php
}
?> 
           </select>
          </div>
          <div class="col-md-2 mb-2">
           <label>Transfer Type</label>  
           <select name="transfertype" class="form-control" required>
             <option value="">Select</option>
             <option value="Credit">Credit</option>
             <option value="Debit">Debit</option>
           </select>
          </div>
          <div class="col-md-2 mb-2">
           <label>Amount</label>  
           <input type="number" name="amount" class="form-control" placeholder="Enter Amount" required>
          </div>
          <div class="col-md-3 mb-2">
           <label>Reference Number</label>  
           <input type="text" name="ref_id" placeholder="Enter Reference Number" value="<?php echo order_txn_id();?>" class="form-control" required>
          </div>
          <div class="col-md-2 mb-2">
           <label>&nbsp;</label>
           <button type="submit" name="submit" class="btn btn-primary btn-block">Submit</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<?php
require_once("../layouts/members/foot.php");
select2(".select2");
?>